I want a simple script to stay running in the background. It currently looks like this:
import keyboard
while True:
keyboard.wait('q')
keyboard.send('ctrl+6')
Now, this works already (When q is pressed, it also presses ctrl+6), but I guess there has to be a more efficient way of keeping a program running, so it can act on input.
I would rather not use an infinite while loop.
I'm on Windows
Thanks :)
You can using nohup
nohup python script.py > log.txt 2>&1 &
If you want check command is running
ps aux | grep script
user 5124 <- process_id 1.0 0.3 214588 13852 pts/4 Sl+ 11:19 0:00 python script.py
Kill command is running
kill -9 [process_id]
I was searching for something same and landed here. Here is another solution. Its working fine for me. Maybe useful to someone else too.
Use the -i option at the command line. Thus, if your Python script is foo.py, execute the following at the command line:
python -i foo.py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With