Process Control Commands in Unix/Linux

Process control commands in Unix are:

bg - put suspended process into background
fg - bring process into foreground
jobs - list processes
  1. bg Command : bg is a process control command that resumes suspended process while keeping them running in the background. User can run a job in the background by adding a “&” symbol at the end of the command.
    Syntax :

    bg [job]
    

    Options

    The character % introduces a job specification. The Job can be a process ID (PID) number, or we can use one of the following symbol combinations:

    %Number  : Use the job number such as %1 or %2.
    %String  : Use the string whose name begins 
                      with suspended command such as %commandNameHere or 
                      %ping.
    %+ OR %% : Refers to the current job.
    %-       : Refers to the previous job.
    

    bg examples

    Command

    bg %1

    Output:

    The stopped job will resume operation, but remain in the background. 
    It will not receive any input from the terminal while it's in the background, 
    but it will keep running.
    
  2. fg Command : fg command moves a background job in the current shell environment into the foreground. Use the job ID parameter to indicate a specific job to be run in the foreground. If this parameter is not supplied, the fg command uses the job most recently suspended, placed in the background, or run as a background job .
    Syntax :

    fg [ %job]

    Options

    %job: Specifies the job that you want to run in the foreground.
    

    fg examples

    Command

    $ fg

    Output:

    It will resume the most recently suspended or background job.
    

    Command

    $ fg 1

    Output:

    It brings the job with the id 1 into the foreground, resuming it if it was suspended.
    
  3. Jobs Command : Jobs command is used to list the jobs that you are running in the background and in the foreground. If the prompt is returned with no information no jobs are present. All shells are not capable of running this command. This command is only available in the csh, bash, tcsh, and ksh shells.
    Syntax :

    jobs  [JOB]

    Options

    JOB      Job name or number.
    -l   Lists process IDs in addition to the normal information.
    -n   List only processes that have changed status since the last notification.
    -p   Lists process IDs only.
    -r   Restrict output to running jobs.
    -s   Restrict output to stopped jobs.
    

    jobs command examples

    To display the status of jobs in the current shell:
    Command

    $ jobs

    Output:

    [1]   7893 Running                 gpass &
    [2]   7904 Running                 gnome-calculator &
    [3]-  7955 Running                 gedit fetch-stock-prices.py &
    [4]+  7958 Stopped                 ping cyberciti.biz
    

    To display the process ID or jobs for the job whose name begins with “p,”:
    Command

    $ jobs -p %p

    OR

    $ jobs %p

    Output:

    [4]-  Stopped                 ping cyberciti.biz

    The character % introduces a job specification. In this example, you are using the string whose name begins with suspended command such as %ping.

    Pass the -p option to jobs command to display PIDs only:
    Command

    $ jobs -p

    Output:

    7895
    7906
    7910
    7946
    

    Pass the -r option to jobs command to display only running jobs only:
    Command

    $ jobs -r

    Output:

    [1]   Running                 gpass &
    [2]   Running                 gnome-calculator &
    [3]-  Running                 gedit fetch-stock-prices.py &
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.