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:

    
    [root@server /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

    
    [root@server /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.

    
    [root@server /root]# jobs
    [1]+  Stopped                 less /etc/lilo.conf
    [root@server /root]#
    

  • Resuming a Stopped Job [%1]

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

    
    [root@server /root]# %1
    

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

    
    [root@server /root]# fg %1
    

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

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

    
    [root@server /root]# %1 &
    

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

    
    [root@server /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:

    
    [root@server /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.

    
    [root@server /root]# xcalc
    <CTRL+z>
    [1]+  Stopped                 xcalc
    [root@server /root]# jobs
    [1]+  Stopped                 xcalc
    [root@server /root]# %1 &
    [1]+ xcalc &
    [root@server /root]# jobs
    [1]+  Running                 xcalc &
    [root@server /root]#
    [root@server /root]#
    [root@server /root]#
    [root@server /root]#
    [root@server /root]#
    [root@server /root]# kill %1
    [root@server /root]# <ENTER>
    [1]+  Terminated              xcalc
    [root@server /root]# jobs
    [root@server /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.

    
    [root@server /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.

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

Other Good Reading:

I/O Redirection in Linux

Leave a Comment