How to run Python script in background even after logging out of SSH?

I have a Python script bgservice.py and I want it to work all the time because it is part of the web service that I create. How can I make it work continuously even after logging out of SSH?

+101
python cron service
Jun 04 '10 at 15:39
source share
10 answers

Run nohup python bgservice.py & to get a script to ignore the hover and continue to work. The output will be placed in nohup.out .

Ideally, you run your script with something like supervise so that it can be restarted if (when) it dies,

+195
Jun 04 '10 at 15:41
source share

If you have already started the process and do not want to kill it and restart no noup, you can send it to the background and then edit it.

Ctrl+Z (pause the process)

bg (restart the process in the background

disown %1 (if this is job # 1, use jobs to determine)

+27
Jun 04 '10 at 16:07
source share

You can also use the GNU screen that every Linux / Unix system should have.

If you are running Ubuntu / Debian, its extended version of byobu is also good.

+16
Jun 04 2018-10-06T00:
source share

Perhaps you should turn your python script into the correct python daemon, as described here .

python-daemon is a good tool that you can use to run python scripts as a background daemon process, and not to run the script forever. You need to slightly modify the existing code, but its simple and simple.

If you encounter problems with python-daemon, there is another supervisor utility that will do the same for you, but in this case you will not have to write any code (or modify the existing one), since this is a solution from the mailbox for dismantling processes.

+10
Feb 29 '12 at 5:31
source share

You cannot do this, but I prefer screen .

+6
Jun 04 2018-10-06T00:
source share

Here is a simple solution inside python using a decorator:

 import os, time def daemon(func): def wrapper(*args, **kwargs): if os.fork(): return func(*args, **kwargs) os._exit(os.EX_OK) return wrapper @daemon def my_func(count=10): for i in range(0,count): print('parent pid: %d' % os.getppid()) time.sleep(1) my_func(count=10) #still in parent thread time.sleep(2) #after 2 seconds the function my_func lives on is own 

You can, of course, replace the contents of your bgservice.py file with bgservice.py instead.

+5
Jan 18 '17 at 13:46 on
source share

In zsh, the shell has the ability to do all background processes without nohup.

In ~/.zshrc add the lines:

 setopt nocheckjobs #don't warn about bg processes on exit setopt nohup #don't kill bg processes on exit 

Then you just need to run a process like this: python bgservice.py & , and you no longer need to use the nohup command.

I know that not many people use zsh, but this is a really cool shell that I would recommend.

+3
Jun 04 '10 at 15:57
source share

If you want the process to run forever regardless of whether you are logged in or not, consider starting the process as a daemon.

supervisord is a great solution that can be used to demonstrate any process. It has another supervisorctl management utility that can be used to monitor processes performed by the supervisor.

You do not need to write any additional code or modify existing scripts to make this work. Moreover, detailed documentation makes this process much easier.

After I looped my head around python-daemon for hours, the supervisor is a solution that worked for me in minutes.

Hope this helps someone trying to get python-daemon to work

+1
Oct 11 '16 at 12:46 on
source share

You can also use Yapdi :

Main use:

 import yapdi daemon = yapdi.Daemon() retcode = daemon.daemonize() # This would run in daemon mode; output is not visible if retcode == yapdi.OPERATION_SUCCESSFUL: print('Hello Daemon') 
+1
Jul 26 '19 at 13:44 on
source share

This other thread may be useful to decide whether to use nohup or disown:

https://unix.stackexchange.com/questions/3886/difference-between-nohup-disown-and

0
Jan 31 '19 at 0:12
source share



All Articles