WCC logo

CIS120Linux Fundementals

jobs, fg and bg Commands

In Linux, managing background and foreground processes is essential for multitasking and efficient workflow. The jobs, fg, and bg commands allow users to control and manipulate these processes. Additionally, keyboard shortcuts like Ctrl-C and Ctrl-Z provide quick ways to interact with running processes. Understanding these commands and shortcuts can greatly enhance your command-line productivity.

Creating and Running a Background Script

Consider the following script that repeatedly outputs a message every 15 seconds:

echo 'echo "I am running"; while sleep 15; do echo "I am running"; done' > myscript.sh
chmod +x myscript.sh

This script prints "I am running" immediately and then every 15 seconds. The chmod +x myscript.sh command makes the script executable. NOTE: You just do the chmod +x myscript.sh or chmod 700 myscript.sh

To run this script in the background, use the following command:

./myscript.sh &

This command runs the myscript.sh script in the background, allowing you to continue using the terminal. Alternatively, you can run the script using:

bash myscript.sh &

The jobs Command

The jobs command lists all background jobs started in the current shell session. Each job is assigned a job ID, which can be used to manipulate the job with other commands.

Example:

jobs

Output:

[1]+  Running ./myscript.sh &

In this example, job 1 is running in the background.

The fg Command

The fg (foreground) command is used to bring a background job to the foreground. You can specify the job ID to bring a specific job to the foreground.

Example:

fg %1

This command brings job 1 (the myscript.sh script) to the foreground.

If you omit the job ID, fg brings the most recently stopped or background job to the foreground:

fg

The bg Command

The bg (background) command resumes a stopped job in the background. Like fg, you can specify the job ID to resume a specific job.

Example:

bg %1

This command resumes job 1 (the myscript.sh script) in the background.

If you omit the job ID, bg resumes the most recently stopped job:

bg

Keyboard Shortcuts: Ctrl-C and Ctrl-Z

Ctrl-C: The Ctrl-C keyboard shortcut sends the SIGINT (interrupt) signal to the foreground process, terminating it immediately. This is useful for stopping processes that are taking too long or behaving unexpectedly.

Example:

./myscript.sh

Press Ctrl-C to stop the myscript.sh script.

Ctrl-Z: The Ctrl-Z keyboard shortcut sends the SIGTSTP (terminal stop) signal to the foreground process, suspending it and putting it in the background as a stopped job. This allows you to temporarily halt a process and resume it later with fg or bg.

Example:

./myscript.sh

Press Ctrl-Z to suspend the myscript.sh script.

Examples Using the Script

To run the script in the background:

./myscript.sh &

This runs the myscript.sh script in the background.

To list all background jobs:

jobs

Output:

[1]+  Running                 ./myscript.sh &

To bring the background job to the foreground:

fg %1

This brings job 1 to the foreground.

To suspend the foreground script:

./myscript.sh

Press Ctrl-Z to suspend the myscript.sh script.

To resume the stopped job in the background:

bg %1

This resumes job 1 in the background.

To terminate the foreground script:

./myscript.sh

Press Ctrl-C to stop the myscript.sh script.

Summary

The jobs, fg, and bg commands, along with keyboard shortcuts Ctrl-C and Ctrl-Z, provide powerful tools for managing processes in Linux. These commands allow you to run, suspend, resume, and terminate processes, enhancing your ability to multitask and control your workflow efficiently. By mastering these commands and shortcuts, you can become more productive and effective in the Linux command-line environment.