Will shell scripts called from python persist after the python script completes?

As part of an automated test, I have a python script that should call two shell scripts that start two different servers, which should interact after the script call is completed. (This is actually a jython script, but I'm not sure if this is important.) What can I do to ensure that the servers stay after the python script completes?

At this point, they are called something like this:

def runcmd(str, sleep): debug('Inside runcmd, executing: ' + str) os.chdir("/new/dir/") directory = os.getcwd() print 'current dir: '+ directory os.system(str) t = threading.Thread( target=runcmd, args=( cmd, 50,) ) 
+4
source share
5 answers

Python threads will die with Python. In addition, os.system blocks. But this is normal - if the os.system () command starts a new process (but not a child process), everything will be fine. For example, on Windows, if a command starts with "start", the process "start" d will remain after Python dies.

EDIT: nohup is the equivalent of start for Linux. (Thanks to S. Lott).

+2
source

os.system() not returned until the process that completes is completed. Use subprocess or Runtime.exec() if you want it in a separate process.

+1
source

I wonder if using subprocess.Popen will work better for you.

maybe something like shell = True

0
source

Threads will not work because they are part of the process. A system call will not work as it blocks as your new process completes.

You will need to use something like os.fork() to create a new process and execute it in the new process. Look at the subprocess for some good cookery style solutions to this.

0
source

Typically, to run a long-term server that is independent of its parent, you need to demonize it. Depending on your environment, there are various wrappers that can help in this process.

0
source

Source: https://habr.com/ru/post/1300922/


All Articles