2

I have a long running python script on Linux, and in some situations it needs to execute a command to stop and restart itself. So, I would like to have an external script (either in bash or python) that executes command to restart the original script. Let me elaborate.

Suppose I have original_script.py. In original_script.py I have this in an infinite loop:

if some_error_condition:
    somehow call external script external.sh or external.py

Let's suppose I can call external.sh and it contains this:

#!/bin/bash
command_to_restart_original_script

Finally, I know the command "command_to_restart_original_script". That isn't the problem. What need is the python command to "somehow call external script external.sh". I need the external script (which is a child process) to keep running as the parent process original_script.py is restarting, ie I need the child process to be detached/daemonized. How do I do this?

Marc
  • 3,386
  • 8
  • 44
  • 68
  • Probably related/duplicate: http://stackoverflow.com/questions/473620/how-do-you-create-a-daemon-in-python – dhke Apr 15 '15 at 21:51
  • Another: http://stackoverflow.com/questions/4705564/python-script-as-linux-service-daemon – tzaman Apr 15 '15 at 21:51
  • May I ask a naive question? Unless you want to restart it with specific parameters, you could as well run your script in a shell loop to begin with, then just `sys.exit` it. The loop would restart it right after, without even have to perform an additional fork. – Arkanosis Apr 15 '15 at 21:55
  • Actually my original python script is a twisted program, so I can't really run it in a shell loop as you suggest. Anyway, the answer below worked, the key was the nohup parameter. – Marc Apr 15 '15 at 21:57
  • The first two suggestions in the comments above use some complicated libraries to deamonize. I don't know if they work or not, but the nohup solution below does work and is far simpler. Hopefully this will help someone else who has this same issue. – Marc Apr 15 '15 at 21:59

1 Answers1

4

I found lots of suggestions in various places, but the only answer that worked for me was this: How to launch and run external script in background?

import subprocess
subprocess.Popen(["nohup", "python", "test.py"])

In my case I ran a script called longrun.sh so the actual command is:

import subprocess
subprocess.Popen(["nohup", "/bin/bash", "longrun.sh"])

I tested this using this run.py:

import subprocess
subprocess.Popen(["nohup", "/bin/bash", "longrun.sh"])
print "Done!"

and I verified (using ps -ax | grep longrun) that longrun.sh does indeed run in the background long after run.py exits.

Justin
  • 6,611
  • 3
  • 36
  • 57
Marc
  • 3,386
  • 8
  • 44
  • 68