Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to keep a Python script running?

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 :)

like image 629
Jojo Avatar asked Jan 28 '26 15:01

Jojo


2 Answers

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]
like image 163
Giang Avatar answered Jan 30 '26 05:01

Giang


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
like image 22
Adnan Ali Avatar answered Jan 30 '26 04:01

Adnan Ali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!