Day 14: Manage jobs in GNU-Linux
Multitasking. A frivolous concept.
Companies are looking for people who can multitask.
Do you realise what it means?
Multitasking is the concurrent execution of multiple tasks.
Is it possible for a human ?
No, it looks cool on the CV.
But at any time T, you are always doing 1 (one) task at a time.
That’s not multitasking sorry.
We are only task switching.
In the world of servers
Here, there is a real need to do several tasks.
Here’s a script
[email protected]:~/Projects/tests$ cat job1.sh #!/bin/bash sleep 150
It does a simple thing, it sleeps for 150 seconds.
If we launch it it will last for at least 150 seconds.
Which is what we want to demonstrate execution.
This is a foreground execution
Now we know that while this script is running, for 150 seconds this terminal cannot be used.
Except if we were to send a SIGINT (Ctrl + C) or a SIGSTOP (CTRL + Z).
More jobs !!! Moar
I created 4 job scripts with increasing sleep time, i.e.execution time.
Let’s execute all of them
The & means background execution. Notice that they have a job id (1,2,3,4) and the 4-digit number is a PID.
the jobs command
[email protected]:~/Projects/tests$ jobs  Running ./job1.sh &  Running ./job2.sh & - Running ./job3.sh & + Running ./job4.sh &
Using this command, I am able to see their running status
[email protected]:~/Projects/tests$ jobs  Done ./job1.sh  Running ./job2.sh & - Running ./job3.sh & + Running ./job4.sh &
After some time, it’s marked done. The done task will disappear on the next jobs command launch.
The plus sign tells us that this job will be the default command to go to foreground if i were to use the foreground command.
The minus sign tells us this job will be the default to go to foreground.
Only after the first one (+) finishes.
Just telling us this one is next.
fg and bg
The fg (foreground) and bg (background) commands go hand in hand.
We can switch to the + task using the fg command
Then suspend its execution using CTRL + Z
Then send it back to background execution using the bg command
foreground specific task
If we want to foreground a specific task, we use the job id.
In this case, we wanted to switch to job 4.
Background specific task
Same for bg
Real world usage
I use this a lot when there is a need to run multiple database queries.
Particularly those that I am outputting to some file.
But this could also be used for scripts that you want to run in background.
/!\ Once you close your terminal session, the current jobs will be exited /!\