Open In App

How to Prevent a Background Process From Being Stopped after Closing SSH client in Linux

Last Updated : 09 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

What is Background Process?

A Background Process is an ongoing process running independently, and that is not visible to the users as it runs without any user interaction after the terminal has been closed. Some of the well-known examples of Background processes are the web servers like Nginx and Apache. The Nginx discovers the file and sends it to the server as the request is for the dynamic files, while Apache gives the response to the Nginx by executing it, which is displayed to the client.

SSH (Secure Shell) client in Linux

It is a client/server application, it delivers secure encrypted communication between the two hosts beyond an insecure network. To manage the systems and applications remotely, allowing them to login into another computer over a secured network, execute file transfers, terminal access, and secure tunnel for other applications.

To start a background process in Linux

Syntax:

$ command &

Adding the ampersand symbol (&) appending at the end of the command, makes the commands run in the background.

We can also check the status of the background process running  and the stopped sessions by using the jobs utility

1. Sleep Command:

The sleep command is used to delay for any fixed amount of time, throughout the execution of the script. also, act as a dummy job. we can set the delay time with suffixes of the second (s), minutes(m), and hours(h), even if it can be set for days(d).

Syntax:

$ sleep number[suffix]

Example:

$ sleep 10000 &
/*Sleep command makes the command to run with defined time.*/

Output:

Output for sleep command

 

Here, we simply use the sleep command for background processes which enables a prolonged execution time by giving the background processes a dummy job. and appending (&) at the end of the command runs it as background processes. As default, the given time of 10000 is taken in seconds(s).

2. Jobs Command:

It will list the processes that are running in the background and with the processes [id] and their status is active or stopped or currently running process and also be used for postponing the process and resuming the processes. 

Syntax:

$ jobs
/*Displays the processes running in the background.*/

Output:

Output for jobs command

By using the jobs command we can clearly see that, it shows the sleep command is running as a background process as we previously used it with the (&) symbol to it.

To make the process run after the SSH client is closed

Suggested approaches:

1. nohup command

The suggested approach is to use the command nohup which represents the “no hang up“. As this command avoids the hangup signal and makes the program run efficiently even after the SSH is closed.

Syntax:

$ nohup command-name &

Example:

$  nohup sleep &

Output:

nohup output

 

In the above output, we can observe that the nohup command is continuing to run the processes even after the terminal gets closed. To view the output type the $ cat nohup.out command.

2. nice command

It allows the process to run at low priority. we use nice value to mention the priority of the process. If we do not specify the increment value it will take the default increment value as 10. the priority of the process is known as the nice value.

Syntax:

$ nohup nice command-name &

Example:

$ nohup nice sleep &

Output:

nice output

 

We are using nice command so that the process is running in low priority as the same as the nohup command stores the output in the ‘nohup.out’ file. and also it continues to run in low priority after the session is closed.

3. GNU Screen 

It allows you to close the screen (virtual terminal) inside the sessions, but the background processes will persist to run even after the screen is closed.

4.  Tmux

It is the latest replacement of the GNU Screen, this has the additional advantage is to create a session and also opening numerous windows inside the session as well as the background processes will persist even after the server is disconnected.

5. Double fork

It will run the background process even though the connection is closed. the command inside the parentheses will continue to execute as the background process without killing the process even though the session is closed.

Syntax:

$ ((command &)&)

Hence, we can use these commands to prevent the background processes from being killed when the session is closed using nohup is suggested as it is dedicated to running the background processes after disconnecting the session. so, these commands are very useful for processes with a long duration.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads