Check a Python Script Is Running - python

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.

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.

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

Related

How to terminate a process with python in Ubuntu?

I need to close an .exe at the end of my code. I was able to start the .exe file
proc = subprocess.Popen('.nameProgram.exe')
Now I have to close it but the terminate() function doesn't seem to work.
I tried this code:
proc.terminate()
I noticed that the exe executable is under another java process. How can I close it. Do you have any suggestions? Thanks
Type
ps
In terminal & check on which name program is runing once you run. copy that name.
#inside python program
import os
os.system('pkill programName')

Cant trigger python 3 code with asterisk AGI even on using subprocess inside python2

I am running a python2 code which is triggered by dial plan. In order to process saved recording I need to run a python 3 script. Is there any way to do it. If I am switching the code to python3 the code is not working.
This is the extension
same=>n,AGI(code.py)
in code.py on giving the header
#!/usr/bin/env python2
i am able to run the function
def run_cmd(cmd):
#This runs the general command
sys.stderr.write(cmd)
sys.stderr.flush()
sys.stdout.write(cmd)
sys.stdout.flush()
result = sys.stdin.readline().strip()
checkresult(result)
which is able to process various agi command
but on switching it to python 3 #!/usr/bin/env python3
code wont run.
Now I need to use google cloud engine to process something thats written in python 3
Is there a way to make it run
i have done
def run_sys_command(command):
subprocess.call(command, shell=True)
checkresult(result)
command = "sudo python3 /root/Downloads/check2.py"
run_sys_command(command)
Is there any way to run the python 3 script or any way to run python 3 script directly with agi.
I have checked permission n everything
Sure you can run threads inside AGI.
But it should be stopped before AGI script end.
The simplest way do what you want - setup some type of queue(rabbitmq/simple tasks list in mysql?) and process it outside asterisk process scope.
There is no any problem with running python3 as AGI script. I have plenty of such scripts in my projects. Just check your code.

Run python script from another python script but not as an child process

Is it possible to run a python script from another python script without wating for termination.
Parent process will terminate immediately after creation of child process.
I tried:
subprocess.Popen([sys.executable, "main.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
and also:
os.system(...)
If you know that the other Python script has a main method, you could simply in you code call that other script:
import main
...
exit(main.main())
But here the other script executes in the context of calling script. If you want to avoid it, you could use the os.exec... functions, by launching a new Python interpretor:
import os
...
os.execl(sys.executable, "python", 'main.py')
The exec family functions will replace (under Unix-Linux) the current Python interpretor with a new one.
You can just add & to start script in background:
import os
os.system('/path/to/script.sh &')
exit()
In this case launched shell script will continue working even after main Python script exits.
But keep in mind that it can cause zombie processes appearance in our system.

How to write a python script (on linux) that executes another script and exits?

I want script a.py to execute script B.y, then exit immediately.
script B.y is then to continue running indefinitely and regularly as if run from the command line.
Target system is Linux Centos if it makes any difference
I guess Popen subprocess is what you are looking for, i.e.:
For windows, something like:
import sys ,subprocess
subprocess.Popen(["C:/Python27/python.exe", "C:/path/to/script.py"])
sys.exit(0)
For linux, just change the path:
import sys ,subprocess
subprocess.Popen(["/usr/local/bin/python", "/path/to/script.py"])
sys.exit(0)
Note:
To find python location on linux, you can use which python

subprocess running python getting import error

I'm trying to run a python script from a python program by kicking it off from subprocess (The reason is that the main program has to have exited when the script runs, with a combination of wx.CallAfter and Close). However when the script runs I get an error on line 1 with ImportError: No module named os which makes me think it's something to do with the PythonPath, but I can run the script just fine from a terminal.
Why can't the script see any core modules when run this way?
Edit:
The line in question is:
wx.CallAfter(subprocess.Popen,'python %s "%s" %s %s'%(os.path.join(BASE_DIR,"updatecopy.py"),BASE_DIR,pos[0],pos[1]),shell=True)
BASE_DIR is just the directory that the script lives in.
subprocess is there because os.exec* has been deprecated so I wouldn't suggest using that in place of Popen as someone suggested.
I've seen this issue crop up when running from a frozen process. If that is the case then you're most likely inheriting a weird environment for the new python process.
Most frozen scripts will be trying to run from a zip file, in which case it's no wonder that Python can't find anything, it's all trapped in a zip file :)
If this is the situation then try running using the python executable that you are using to run the frozen script. It should be able to deal with the special environment.
Maybe you could use os.execv instead of Popen.
From os/python docs:
These functions all execute a new program, replacing the current process; they do not return. On Unix, the new executable is loaded into the current process, and will have the same process id as the caller. Errors will be reported as OSError exceptions.
(emphasis mine)

Categories

Resources