Different ways of running scripts 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...

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

Some tasks are usually short and don’t need to be sent 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 long script all the time as I continue with my my other tasks. In some situations, I have found myself accidentally closing the terminal altogether leaving me cursing.

Another scenario where you might need to run a task/script in the background is when running tasks/scripts that take 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 task takes very long it makes zero sense to keep the SSH session alive for hours waiting for 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 the script in the background
  • Command to bring script back to shell
  • Command to send back script to background

Method 1: Using FG, BG 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 programs in the background run:

bg

To start a specific job use %

bg %1

To bring the job to the foreground run the below command:

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

Limitations

Once you log out 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 to continue running to completion even if the user who initiated the command logs out or disconnects from the server.

NB: Assuming we have a script named task.sh

We use nohup like below.

nohup ./task.sh

However, this creates an undesired nohup.out file that 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 prefer not logging anything you can achieve that by redirecting the output to /dev/null .

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

NB: The tasks started above still hang on the terminal to send them to the background use ctrl+z, fg and bg as I had shown in method 1.

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 won't need to use ctrl+z and bg to start the program in the background.

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

Limitation

Once you log out or disconnect from the server you can no longer bring the command back to the foreground using fg nor 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

The 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 can't 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>

Method 3: Using screen

This is one of my favorite ways of running tasks in the background. Screen lets you reattach the running task to the 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

To list running jobs:

screen -ls

To run a task in detached mode:

screen -dmS <name> <command>

To re-attach the command to the screen:

screen -r <name>

To detach the screen without exiting the running task:

Hold cltr+a then press d

Subscribe to Bluedoa Digest

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
alexander@example.com
Subscribe