Skip to content

Managing Jobs in Linux

In Linux, a job refers to a process that the shell manages. Linux provides tools to manage foreground and background jobs, monitor their status, and control their execution. The job management system is particularly useful when running multiple processes in a shell environment.


Foreground and Background Jobs

  • Foreground Job: A process that takes control of the terminal. You cannot interact with the shell until the process completes.
  • Background Job: A process that runs in the background, allowing you to continue interacting with the shell.

Key Commands for Job Management

1. Running a Process in the Background

To run a command in the background, append an & symbol:

command &

  • Example:

sleep 60 &

  • Runs the sleep command in the background for 60 seconds.

2. Viewing Active Jobs

To see all jobs managed by the shell, use the jobs command:

jobs

  • Example Output:

 [1]+  Running    sleep 60 &

[2]-  Running    ping google.com &

  • [1]+ and [2]- are job numbers.
  • Running indicates the job’s status.

3. Bringing a Background Job to the Foreground

To bring a background job to the foreground, use the fg command:

fg [job_number]

  • Example:

fg 1

  • Brings job [1] to the foreground.

4. Sending a Foreground Job to the Background

To move a foreground job to the background:

  1. Suspend the job with Ctrl+Z.
  2. Resume it in the background using the bg command:

bg [job_number]

  • Example:

sleep 100

  • Press Ctrl+Z:

 [1]+  Stopped    sleep 100

  • Resume in the background:

bg 1


5. Stopping a Job

To terminate a foreground job, press Ctrl+C.

To terminate a background job:

  1. Identify the job number with jobs.
  2. Kill the job using the kill command:

kill %job_number

  • Example:

kill %1


Job States

Jobs can have the following states:

  1. Running: Actively executing.
  2. Stopped: Suspended with Ctrl+Z.
  3. Terminated: Finished or killed.

Using ps and kill with Jobs

1. Listing Processes (ps)

To view detailed information about processes, use ps:

ps aux

  • Example:

ps aux | grep “sleep”

2. Killing Processes by PID

To kill a process by its Process ID (PID):

kill PID

  • Example:

kill 12345


Persistent Jobs: nohup and disown

1. Using nohup

nohup allows a process to continue running even after the shell is closed:

nohup command &

  • Example:

nohup python script.py &

2. Using disown

disown removes a job from the shell’s job table, making it independent:

disown %job_number

  • Example:

disown %1


Job Prioritization: nice and renice

1. nice

Set the priority of a job at the time of starting:

nice -n priority command

  • Example:

nice -n 10 tar -czf backup.tar.gz /home

2. renice

Change the priority of a running process:

renice priority -p PID

  • Example:

renice 15 -p 12345


Monitoring Jobs and Processes

1. Using top

The top command provides a real-time view of system processes:

top

2. Using htop

An enhanced version of top with better visualization:

htop


Examples of Job Management

  1. Run a Long Task in the Background:

tar -czf backup.tar.gz /home &

  • Suspend and Resume a Job:

find / -name “*.conf”

  • Press Ctrl+Z.
    • Resume in the background:

bg

  • Kill a Stuck Job:

jobs

kill %1

  • Ensure Job Runs After Logout:

nohup wget http://example.com/largefile.zip &


Conclusion

Linux job management provides a robust framework to control processes efficiently. With commands like jobs, fg, bg, kill, and tools like nohup and disown, you can manage processes in both interactive and automated environments. Mastering these techniques is essential for effective system administration and multitasking in Linux.