How To Run A Python Script In The Background After SSH Logout?

Published August 7, 2024

Problem: Running Python Scripts After SSH Logout

Running Python scripts on a remote server can stop when the SSH connection closes. This issue stops long-running scripts from finishing their tasks. You need a way to keep Python scripts running in the background after logging out of an SSH session.

Solution: Using nohup Command

What is nohup?

The nohup command, short for "no hangup," is a tool that lets you run processes that keep executing after you log out from a shell. It stops the system from sending the hangup signal (SIGHUP) to the running process when the terminal closes. This signal usually ends processes linked to the closed terminal.

Implementing nohup with Python scripts

To use nohup with your Python script:

  1. Open your terminal or SSH session.
  2. Go to the directory with your Python script.
  3. Run this command:

    nohup python bgservice.py &

The ampersand (&) at the end of the command tells the system to run the process in the background. This lets you use the terminal for other tasks while your script runs.

Tip: Check Running Processes

To check if your nohup process is running, use the 'ps' command:

ps aux | grep python

This will show all running Python processes, including your nohup command.

Managing output with nohup

By default, nohup sends the command's output to a file named "nohup.out" in the current directory. If you want to use a custom output file, you can change the command like this:

nohup python bgservice.py > custom_output.log 2>&1 &

This command sends both standard output and standard error to the "custom_output.log" file.

Alternative Methods for Background Execution

Screen: A Terminal Multiplexer

Screen is a terminal multiplexer that lets you run multiple terminal sessions in one window. It helps keep processes running after you disconnect from an SSH session.

To use Screen with your Python script:

  1. Start a new Screen session:

    screen -S my_python_script
  2. Run your Python script:

    python bgservice.py
  3. Detach from the Screen session by pressing Ctrl+A, then D.

To reattach to the session later, use:

screen -r my_python_script

Managing Multiple Screen Sessions

To manage multiple Screen sessions, use the command screen -ls to list all active sessions. This allows you to see which sessions are running and their names, making it easier to switch between different background processes.

Tmux: Terminal Multiplexer Alternative

Tmux is another terminal multiplexer like Screen. It has more features and is often seen as easier to use.

To use Tmux with your Python script:

  1. Start a new Tmux session:

    tmux new -s my_python_script
  2. Run your Python script:

    python bgservice.py
  3. Detach from the Tmux session by pressing Ctrl+B, then D.

To reattach to the session later, use:

tmux attach -t my_python_script

Compared to Screen, Tmux offers:

  • Better window management
  • More customization options
  • Easier status bar setup

Both Screen and Tmux are good for keeping Python scripts running in the background. The choice between them often depends on what you prefer.

Process Management Tools

Supervisor: A Process Control System

Supervisor is a process control system for Unix-like operating systems. It manages long-running Python scripts. Supervisor starts, stops, and monitors processes, and can restart them if they crash.

Benefits of using Supervisor for Python scripts:

  • Automatic restart on crashes
  • Process management through a web interface or command line
  • Logging of process output
  • Group processes for management

Basic setup and configuration for Supervisor:

  1. Install Supervisor:

    sudo apt-get install supervisor
  2. Create a configuration file for your Python script:

    sudo nano /etc/supervisor/conf.d/bgservice.conf
  3. Add the following content to the file:

    [program:bgservice]
    command=python /path/to/bgservice.py
    directory=/path/to/script/directory
    autostart=true
    autorestart=true
    stderr_logfile=/var/log/bgservice.err.log
    stdout_logfile=/var/log/bgservice.out.log
  4. Reload Supervisor and start your program:

    sudo supervisorctl reread
    sudo supervisorctl update
    sudo supervisorctl start bgservice

Tip: Configuring Environment Variables

To set environment variables for your Python script in Supervisor, add the following line to your configuration file:

environment=ENV_VAR1="value1",ENV_VAR2="value2"

This allows you to pass configuration options to your script without modifying the code.

systemd: System and Service Manager

systemd is a system and service manager for Linux operating systems. It manages Python scripts as system services.

Using systemd for Python script management:

  1. Create a service file:

    sudo nano /etc/systemd/system/bgservice.service
  2. Add the following content:

    [Unit]
    Description=Background Service Python Script
    After=network.target
    
    [Service]
    ExecStart=/usr/bin/python3 /path/to/bgservice.py
    Restart=always
    User=yourusername
    
    [Install]
    WantedBy=multi-user.target
  3. Reload systemd, enable and start the service:

    sudo systemctl daemon-reload
    sudo systemctl enable bgservice.service
    sudo systemctl start bgservice.service

To manage your systemd service:

  • Check status: sudo systemctl status bgservice.service
  • Stop service: sudo systemctl stop bgservice.service
  • Restart service: sudo systemctl restart bgservice.service

Supervisor and systemd offer solutions for managing long-running Python scripts. systemd is more integrated into modern Linux systems, while Supervisor focuses on process management.