Different ways of running a script or tasks in the background in Linux

For one reason or another you might need to run a script in the background in a manner that does not disrupt your work flow. 

Some tasks are usually short and don't need to be send back to the background however long tasks need to be run in the background. 

I personally usually find myself alt+tabbing to the terminal running the script all the time as I continue with my my other tasks. In some situations, I have found myself to have accidentally closed the terminal altogether leaving me cursing.

 Another scenario where you might need to run a task/script in the background is when running tasks/script that takes very long on your VPS, either AWS, GCP, Digitaloceans or whatever provider you use. In most cases you usually SSH to the VPS, if a tasks takes very long it makes zero sense to keep the SSH session alive for hours waiting to the task/script to complete.

In this tutorial am going to demonstrate different ways of running a program in the background.
For each method am going to concentrate on three major parts:

  • Command to run script in the background
  • Command to bring script back to shell 
  • Command to send back script to background

Method 1 : Using FGBG and CLTR+Z

Run the command normally
i.e.
ping google.com
To take the program to the background hold CLTR+Z

To list jobs in the background run 
jobs -l
To start all program in the background run:
bg
To start a specific job use %
bg  %1
To bring job to the foreground run:

NB: Where 1 is the hierarchical number of the task given.
fg %1


screenshot using bg and fg


Limitations

 Once you logout all background tasks are stopped.

Method 2 : Using nohup, fg, bg and cltr+z.

Why use nohup ?

Nohup stands for "No Hangups". Nohup allows the command running to continue running to completion even if the user who initiated the command logout or disconnect's from the the server.

NB: Assuming we have a script named task.sh

We use nohup like below.

nohup ./task.sh

However this create an undesired nohup.out which appends what was to be outputted on the terminal.
Alternatively you could specify which file nohup to use to append logs.
nohup ./task.sh > log.txt
I personally prefer not logging anything you can achieve that by redirecting the output to /dev/null .
nohup task.sh >/dev/null 2>&1 
NB: The task's started above still hang on the terminal in order to send them to the background use ctrl+z,  fg and bg as I had shown in method 1.

Using nohup

If you prefer sending the program immediately to the background append the & symbol at the end this will inform the system to send the program to the background. You wont need to use ctrl+z and bg to start the program in background.

i.e.
nohup task.sh >/dev/null 2>&1 &

Screenshot nohup with &

Limitation

Once you logout or disconnect from the server you can no longer bring the command back to the foreground using fg neither can you list the running jobs using jobs -l
The good news is the job continues running even after logging out and re-logging.

Listing nohup running jobs after logout/disconnected from server.

You can use the following command to list nohup running jobs 
ps xw

screenshot using ps xw

Reason why we can not use fg and bg even if we have the pid of the running task.

You might be wondering if we have the PID of the running task why cant we just re-hook it to the foreground. This is because you cannot use fg and bg with a pid. They are shell built-in-s which require a jobspec, not a pid.

Pausing and Continuing a task using PID

 The fact that we can acquire the pid of running tasks does not leave us helpless. With the pid we can pause and resume the running command as we desire.

Pause running task using PID

kill -STOP <pid>

Resume running task using PID

kill -CONT <pid>

 

Screenshot ps xw

Method 3 : Using screen

This is one of my favorite ways for running tasks on the background. Screen lets you reattach running task to terminal once you log out which is the major limitation for nohup as I had mentioned above. Screen is a terminal multiplexer. Tasks continue running even if the user is disconnected or logout from the system.

Using screen

In order to use screen you need to install it. It does not come installed by default.
sudo apt install screen
To run a task in detached mode:
screen -dmS <name> <command>

To list running jobs:

screen -ls

To re-attach command to screen:

screen -r <name>

To detach the screen without exiting the running task:

Hold cltr+a then press d

Screenshot using screen