Run python as a daemon process
I am using `&`: why isn't the process running in the background?
![](http://engine.adzerk.net/i.gif?e=eyJhdiI6NDE0LCJhdCI6NCwiYnQiOjAsImNtIjozNzM4NjIsImNoIjoxNjUzMSwiY2siOnt9LCJjciI6MTQ1MTYzMiwiZGkiOiI1YjZlNDdiYWFkNDM0YjlhYjg2ZTc4ZDk0MzQyZmVkMSIsImRtIjoxLCJmYyI6MTY3NDM2MiwiZmwiOjE1MzMwNzIsImlwIjoiMjA4LjE0Ni40My41Iiwia3ciOiJzaGVsbCxzc2gsdGVybWluYWwsYmFja2dyb3VuZC1wcm9jZXNzLHByb2Nlc3Nlcyx4LWNvbW11bml0eSx4LWhvc3QtdW5peC5zdGFja2V4Y2hhbmdlLmNvbSIsIm53IjoyMiwicGMiOjAsImVjIjowLCJwciI6NTcyMjIsInJ0IjoyLCJyZiI6Imh0dHA6Ly93d3cuZ29vZ2xlLmNvbS91cmw_c2E9dCZyY3Q9aiZxPSZlc3JjPXMmc291cmNlPXdlYiZjZD01JmNhZD1yamEmdWFjdD04JnZlZD0wYWhVS0V3anBrOTZrbXZqTUFoVkR4Mk1LSFNQa0F3a1FGZ2dfTUFRJnVybD1odHRwOi8vdW5peC5zdGFja2V4Y2hhbmdlLmNvbS9xdWVzdGlvbnMvMTM0OTI0L2ktYW0tdXNpbmctd2h5LWlzbnQtdGhlLXByb2Nlc3MtcnVubmluZy1pbi10aGUtYmFja2dyb3VuZCZ1c2c9QUZRakNORldIQUNscHFrYjU0NWFYdERiclo4LWJJRW9kUSZzaWcyPUJfN3JLeFRBYS1qOUVZd1YzandyUnciLCJzdCI6NTY3NTIsInVrIjoidWUxLTVjMjc0YjQ0NWIzMjQ3MzRhYjIyY2UwODM0NmNjOWU3Iiwiem4iOjI5LCJ0cyI6MTQ2NDI4MjIwODExNSwiYmYiOnRydWUsInBuIjoiYWR6ZXJrMzU1NDk5OTQzIiwiZnEiOjB9&s=0TZSIOZ4uhCfm6l5rV52vojw5io)
I know that I can append I'm SSH'ing into an Ubuntu 12.04 box and running a python program with Why is this? I am using the ampersand to run the process in the background. How can I get it to run regardless of if I am SSH'ed in? |
|||||||||
|
When you close a terminal window, the terminal emulator sends a SIGHUP to the process it is running, your shell. Your shell then forwards that SIGHUP to everything it's running. On your local system, this is the ssh. The ssh then forwards the SIGHUP to what it's running, the remote shell. So your remote shell then sends a SIGHUP to all its processes, your backgrounded program. There are 2 ways around this.
Additionally you must make sure that your program doesn't write to the terminal through STDOUT or STDERR, as both of those will no longer exist once the terminal exits. If you don't redirect them to something like |
|||||||||||||||||
|
![](http://engine.adzerk.net/i.gif?e=eyJhdiI6NDE0LCJhdCI6NCwiYnQiOjAsImNtIjozNzM4NjIsImNoIjoxNjUzMSwiY2siOnt9LCJjciI6MTQ1MTYzNSwiZGkiOiJiZjEzMGFhZDIyMTU0NzVlOWRhMDIyNTI3MDY3NDZlMCIsImRtIjoxLCJmYyI6MTY3NDM2NSwiZmwiOjE1MzMwNzIsImlwIjoiMjA4LjE0Ni40My41Iiwia3ciOiJzaGVsbCxzc2gsdGVybWluYWwsYmFja2dyb3VuZC1wcm9jZXNzLHByb2Nlc3Nlcyx4LWNvbW11bml0eSx4LWhvc3QtdW5peC5zdGFja2V4Y2hhbmdlLmNvbSIsIm53IjoyMiwicGMiOjAsImVjIjowLCJwciI6NTcyMjIsInJ0IjoyLCJyZiI6Imh0dHA6Ly93d3cuZ29vZ2xlLmNvbS91cmw_c2E9dCZyY3Q9aiZxPSZlc3JjPXMmc291cmNlPXdlYiZjZD01JmNhZD1yamEmdWFjdD04JnZlZD0wYWhVS0V3anBrOTZrbXZqTUFoVkR4Mk1LSFNQa0F3a1FGZ2dfTUFRJnVybD1odHRwOi8vdW5peC5zdGFja2V4Y2hhbmdlLmNvbS9xdWVzdGlvbnMvMTM0OTI0L2ktYW0tdXNpbmctd2h5LWlzbnQtdGhlLXByb2Nlc3MtcnVubmluZy1pbi10aGUtYmFja2dyb3VuZCZ1c2c9QUZRakNORldIQUNscHFrYjU0NWFYdERiclo4LWJJRW9kUSZzaWcyPUJfN3JLeFRBYS1qOUVZd1YzandyUnciLCJzdCI6NTY3NTIsInVrIjoidWUxLTVjMjc0YjQ0NWIzMjQ3MzRhYjIyY2UwODM0NmNjOWU3Iiwiem4iOjMwLCJ0cyI6MTQ2NDI4MjIwODExNiwiYmYiOnRydWUsInBuIjoiYWR6ZXJrMzU4NTUzOTY5IiwiZnEiOjB9&s=X4RiHw2YoNXwa6t8fgEzN_FQEL8)
The process is running in the background in the terminal, but the output from
In your case this would be:
|
||||
|
When you logout, background processes associated with the login session are normally killed as well. If you want them to be disconnected from the session, run them with
|
||||
|
The So, when you close the shell which started those children, the children will be closed also. What you appear to want is a daemon process, which is significantly more tricky because it needs to dissociate entirely from the parent process. The shell usually doesn't have a simple way of doing that. |
|||
|
After the command ending with ampersand (&), at the command prompt run the command "bg" bash> python program.py & bash> bg This will put the "&" command to the background bash> jobs This will list the jobs running in the background bash> fg 1 This will bring Job #1 to the foreground Another way, (to be able to log out) bash> at now bash> /full/path/python /full/path/program.py bash> ^d |