Sending long-running jobs to background and foreground in Linux
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 + Stopped $ bg +
To bring it again in the foreground use fg command. When you send multiple jobs to the background you will get its ids like + or + 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 + 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.
- Ctl + Z to stop jobs
- Use bg command to send the job to background
- Use Jobs to see background running jobs
- fg with number bring the specific job to the foreground
- Use Ctrl + C to kill the job.