Expert Tips: Efficiently Handling Background Tasks in Linux Systems

Expert Tips: Efficiently Handling Background Tasks in Linux Systems

Edward Lv13

Expert Tips: Efficiently Handling Background Tasks in Linux Systems

Use the Bash shell in Linux to manage foreground and background processes. You can use Bash’s job control functions and signals to give you more flexibility in how you run commands. We show you how.

All About Processes

Whenever a program is executed in a Linux or Unix-like operating system, a process is started. “Process” is the name for the internal representation of the executing program in the computer’s memory. There is a process for every active program. In fact, there is a process for nearly everything that is running on your computer. That includes the components of your graphical desktop environment (GDE) such as GNOME or KDE , and system daemons that are launched at start-up.

Why nearly everything that is running? Well, Bash built-ins such as cd , pwd , and alias do not need to have a process launched (or “spawned”) when they are run. Bash executes these commands within the instance of the Bash shell that is running in your terminal window. These commands are fast precisely because they don’t need to have a process launched for them to execute. (You can type help in a terminal window to see the list of Bash built-ins.)

Processes can be running in the foreground, in which case they take over your terminal until they have completed, or they can be run in the background. Processes that run in the background don’t dominate the terminal window and you can continue to work in it. Or at least, they don’t dominate the terminal window if they don’t generate screen output.

A Messy Example

We’ll start a simple ping trace running . We’re going to ping the How-To Geek domain. This will execute as a foreground process.

ping www.howtogeek.com

ping www.howtogeek.com in a terminal window

We get the expected results, scrolling down the terminal window. We can’t do anything else in the terminal window while ping is running. To terminate the command hit Ctrl+C.

Ctrl+C

ping trace output in a terminal window

The visible effect of the Ctrl+C is highlighted in the screenshot. ping gives a short summary and then stops.

Let’s repeat that. But this time we’ll hit Ctrl+Z instead of Ctrl+C. The task won’t be terminated. It will become a background task. We get control of the terminal window returned to us.

ping www.howtogeek.com

Ctrl+Z

effect of Ctrl+Z on a command running in a terminal window

The visible effect of hitting Ctrl+Z is highlighted in the screenshot.

This time we are told the process is stopped. Stopped doesn’t mean terminated. It’s like a car at a stop sign. We haven’t scrapped it and thrown it away. It’s still on the road, stationary, waiting to go. The process is now a background job.

The jobs command will list the jobs that have been started in the current terminal session. And because jobs are (inevitably) processes, we can also use the ps command to see them. Let’s use both commands and compare their outputs. We’ll use the T option (terminal) option to only list the processes that are running in this terminal window. Note that there is no need to use a hyphen - with the T option.

jobs

ps T

jobs command in a terminal window

Nero Burning ROM:
The ultimate burning program for all your needs!

The jobs command tells us:

  • [1]: The number in square brackets is the job number. We can use this to refer to the job when we need to control it with job control commands.
  • +: The plus sign + shows that this is the job that will be acted upon if we use a job control command without a specific job number. It is called the default job. The default job is always the one most recently added to the list of jobs.
  • Stopped: The process is not running.
  • ping www.howtogeek.com : The command line that launched the process.

The ps command tells us:

  • PID: The process ID of the process. Each process has a unique ID.
  • TTY: The pseudo-teletype (terminal window) that the process was executed from.
  • STAT: The status of the process.
  • TIME: The amount of CPU time consumed by the process.
  • COMMAND: The command that launched the process.

These are common values for the STAT column:

  • D: Uninterruptible sleep. The process is in a waiting state, usually waiting for input or output, and cannot be interrupted.
  • I: Idle.
  • R: Running.
  • S: Interruptible sleep.
  • T: Stopped by a job control signal.
  • Z: A zombie process. The process has been terminated but hasn’t been “cleaned down” by its parent process.

The value in the STAT column can be followed by one of these extra indicators:

  • <: High-priority task (not nice to other processes).
  • N: Low-priority (nice to other processes).
  • L: process has pages locked into memory (typically used by real-time processes).
  • s: A session leader. A session leader is a process that has launched process groups. A shell is a session leader.
  • l: Multi-thread process.
  • +: A foreground process.

We can see that Bash has a state of Ss. The uppercase “S” tell us the Bash shell is sleeping, and it is interruptible. As soon as we need it, it will respond. The lowercase “s” tells us that the shell is a session leader.

The ping command has a state of T. This tells us that ping has been stopped by a job control signal. In this example, that was the Ctrl+Z we used to put it into the background.

The ps T command has a state of R, which stands for running. The + indicates that this process is a member of the foreground group. So the ps T command is running in the foreground.

The bg Command

The bg command is used to resume a background process. It can be used with or without a job number. If you use it without a job number the default job is brought to the foreground. The process still runs in the background. You cannot send any input to it.

If we issue the bg command, we will resume our ping command:

bg

bg in a terminal window

The ping command resumes and we see the scrolling output in the terminal window once more. The name of the command that has been restarted is displayed for you. This is highlighted in the screenshot.

resumed ping background process with output in a terminal widow

But we have a problem. The task is running in the background and won’t accept input. So how do we stop it? Ctrl+C doesn’t do anything. We can see it when we type it but the background task doesn’t receive those keystrokes so it keeps pinging merrily away.

Background task ignoring Ctrl+C in a terminal window

In fact, we’re now in a strange blended mode. We can type in the terminal window but what we type is quickly swept away by the scrolling output from the ping command. Anything we type takes effect in the foregound.

To stop our background task we need to bring it to the foreground and then stop it.

The fg Command

The fg command will bring a background task into the foreground. Just like the bg command, it can be used with or without a job number. Using it with a job number means it will operate on a specific job. If it is used without a job number the last command that was sent to the background is used.

If we type fg our ping command will be brought to the foreground. The characters we type are mixed up with the output from the ping command, but they are operated on by the shell as if they had been entered on the command line as usual. And in fact, from the Bash shell’s point of view, that is exactly what has happened.

fg

fg command mixed in with the output from ping in a terminal window

And now that we have the ping command running in the foreground once more, we can use Ctrl+C to kill it.

Ctrl+C

Ctrl+C stopping the ping command in a terminal window

We Need to Send the Right Signals

That wasn’t exactly pretty. Evidently running a process in the background works best when the process doesn’t produce output and doesn’t require input.

But, messy or not, our example did accomplish:

  • Putting a process into the background.
  • Restoring the process to a running state in the background.
  • Returning the process to the foreground.
  • Terminating the process.

When you use Ctrl+C and Ctrl+Z , you are sending signals to the process. These are shorthand ways of using the kill command. There are 64 different signals that kill can send. Use kill -l at the command line to list them. kill isn’t the only source of these signals. Some of them are raised automatically by other processes within the system

Here are some of the commonly used ones.

  • SIGHUP: Signal 1. Automatically sent to a process when the terminal it is running in is closed.
  • SIGINT: Signal 2. Sent to a process you hit Ctrl+C. The process is interrupted and told to terminate.
  • SIGQUIT: Signal 3. Sent to a process if the user sends a quit signal Ctrl+D.
  • SIGKILL: Signal 9. The process is immediately killed and will not attempt to close down cleanly. The process does not go down gracefully.
  • SIGTERM: Signal 15. This is the default signal sent by kill. It is the standard program termination signal.
  • SIGTSTP: Signal 20. Sent to a process when you use Ctrl+Z. It stops the process and puts it in the background.

We must use the kill command to issue signals that do not have key combinations assigned to them.

Further Job Control

A process moved into the background by using Ctrl+Z is placed in the stopped state. We have to use the bg command to start it running again. To launch a program as a running background process is simple. Append an ampersand & to the end of the command line.

Although it is best that background processes do not write to the terminal window, we’re going to use examples that do. We need to have something in the screenshots that we can refer to. This command will start an endless loop as a background process:

while true; do echo “How-To Geek Loop Process”; sleep 3; done &

while true; do echo "How-To Geek Loop Process"; sleep 3; done & in a terminal window

We are told the job number and process ID id of the process. Our job number is 1, and the process id is 1979. We can use these identifiers to control the process.

The output from our endless loop starts to appear in the terminal window. As before, we can use the command line but any commands we issue are interspersed with the output from the loop process.

ls

output of the background loop process interspersed with output from other commands

To stop our process we can use jobs to remind ourselves what the job number is, and then use kill.

jobs reports that our process is job number 1. To use that number with kill we must precede it with a percent sign %.

jobs

kill %1

jobs and kill %1 in a terminal window

kill sends the SIGTERM signal, signal number 15, to the process and it is terminated. When the Enter key is next pressed, a status of the job is shown. It lists the process as “terminated.” If the process does not respond to the kill command you can take it up a notch. Use kill with SIGKILL , signal number 9. Just put the number 9 between the kill command the job number.

kill 9 %1

Things We’ve Covered

  • Ctrl+C: Sends SIGINT, signal 2, to the process—if it is accepting input—and tells it to terminate.
  • Ctrl+D: Sends SISQUIT, signal 3, to the process—if it is accepting input—and tells it to quit.
  • Ctrl+Z: Sends SIGSTP, signal 20, to the process and tells it to stop (suspend) and become a background process.
  • jobs: Lists the background jobs and shows their job number.
  • bg job_number: Restarts a background process. If you don’t provide a job number the last process that was turned into a background task is used.
  • fg job_number: brings a background process into the foreground and restarts it. If you don’t provide a job number the last process that was turned into a background task is used.
  • commandline &: Adding an ampersand & to the end of a command line executes that command as a background task, that is running.
  • kill %job_number: Sends SIGTERM, signal 15, to the process to terminate it.
  • kill 9 %job_number: Sends SIGKILL, signal 9, to the process and terminates it abruptly.

| | Linux Commands | | |
| —————– | ——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————– | |
| Files | tar · pv · cat · tac · chmod · grep · diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr | |
| Processes | alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap | |
| Networking | netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw · arping · firewalld | |

  • Title: Expert Tips: Efficiently Handling Background Tasks in Linux Systems
  • Author: Edward
  • Created at : 2024-08-31 08:54:26
  • Updated at : 2024-09-01 08:54:26
  • Link: https://vp-tips.techidaily.com/expert-tips-efficiently-handling-background-tasks-in-linux-systems/
  • License: This work is licensed under CC BY-NC-SA 4.0.