Starting/stopping a background Python process wtihout nohup + ps aux grep + kill

Basj picture Basj · Jan 9, 2016 · Viewed 11.1k times · Source

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.

Answer

terence hill picture terence hill · Jan 10, 2016

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