Running python script in background in windows 10 - python

I am running a python in Windows 10 Command prompt. After the system is kept idle the script stop working. The system also had sleep mode turned off. I need to press "enter" key to make script working again. I tried windows scheduler, however it shows CMD pop up everytime script is executed. Please suggest solution to run the script without any human intervention. I am running the script in cmd with 'schedule' module for running after specific interval.

Related

Shell script kill command

OS: Raspbian
Python: 3.7.3
I am trying to run and kill my shell script through a Python script. The purpose is so that I can simply press "Run" in my py script, instead of having to go through the terminal every time. Here is my shell script (T.sh):
#!/bin/bash
#Track command
cd /home/pi/rpi-deep-pantilt
. . /rpi-deep-pantilt-env/bin/activate
rpi-deep-pantilt track Raspi --edge-tpu
Here is my Py script:
import os
os.system('bash /home/pi/T.sh')
When I issue the command rpi-deep-pantilt track Raspi --edge-tpu in my terminal and press CTRL + C it kills the script, but it doesn't work when I use this Python script, and neither does pkill. The Python script stops, but the camera stays on and the tracking functionality is still operating. Is there any way I can incorporate some kill command that I can issue with a key interruption?
If there is a better way to go about this let me know. I'm very new to this as you can probably tell.
Python may create a new process for T.sh, so within your python code, try:
os.system("pkill T.sh")

Script runs on windows startup but exits immediately even with forever loop

I am trying to make my python script to start on windows boot. I have managed to start with registry, shortcut in startup folder and taskscheduler. However, windows stops the script immediately. This does not persist if I use the same start command on CMD or double clicking the shortcut. Only when trying to get it start automatically.
One more problem i have noticed, that when i run script with .pyw extension nothing happens. But it works if i use pythonw in CMD. Not sure if that has to do anything with the problem.
I am using Windows 10, python 3.7 and this is my code:
from serial import Serial
from time import sleep
ser = Serial('COM7', 9600)
while True:
ser.write(b'1')
sleep(1)
Launch script:
pythonw "%UserProfile%\PyCharm\hearthbeat\hearthbeat.pyw"

Waiting until a os.system command finishes in my virtual machine (Windows 7)

I have encountered a problem with my python script. I am using python 3.6.2 I am using the os.system function to execute a bash command. The problem is the script launches the bash command and continues without waiting it to finish, knowing that the script acts correctly in my physical machine, it waits till the bash command is done then continues with the following instructions. This is my script:
import os
os.system("C:/Bash/bashCmd.sh")
print("ok")
I also tried subprocess.call, but it never waits in my vm.
My question is about the source of this issue, then if any suggestions to deal with it!

Launch Windows games from shell using Python

I want to execute and terminate Windows games from shell. The actual need is to launch games using Python upon receiving specific command. I have tried using the following code to launch game.
import subprocess
subprocess.Popen("D:/Entertainement/Games/NFS1/speed.exe",shell=False)
When I run this code, the game launches and stops without any errors. But the same game runs fine when executed from Windows Explorer. The game doesn't require administrative privileges and runs fine without that.
(Posted on behalf of the OP).
I found a workaround. Instead of launching the executable directly, i created a shortcut and launched it with the following command.
import subprocess
p = subprocess.Popen("C:\Users\devel\Desktop\speed.exe.lnk",shell=True)
p.wait()
print("Program Exited with return code : "+str(p.returncode))
Now everything works fine and my Python program waits for the return code. Thank you all for replying.

Hanging script and cmd won't close

I am trying to run a python script via cmd prompt on my work PC (Windows 7, Python 2.7). The script requires filepaths from different drives on my PC. I am correctly pulling all necessary filepaths and I press Enter to run the script but the script just hangs. The only thing that shows is a blinking underscore. I try to click the X to close the prompt but nothing happens.
I am not able to Ctrl+C out of the program either. I open up Task Manager and I am not able to End Task (nothing happens) or End Process (cmd.exe doesn't even show up in this tab). I also tried Start-->Run-->taskkill /im cmd.exe but nothing happens. The rest of my team has no problem with Python 2.7. The only way to get out of the frozen cmd is to hold down the power button. I do not want to have to keep going through this process especially since this is during work. I'm hoping someone will be able to help me out:
Any idea what's wrong with the version of Python I am using?
How am I able to kill cmd.exe so that I can continue normal work functions without having to hold down the power button and waiting 5-10 minutes to reboot my PC?
The filepaths I was pulling files from were through ClearCase directories. It turned out that I simply had to reinstall ClearCase. There must have been some configuration issue that was causing the cmd to hang and thus forcing a hard reboot. It's good to point out that I am no longer experiencing this problem and that nothing was wrong with the python scripts.

Categories