Place and Manage Tasks or Jobs in the Background from the Command Line

Posted August 22, 2005 by MickeyMouse in UNIX

Managing tasks or jobs from the command line or terminal is easy in UNIX flavors once you know the simple commands.

UNIX, like other modern operating systems, can easily run multiple tasks at once. If you are running from the command line or terminal, it is not obvious how accomplish this or how to manage multiple tasks.

In UNIX, running tasks are typically called jobs. While running any job, you can stop it by pressing CTRL-Z. Your prompt will once again appear, and you can run more jobs while the original job continues to be active but stopped in the background.

If you want to see what jobs are running, just type jobs. You still see something like the following:

[1]+ Stopped NameofJob

    The number 1 is the job number, “stopped” is the status of the job, and “NameofJob” is the name of the process that you stopped with CTRL-Z

To restart a job in the background, just type bg %jobnumber. In our case, we would type bg %1.

To restart a job in the foreground, just type fg %jobnumber. In our case, we would type fg %1.

The command jobs -pl will give you a list of the jobs plus the process ID:

[1]+ 26427 Stopped NameofJob

    The number 26427 is the process ID in this example.

Knowing the process id is helpful because you can use it to completely stop a job. The format of this command is as follows:

The command kill -15 processid will attempt to shutdown the job safely.
The command kill -9 processid is used more frequently and simply aborts the job.

Therefore, to stop NameofJob with the process ID of 26427, I would type the following:

    kill -9 26427

The Conversation

Follow the reactions below and share your own thoughts.