I usually use:
nohup python -u myscript.py &> ./mylog.log & # or should I use nohup 2>&1 ? I never remember
to start a background Python process that I'd like to continue running even if I log out, and:
ps aux |grep python
# check for the relevant PID
kill <relevantPID>
It works but it's a annoying to do all these steps.
I've read some methods in which you need to save the PID in some file, but that's even more hassle.
Is there a clean method to easily start / stop a Python script? like:
startpy myscript.py # will automatically continue running in
# background even if I log out
# two days later, even if I logged out / logged in again the meantime
stoppy myscript.py
Or could this long part nohup python -u myscript.py &> ./mylog.log & be written in the shebang of the script, such that I could start the script easily with ./myscript.py instead of writing the long nohup line?
Note : I'm looking for a one or two line solution, I don't want to have to write a dedicated systemd service for this operation.
2 Answers 2
As far as I know, there are just two (or maybe three or maybe four?) solutions to the problem of running background scripts on remote systems.
1) nohup
nohup python -u myscript.py > ./mylog.log 2>&1 &
1 bis) disown
Same as above, slightly different because it actually remove the program to the shell job lists, preventing the SIGHUP to be sent.
2) screen (or tmux as suggested by neared)
Here you will find a starting point for screen.
See this post for a great explanation of how background processes works. Another related post.
3) Bash
Another solution is to write two bash functions that do the job:
mynohup () {
[[ "1ドル" = "" ]] && echo "usage: mynohup python_script" && return 0
nohup python -u "1ドル" > "${1%.*}.log" 2>&1 < /dev/null &
}
mykill() {
ps -ef | grep "1ドル" | grep -v grep | awk '{print 2ドル}' | xargs kill
echo "process "1ドル" killed"
}
Just put the above functions in your ~/.bashrc or ~/.bash_profile and use them as normal bash commands.
Now you can do exactly what you told:
mynohup myscript.py # will automatically continue running in
# background even if I log out
# two days later, even if I logged out / logged in again the meantime
mykill myscript.py
4) Daemon
This daemon module is very useful:
python myscript.py start
python myscript.py stop
3 Comments
myrun that will be used with myrun script.py that 1) reads the first line of script.py, remove the leading #MYRUN: 2) execute the command written in this first line. Example: doing myrun script.py with this file will execute nohup python -u script.py &.screen and that's awesome and simple. screen -s myscript, then run the script python myscript.py then detach with CTRL+A, D, then I can log out etc. To reattach screen -r myscript, that's it!Do you mean log in and out remotely (e.g. via SSH)? If so, a simple solution is to install tmux (terminal multiplexer). It creates a server for terminals that run underneath it as clients. You open up tmux with tmux, type in your command, type in CONTROL+B+D to 'detach' from tmux, and then type exit at the main terminal to log out. When you log back in, tmux and the processes running in it will still be running.
2 Comments
nohup python -u myscript.py &> ./mylog.log & every time? Can I put this thing in shebang?logout or with exit, the script will continue to run either way. The only things you will need to type are tmux, command to run, CTRL + B + D (to detach from tmux), and logout. When you log back in and want to stop the script, tmux will still be running. You can connect to it with tmux attach. As another plus, you can run as many terminal clients in one window as you want. See here for its many configuration options: manpages.ubuntu.com/manpages/precise/en/man1/tmux.1.html
os.system()orsubprocessto run a program in the backgound. You can also useos.kill()instead of GNUkill. And here's some ways to get a list of running processes. But something I don't understand: What do you mean about two days later, even after some reboot ?osmodule). I want to start / stop a background-running-Python process easily, in one or two lines, from bash.pkillorkillallcommand? Which kills a process by it's name.nohup python myscript...works bad whereasnohup python -u myscript...ensures everyting is written, unbuffered etc.)