Through the subprocess run the command. The task is to output the executable command to the log and get the exit code of the executable command. Print to the log, I understand how, but how to get another return code and depending on this, it is not clear what to do.

Code example:

rsync = 'rsync /tmp/1 /tmp/2' command_process = subprocess.Popen( rsync, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) command_output = command_process.communicate()[0] debbug_rsync = open('debbug_rsync.log', 'a+') debbug_rsync.write('[ {} ]'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) debbug_rsync.write(rsync + '\n') debbug_rsync.write(command_output) debbug_rsync.close() 

    1 answer 1

    Like this:

     command_process = subprocess.Popen( rsync, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) command_output = command_process.communicate()[0] exit_code = command_process.returncode 

    https://docs.python.org/2/library/subprocess.html#subprocess.Popen.returncode

    • Yes, this is what you need! My mistake was that I did not understand what process the returncode was monitoring. Thank! - Kislik
    • @nikitamerzlikin Check the answer as a solution if it is. - zed
    • @nikitamerzlikin in your case, you can also remove the shell=True and send the command as a list of parameters, not a string. - jfs