Unix Job Control Commands – bg, fg, Ctrl+Z,jobs


Since Hadoop jobs are often long running, its difficult for newbies to manage the processes in Unix unless they know some useful Unix commands to do so, so that they can increase their efficiency.

In this post, I will explain some of the commands that are very useful while executing some long running jobs .We will see how to execute a job in background, bring it back to foreground, stopping the execution and starting it back and kill a job.

Executing a Job in background

Just append & at the end of any command to put the execution of that command in background.

For example –

$ hadoop jar examples.jar param1 param2 &

We can see all the jobs running in background by executing jobs command.

$jobs
[1] Running      hadoop jar examples.jar param1 param2 &

Jobs Command

When we run jobs command it gives a list of all the jobs with their status. The general syntax of output is –

[job_id] +/- <status> command

job_id is id of the current job.

status represents the status of the job and can be one of RUNNING, STOPPED, EXIT, and DONE.

The character ‘+’ represents the job which will be used as default in bg and fg commands.

The character ‘-‘ represents the job which would become default when current default job exits.

For example –

[1]   Running                 tar -zxvf file.tar.gz ../path/ &
[2]-  Running                 tar -zxvf file2.tar.gz ../path2 &
[3]+  Running                 tar -zxvf file3.tar.gz ../path3/ &

fg command

Also, we can bring the job in foreground with fg command. When executed without any argument, it will bring most recent background job in foreground.

$fg

Now, if job is running in foreground and you want to stop the execution of job, without killing it, press Ctrl-Z on keyboard and you will see an output like this –

[1]   STOPPED                 tar -zxvf file.tar.gz ../path/ &

bg command

Now, current status of job is STOPPED, we can again start the job in background using it’s job_id using bg command.

$ bg %1

And , you can again see job running in background using jobs.

$jobs
[1]   Running                 tar -zxvf file.tar.gz ../path/ &

Killing a Job

We can kill a running job using kill command with it’s job_id.

For example –

$ kill %1
[1]   Exit                tar -zxvf file.tar.gz ../path/ &

That’s it !! In the upcoming posts , we will see about how we can execute job in background even when we are logged out of system using nohup command.

Interesting Reads –

Multithreaded Mappers in MapReduce


You may also like...

%d bloggers like this: