Controlling Jobs in Linux

  • Starting a Job in the Background [cmd &]

  • Attaching an ampersand to the end of a command will cause that command to run in the background. For example:

    [[email protected] /root]# xcalc &
    

    This command causes the xcalculator to run, also it frees up the bash prompt so you can perform other tasks.

    You can also use this method when starting X-Windows

    [[email protected] /root]# startx &
    

    This will start X-Windows, but it will also free up that console that was used to start X-Windows.

  • Stopping (Pausing) a Job [CTRL+z]

  • If you are running a program, such as less or vi, you can temporarily stop (or pause) that job. You could then, for example, use another program and then come back to this stopped job.

    Press CTRL+z to stop a job.

  • Listing Jobs [jobs]

  • You may list jobs and their status by running the “jobs” command.

    [[email protected] /root]# jobs
    [1]+  Stopped                 less /etc/lilo.conf
    [[email protected] /root]# 
    

  • Resuming a Stopped Job [%1]

  • You may resume a stopped job by typing %jobnumber as follows:

    [[email protected] /root]# %1
    

    An alternate method to resuming a stopped job is to use the command fg as follows:

    [[email protected] /root]# fg %1
    

  • Placing a Job in the Background [%1 &]

  • You may place a stopped job in the background by typing %jobnumber & as follows:

    [[email protected] /root]# %1 &
    

    An alternate method to run a stopped job in the background is to use the command fg as follows:

    [[email protected] /root]# bg %1
    

    Note: Some jobs don’t run well in the background, these are jobs that need constant input. As soon as the job requires input it will go to a stopped status.

  • Killing a stopped Job [kill %1]

  • You may kill a stopped job by typing kill %jobnumber as follows:

    [[email protected] /root]# kill %1
    

  • Job Examples

  • In the following example we start xcalc, we then realize we need that particular xterm console again. In the console we press CTRL+z to stop the job. We then type “%1 &” to start it running again in the background. We also type the “jobs” command a couple of times along the way so you can get the feel of how job status works. We then decide we no longer want xcalculater running and so we kill it with the job kill command.

    [[email protected] /root]# xcalc
    <CTRL+z>
    [1]+  Stopped                 xcalc
    [[email protected] /root]# jobs
    [1]+  Stopped                 xcalc
    [[email protected] /root]# %1 &
    [1]+ xcalc &
    [[email protected] /root]# jobs
    [1]+  Running                 xcalc &
    [[email protected] /root]# 
    [[email protected] /root]# 
    [[email protected] /root]# 
    [[email protected] /root]# 
    [[email protected] /root]# 
    [[email protected] /root]# kill %1
    [[email protected] /root]# <ENTER>
    [1]+  Terminated              xcalc
    [[email protected] /root]# jobs
    [[email protected] /root]# 
    
    

  • The nohup Utility [nohup cmd &]

  • The nohup utility will run as a process that is detached from your console. If you close your console, the nohup process will keep running in the background.

    The following command will run “myjob.sh” in the background at a slightly lower priority than normal. This job will continue to run even after I log out. Output from this process will be sent to nohup.out by default.

    [[email protected] /root]# nohup myjob.sh &
    

  • Capturing Output [script]

  • The “script” command will put you into a new shell and it will log all the output for you into the file called “typescript”. When you are done collecting data, you may type “exit” to get out of this script shell.

    [[email protected] /root]# script
    Script started, file is typescript
    [[email protected] /root]# 
    [[email protected] /root]# 
    [[email protected] /root]# (Generate output here...)
    [[email protected] /root]# 
    [[email protected] /root]# 
    [[email protected] /root]# exit
    exit
    Script done, file is typescript
    [[email protected] /root]# 
    

Other Good Reading:

I/O Redirection in Linux

Published
Categorized as How To

By shibaa987

Linux kernel developer and a firmware developer with an experience of 10+ years.

Leave a comment

Your email address will not be published. Required fields are marked *