In this post we will learn how to send a long running jobs to background and foreground.
When you run any command on the Linux terminal it executes immediately but what if it takes long time ? X 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 its a server then you may need that same terminal.
So lets see how to free your terminal and send the job to background.
You can send the jobs background if its already started, for that we use bf, fg and jobs commands.
If you want to send job to background when its starting then you use the ‘&’ in the end of command.
$ node export.js "https://meetngurl.com" video1.webm 0 true &
Above job gets a video from the url and then converts into mp4, ths process takes lot of time, so better we start this process in the background itself.
Lets say you already started the job and then you want to send it to background. To do ths you need to press Ctl + Z this will stop the process, then you use 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 foreground use fg command. When you send multliple jobs to background you will get its ids like [2]+ or [3]+ to get specfc job to foreground you can use its jobs number wth fg as shown below
$ fg 2
To get the list of all background jobs you can use jobs command as shown below
$ jobs
[1]+ Running node export.js
Here only one job is running in the background.
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.