📜  Unix/Linux 中的进程控制命令

📅  最后修改于: 2022-05-13 01:57:34.628000             🧑  作者: Mango

Unix/Linux 中的进程控制命令

Unix 中的进程控制命令是:

bg - put suspended process into background
fg - bring process into foreground
jobs - list processes
  1. bg 命令: bg 是一个进程控制命令,它恢复挂起的进程,同时保持它们在后台运行。用户可以通过在命令末尾添加“&”符号在后台运行作业。
    句法 :
    bg [job]
    

    选项

    字符% 引入了工作规范。 Job 可以是进程 ID (PID) 号,或者我们可以使用以下符号组合之一:

    %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 %1

    输出:

    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 命令: fg 命令将当前 shell 环境中的后台作业移动到前台。使用作业 ID 参数指示要在前台运行的特定作业。如果未提供此参数,则 fg 命令使用最近暂停、置于后台或作为后台作业运行的作业。
    句法 :
    fg [ %job]

    选项

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

    fg 示例

    命令

    $ fg

    输出:

    It will resume the most recently suspended or background job.
    

    命令



    $ fg 1

    输出:

    It brings the job with the id 1 into the foreground, resuming it if it was suspended.
    
  3. 作业命令:作业命令用于列出您在后台和前台运行的作业。如果返回的提示没有任何信息,则不存在作业。并非所有 shell 都能够运行此命令。此命令仅在 csh、bash、tcsh 和 ksh shell 中可用。
    句法 :
    jobs  [JOB]

    选项

    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.
    

    作业命令示例

    要显示当前 shell 中的作业状态:
    命令

    $ jobs

    输出:

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

    要显示名称以“p”开头的作业的进程 ID 或作业,请执行以下操作:
    命令

    $ jobs -p %p

    或者

    $ jobs %p

    输出:

    [4]-  Stopped                 ping cyberciti.biz

    字符% 引入了工作规范。在此示例中,您将使用名称以挂起命令开头的字符串,例如 %ping。

    将 -p 选项传递给 jobs 命令以仅显示 PID:
    命令

    $ jobs -p

    输出:

    7895
    7906
    7910
    7946
    

    将 -r 选项传递给 jobs 命令以仅显示正在运行的作业:
    命令

    $ jobs -r

    输出:

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