All Products
Search
Document Center

:Keep processes running on Linux after an SSH disconnect

Last Updated:Nov 05, 2025

Choose a solution

To keep a command running after your SSH session closes, choose one of the following solutions:

Solution 1: Use the nohup and & commands

The nohup command lets a process continue running after you close the terminal, and the & symbol sends the process to the background.

  1. Run the command in the background.

    This command redirects both output and error to [your_log_file].

    sudo nohup [your_command] >[your_log_file] 2>&1 &
    For example, to run bash hello.sh in the background and write the output to a new file named output.log in the current directory, run the following command: sudo nohup bash hello.sh > output.log 2>&1 &.
  2. View the command output.

    sudo tail -f [your_log_file]
  3. End the process.

    Important

    Terminating the process ends the command or task. Proceed with caution.

    1. Find the Process ID (PID). Replace [your_command] with the command you ran in Step 1.

      sudo ps aux | grep "[your_command]"
      For example, if you ran sudo nohup bash hello.sh > output.log 2>&1 &, you would run sudo ps aux | grep "bash hello.sh" to find its PID.
    2. Use the sudo kill [PID] command to terminate the process.

Solution 2: Use GNU screen

  1. Install GNU screen.

    • For Alibaba Cloud Linux and CentOS

      sudo yum install -y screen
    • For Debian and Ubuntu

      sudo apt-get install -y screen
  2. Create and enter a new session.

    [Name] is a custom name for your session.

    screen -S [Name]
    For example, to create a session named mysession, run sudo screen -S mysession.
  3. Run your task in the new session.

  4. Detach the session.

    Press Ctrl+A+D to detach the current screen session.

  5. Resume the session.

    1. List the available screen sessions to find the target session's PID.

      sudo screen -ls
      There is a screen on:
              2046.mytask     (Detached)
      1 Socket in /run/screen/S-root.

      In this example, the session PID is 2046.

    2. Resume the session.

      Replace [PID] with the session PID from the previous step.

      sudo screen -r -d [PID]
      For example, to resume the session with a PID of 2046, run sudo screen -r -d 2046.
  6. Terminate the session.

    Important

    Terminating a session also ends all tasks and commands running within it. Proceed with caution

    Replace [PID] with session's PID.

    sudo kill [PID]