Sending long-running jobs to background and foreground in Linux

Subscribe to my newsletter and never miss my upcoming articles

When you run any command on the Linux terminal it executes immediately but what if it takes a long time?

What you do when it takes few minutes you have to wait till it gets completed.

So often what you do is start another terminal and work on it. If it's a server then you may need that same terminal.

So let's see how to free your terminal and send the job to the background.

You can send the job to the background even if it's already started.

If you want to send the job to the background when it's starting then you use the '&' at the end of the command.

To start a job in the background put & in the end as shown below.

$ node export.js "https://meetngurl.com" video1.webm 0 true &

The above job gets a video from the URL and then converts it into mp4, this process takes a lot of time, so better we start this process in the background itself.

To send already started a job to background

Let's say you already started the job and then you want to send it to the background. To do this you need to press Ctrl + Z this will stop the process, then you use the bg command to send it background.

$ node export.js "https://meet56546url.com" video1.webm 0 true
---->  Ctl + Z
[1]+  Stopped
$ bg
[1]+

To bring it again in the foreground use fg command. When you send multiple jobs to the background you will get its ids like [2]+ or [3]+ to get a specific job to foreground you can use its jobs number with fg as shown below

$ fg 2

List all background jobs

To get the list of all background jobs you can use the jobs command as shown below

$ jobs
[1]+  Running       node export.js

Here only one job is running in the background.

Kill running job

To kill the running job you can simply use the Ctl + C. First, get the command in the foreground and then use Ctrl + C to kill the job.

Summary

  1. Ctl + Z to stop jobs
  2. Use bg command to send the job to background
  3. Use Jobs to see background running jobs
  4. fg with number bring the specific job to the foreground
  5. Use Ctrl + C to kill the job.

No Comments Yet