Unix Job Control Commands: bg
, fg
, Ctrl+Z
, jobs
A practical guide for developers and data engineers to manage long-running jobs in Unix, especially useful when working with Hadoop or other big data tools. —
Table of Contents
Introduction
When running long Hadoop or other data processing jobs on Unix, it’s important to know how to manage processes efficiently. This guide covers essential Unix job control commands to help you:
- Run jobs in the background
- Bring jobs to the foreground
- Pause and resume jobs
- List and kill jobs
Running a Job in the Background
To run any command in the background, append &
at the end:
hadoop jar examples.jar param1 param2 &
This allows you to continue using the terminal while the job runs.
Listing Background Jobs: jobs
Command
To see all jobs running in the background:
jobs
Example output:
[1] Running hadoop jar examples.jar param1 param2 &
[1]
is the job IDRunning
is the status- The command is shown at the end
The output format is:
[job_id] +/- <status> command
+
marks the default job forfg
/bg
-
marks the next default if the current one exits
Example with multiple jobs:
[1] Running tar -zxvf file.tar.gz ../path/ &
[2]- Running tar -zxvf file2.tar.gz ../path2 &
[3]+ Running tar -zxvf file3.tar.gz ../path3/ &
Bringing a Job to Foreground: fg
Command
To bring the most recent background job to the foreground:
fg
To bring a specific job (e.g., job 1):
fg %1
Stopping a Foreground Job: Ctrl+Z
If a job is running in the foreground and you want to pause (stop) it without killing it, press:
Ctrl+Z
You’ll see output like:
[1] STOPPED tar -zxvf file.tar.gz ../path/ &
Resuming a Stopped Job in Background: bg
Command
To resume a stopped job in the background (e.g., job 1):
bg %1
Check status again:
jobs
[1] Running tar -zxvf file.tar.gz ../path/ &
Killing a Job: kill
Command
To kill a running or stopped job (e.g., job 1):
kill %1
Example output:
[1] Exit tar -zxvf file.tar.gz ../path/ &
Summary Table
Command | Description |
---|---|
jobs |
List all background jobs |
fg [%job_id] |
Bring job to foreground |
bg [%job_id] |
Resume stopped job in background |
kill [%job_id] |
Kill a job |
Ctrl+Z |
Pause (stop) a foreground job |
& |
Run a command in the background |