1

I have a python script named update.py, and I want to use another python script to check whether the script is running or not. If update.py doesn't run or error, then the script will run update.py.

Can i do it? If there is an example it will be very thankful.

Mirza
  • 13
  • 1
  • 1
  • 3
  • Hi Mirza, welcome to SO! I am a fan of the simplest answer from Vivek. I've used that solution in the past and I haven't found a problem with it. If that suits your purposes, look no further. If you need to provide an administrator more standard control of your process, consider systemd. If I knew how to use it better, I would have provided an answer. systemd is a standard service that is used to stop and start other processes ( and monitor them, too, restarting them if required and configured ). See if you can learn more about systemd - if you want to get more formal. – Mark Oct 05 '18 at 02:49
  • For a minimal embedded system (if not on a distro that already provides systemd), runit is even easier to configure -- a runit service is literally even easier than Vivek's answer; see the sample run scripts at http://smarden.org/runit/runscripts.html – Charles Duffy Oct 05 '18 at 04:32
  • Does this answer your question? [Check to see if python script is running](https://stackoverflow.com/questions/788411/check-to-see-if-python-script-is-running) – Appyens Jul 03 '20 at 11:44

2 Answers2

5

Not sure about what you are asking but as given this may help, so if you just want to call one python script from another then you can use script 1

 #!/usr/bin/python 
 from subprocess import call
 call(["python", "update.py"])

Save this file in a script named script1 and run it, it will compile update.py. If you want to check for any syntax error in update.py then you can use script 2

#!/usr/bin/python
from subprocess import call
call(["python","-m","py_compile", "update.py"])

If script2 compiles without any error then it shows that there is no syntax error in your program.

Thirdly if you want to check if update.py is running currently or not you can use script 3

#!/usr/bin/python
import psutil
import sys
from subprocess import Popen

for process in psutil.process_iter():
    if process.cmdline() == ['python', 'update.py']:
         sys.exit('Process found: exiting.')

print('Process not found: starting it.')
Popen(['python', 'update.py'])

This last script will tell if your script is running or not and if it is not running it will compile it.

0

Scripts are generally used for these kinds of tasks. You have a monitor script that keeps track of update.py that keeps running in the background. It becomes easier if monitor script launches the python script in the beginning.

#!/bin/bash
# Monitor script. 

EXEC=<path>/update.py

while true; do
    "$EXEC" &
    wait # Here the assumption is that you want to run this forever. 
done
apatniv
  • 1,771
  • 10
  • 13
  • Why would you do this, rather than `while true; update.py; done`? There's no value in starting a background process and waiting for it, vs just starting a foreground process (where the parent shell *implicitly* waits). – Charles Duffy Oct 05 '18 at 03:22
  • In the simplest of cases, it is not required at all. I was thinking along the lines of what would I do i had multiple process to monitor or multiple instances of `update` to handle. – apatniv Oct 05 '18 at 03:36
  • Even with multiple processes: `(while true; do process1; done) & (while true; do process2; done)`. When you rely only on `wait` in the multi-process case you need to track the PIDs individually -- it gets to be a lot more complexity, and somewhat error-prone (on account of PID-reuse corner cases and the like). – Charles Duffy Oct 05 '18 at 04:30