Automate sending CMD command in specific window using python - python

I'm running a couple of things on my home server, two of which are Minecraft servers. I want to set up an automatic restart of the computer every week, but first want to close the servers in the correct way. This is done by sending the command "stop" in their specific CMD windows.
I've successfully been able to select and open specific CMD windows as shown below:
import pygetwindow as gw
handle = gw.getWindowsWithTitle('C:\Windows\system32\cmd.exe')[0]
handle.activate()
My problem is that I can't seem to be able to send the "stop" command in those specific windows. If I for example run the following commands instead:
import pygetwindow as gw
import os
print(gw.getAllTitles())
handle = gw.getWindowsWithTitle('C:\Windows\system32\cmd.exe')[0]
handle.activate()
os.system("stop")
It doesn't do anything because it opens a new CMD window. There might be other issues as well, but since I haven't been able to solve that issue I haven't encountered them yet.
I would really appreciate some help, cause I'm stuck.

I would use a plugin like AutoRestart https://www.spigotmc.org/resources/autorestart.2538/ for Spigot. If you're on vanilla, I would reccomend upgrading to Spigot (or i like to use Pufferfish/Paper)

Related

Keyboard library python3

I am trying to do a python script and while the script is running in the background I want it to listen at some buttons, the problem is that I can't even this simple program to get to work, I get segmentation fault 11. When I run it I use sudo python3 prog.py.
import keyboard
keyboard.wait('esc')
print("test")
Couple of things:
You don't compile a Python program
The problem is that you don't have permission to access that memory, hence the segfault. The fix depends on your system, on my Mac I just changed the permissions of the terminal (so that it could control my computer).
Also, I just want to mention that a process in the background does not take in data from STDIN, so you may have a problem there depending on what you mean.

How to start a new console and run a command in it programmatically?

I'm running tons of experiments and I'm tired of manually opening a terminal and typing:
!tensorboard --logdir="C:\Users\OneDrive \Pycharm\DANN MNISTM SVHN\tmp\1561358957553" --host localhost --port 9353
to start TensorBoard. I want to do this programmatically.
I want to do this in Pycharm, or even Jupyter. But, I want this to be done in a new notebook / terminal in case of Jupyter, not the one I'm currently running cause this will hog the terminal and prevents me from doing extra processing. Same thing in case of Pycharm, I want the command above to be run in a new IPython console / terminal. Is there a Python/IPython way to do that?
Here is what I tried:
import os
os.startafile('cmd')
But I don't know how to write commands to that newly created window without going to it manually.
I also tried subprocess Popen, but it didn't work, in particular, I created a baby process using Popen but when I call communicate method and send a command it waits for a response! I don't want to wait, there is no any response. I just want to move on to next command.
I solved it, but I'm not satisifed.
I used os.popen('the command'). It is working fine, but, it keeps sending back updates to my interactive consonle, this clutters it with useless wiritings, tons of them.

Module won't work in IDLE

This has been troubling me for the past hour. My python script won't run; however, if I select 'check module', input the code, and run the same thing it works. It's a very simple script:
import pyscreenshot as getImage
im = getImage.grab(bbox=(1300,800,1500,850))
im.save("screen.png")
Try running through terminal and it will work.
Although I do not know why this happens exactly (probably ask those who maintain it) I can tell you it is because of some terminal dependant function that is created while running.
The IDLE is not actually a terminal so it cannot run exactly like a terminal (although it outputs the same content, it is not a terminal). For example running os.get_terminal_size() under the IDLE will not work yet the terminal will. There are also some functions in PIL that perform in the same way.
Anyway this post shows a pretty similar code and it is mentioned it doesn't work under the IDLE.
You need to set childprocess=False as IDLE has problems with multiprocessing. IDLE runs user code in a separate process so with pyscreenshot IDLE hangs and seems like the module won't work. You may go through this to know more.
An option is to turn off multiprocessing by setting childprocess=False.
Try:
import pyscreenshot as getImage
im = getImage.grab(bbox=(1300,800,1500,850), childprocess=False)
im.save("screen.png")

Using python to talk to a terminal program back/forth

So I've been using subprocess and pexpect
to try to interact with a separate program running in the terminal. I need to feed it a command, with arguments, and be able to receive it's response and potentially send it more commands.
With subprocess, I have only been able to launch a terminal, but not feed it commands. Or I can pass ONE line of command to an emulated terminal within python. The issue it that it's one-and-done and I can't really interact with it.
pexpect seems to only be able to initiate one command, and then respond to the terminal in an automated fashion, I couldn't find relevant and up to date documentation that went over what I needed.
Are there better modules to use for this? Or am I using them the wrong way?
-Thanks,
-Sean
pexpect is your best candidate, as far as I'm aware.
It's documentation matches version on pypi - 3.2 as for now.
If you would like to run bunch of commands one after another you can try to divide commands with ";" or "&", depends on your usage.
Btw. please take a look at example section.

Python: How to start a process with Administrator Permissions?

I am starting the following script from a Windows 7 command line with administrator permissions:
import win32com.client
import time
import SendKeys
import os
from ctypes import *
shell = win32com.client.Dispatch("WScript.Shell")
os.startfile("C:\...exe")
I have also assigned the feature 'Run this programme as an administrator' to python.exe under Properties > Compatibility > Privilege Level. This did not change anything.
The programme still behaves differently when opened this way to how it behaves when I just open it via a double click on screen. Am I missing some important bit here? Will the process invoked in this way not be run as if started with administrator privileges?
Thanks for your help in advance!
Cheers -
Pat
I don't have access to Vista or Windows 7, but you should be able to use the runas command.
import subprocess
subprocess.call(['runas', '/user:Administrator', 'C:/my_program.exe'])
OK ... I figured out what the problem was. It actually had nothing to do with permissions, contrary to my initial suspicion. Sorry about that!
The reason why the application did not work correctly was because the Python script was located and invoked in another directory. For that reason, some of the application's dependencies were not referenced correctly and it couldn't find some of the files it needs to run properly. Moving the python script into the same directory as the invoked application was one way to fix that.
Sorry again for the misleading initial interpretation of what seemed to be the matter.

Categories