10 Linux/Unix Bash and KSH Shell Job Control Examples

[donotprint][/donotprint]Linux and Unix are multitasking operating systems i.e. a system that can run multiple tasks (process) during the same period of time. In this new blog series, I am going to list the Linux and Unix job control commands that you can use for multitasking with the Bash or Korn or POSIX shell.

Adblock detected 😱

My website is made possible by displaying online advertisements to my visitors. I get it! Ads are annoying but they help keep this website running. It is hard to keep the site running and producing new content when so many people block ads. Please consider donating money to the nixCraft via PayPal/Bitcoin, or become a supporter using Patreon.

What is a job control?

Job control is nothing but the ability to stop/suspend the execution of processes (commands) and continue/resume their execution as per your requirements. This is done using your operating system and shell such as bash/ksh or POSIX shell.

Who provides a facility to control jobs?

The Bash / Korn shell, or POSIX shell provides a facility to control jobs.

Say hello to job table

Your shell keeps a table of current jobs, called job table. When you type command the shell assigns a jobID (also known as JOB_SPEC). A jobID or JOB_SPEC is nothing but small integer numbers.

#1: Creating your first Linux/Unix job

I am going to run a command called xeyes that displays two googly eyes on screen, enter:$ xeyes & Sample outputs:Fig.01: Running the xeyes command in the background I started a job in the background with an ampersand (&). The shell prints a line that looks like the following:

[1] 6891

In this example, two numbers are output as follows

[1] : The xeyes job, which was started in the background, was job number 1.

jobID or JOB_SPEC – Job number to use with the fg, bg, wait, kill and other shell commands. You must prefix the job number with a percent sign (%).A plus sign (+) identifies the default or current job.A minus sign (-) identifies the previous job.

%1fg %1kill %2

2

9379

Process ID – An identification unique number that is automatically assigned to each process when it is created on the system.

kill 9379

3

Running

state – The state of job:Running – The job is currently running and has not been suspended by a signal.Stopped – The job was suspended.

N/A

4

xeyes &

command – The command that was given to the shell.

script &firefox url&

You can also use ps command to list the processes running on the system:$ ps

#5: Resume suspended/stopped job in the background

In this example, I am going to update all installed packages on Red Hat or CentOS Linux production server using yum command background job:# yum -y update &>/root/patch.log & However, due to some reason (say load issue) I decided to stop this job for 20 minutes:# kill -s stop %yum Sample outputs:

A note about shell built-in and external commands

Run the following type command to find out whether given command is internal or external:

type-afgbgjobsdisown

type -a fg bg jobs disown

Sample outputs:

fg is a shell builtin
fg is /usr/bin/fg
bg is a shell builtin
bg is /usr/bin/bg
jobs is a shell builtin
jobs is /usr/bin/jobs
disown is a shell builtin

In almost all cases, you need to use shell built-in commands. All external commands such as /usr/bin/fg or /usr/bin/jobs works in a different shell environment, and can not use parent shell's environment.

Conclusion

I hope you enjoyed this blog post series (rss feed) and I suggest that you read the following for more information:

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Your support makes a big difference:

I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft takes a lot of my time and hard work to produce. If everyone who reads nixCraft, who likes it, helps fund it, my future would be more secure. You can donate as little as $1 to support nixCraft: