python subprocess popen nohup and return code -


i have following code:

    argpass = ['nohup']     argpass.append('/path/to/script')      log_output_err = open(log_file,'a+')     out = subprocess.popen(argpass,                        stdout = log_output_err,                        stderr = log_output_err)      #if script fails need have logic here... 

i wonder how can return code of /path/to/script.

maybe need insert logic in /path/to/script, thoughts?

thanks,

the subprocess.popen object has returncode attribute can access:

http://docs.python.org/2/library/subprocess.html#subprocess.popen.returncode

you @ using check_call convenience function:

http://docs.python.org/2/library/subprocess.html#subprocess.check_call

it return if return code zero; otherwise, raise calledprocesserror (from may read returncode attribute).

your example, stdout , stderr pointing @ invoking python script rather log file:

>>> import subprocess >>> argpass = ['echo'] >>> argpass.append('hello world') >>> # reroute pipe because don't have logfile >>> log_output_err = subprocess.pipe  >>> out = subprocess.popen(argpass,                stdout = log_output_err,                stderr = log_output_err) >>> output,error = out.communicate() >>> print output hello world  >>> # check if child process has terminated.  >>> # if has finished, return returncode attribute. >>> # otherwise, returns none >>> out.poll() 0 >>> # or can read returncode attribute directly: >>> out.returncode # direct 0 >>>  

if process take long time finish, returncode value might not set when go check it. if value of returncode none means child process has not yet terminated. can stop execution of script until after child process has terminated .wait() method.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -