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 –

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

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 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 –

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.

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 –

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.

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

Killing a Job

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

For example –

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.