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

13,152

Solution 1

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

Solution 2

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.

Share:
13,152
Basj
Author by

Basj

I work on R&amp;D involving Python, maths, machine learning, deep learning, data science, product design, and MacGyver solutions to complex problems. I love prototyping, building proofs-of-concept. For consulting/freelancing inquiries : [email protected]

Updated on June 19, 2022

Comments

  • Basj
    Basj almost 2 years

    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.