Can we manage multiple background jobs?

QuestionsQuestions8 SkillsProDec, 15 2025
0113

Yes, absolutely! You can manage multiple background jobs in a bash script or directly from your terminal.

While $! specifically gives you the PID (Process ID) of the last command run in the background, there are other commands and concepts for managing multiple background jobs:

  • jobs command: This command lists all active background jobs in the current shell session. It will show you their job numbers, status, and the commands they are running.
  • Job Control:
    • You can put a running foreground job into the background by pressing Ctrl+Z (which sends a SIGSTOP signal) and then typing bg (background).
    • You can bring a background job to the foreground using the fg command (e.g., fg %1 to bring job number 1 to the foreground).
    • You can send signals to background jobs using their job number or PID (e.g., kill %1 or kill <pid>).

Let's say you started a few background jobs:

sleep 10 &
sleep 20 &
sleep 5 &

If you then type jobs, you would see something like:

[1]   Running                 sleep 10 &
[2]   Running                 sleep 20 &
[3]-  Running                 sleep 5 &

Each job gets a job number (like [1], [2], [3]). You can then refer to them by %{job_number}.

This is a really powerful feature of the shell for managing concurrent tasks!

Do you want to explore an example of managing multiple background jobs?

0 Comments

no data
Be the first to share your comment!