Linux进程放入后台

Linux下进程放入后台执行几种方法:

1. nohup

顾名思义,nohup的用途就是让提交的命令忽略所有的hangup信号。
使用方法:nohup COMMAND [ARG]…

nohup 示例

1
2
3
4
5
6
7
[root@pvcent107 ~]# nohup ping www.ibm.com &
[1] 3059
nohup: appending output to `nohup.out'
[root@pvcent107 ~]# ps -ef |grep 3059
root      3059   <strong>984</strong>  0 21:06 pts/3    00:00:00 ping www.ibm.com
root      3067   984  0 21:06 pts/3    00:00:00 grep 3059
[root@pvcent107 ~]#

2. setsid
在一个新的会话中运行命令,从而可以避开当前终端发出的HUP信号。
使用方法:setsid COMMAND [ARG]…

setsid 示例

1
2
3
4
5
[root@pvcent107 ~]# setsid ping www.ibm.com
[root@pvcent107 ~]# ps -ef |grep www.ibm.com
root     31094     <strong>1</strong>  0 07:28 ?        00:00:00 ping www.ibm.com
root     31102 29217  0 07:29 pts/4    00:00:00 grep www.ibm.com
[root@pvcent107 ~]#

3. &
可以结合()产生一个新的子shell并在这个子shell中将任务放置到后台运行,从而不受当前shell终端的HUP信号影响。
使用方法:(COMMAND [ARG]… &)

subshell 示例

1
2
3
4
5
[root@pvcent107 ~]# (ping www.ibm.com &amp;)
[root@pvcent107 ~]# ps -ef |grep www.ibm.com
root     16270     <strong>1</strong>  0 14:13 pts/4    00:00:00 ping www.ibm.com
root     16278 15362  0 14:13 pts/4    00:00:00 grep www.ibm.com
[root@pvcent107 ~]#

而我通常的使用方式为:
nohup ./filename.sh > filename.log 2>&1 &
三点理由:
1)nohup保障进程不会被hangup信号异常中断;
2)将任务放置到后台运行,不占用当前的终端;
3)将错误输出也打印到log中,默认>只有标准输出,错误输出没有。

4.控制进程
通过以下命令,我们可以对放入到后台的命令进行控制

查看当前终端下的后台进程:
直接执行:jobs

将查看到的某个后台进程放回到前台:
直接输入:fg {jobid} //这里的{jobid}是通过jobs命令中看到的进程前[]中的数字。

将当前正在前台运行的进程放到后台运行:
先敲下快捷键:ctrl +z //暂停当前正在运行的进程。
再执行:bg

终止当前正在前台运行的进程:
直接敲下快捷键:ctrl +c

查看当前后台进程 jobs -l

5.disown
亡羊补牢,为没有使用nohup与setsid的进程加上忽略HUP信号的功能。
使用方法:
将当前正在前台运行的进程放到后台运行;
然后执行disown -h %{jobid} //这里的{jobid}是通过jobs命令中看到的进程前[]中的数字。

  • disown -h jobspec 来使某个作业忽略HUP信号。
  • disown -ah 来使所有的作业都忽略HUP信号。
  • disown -rh 来使正在运行的作业忽略HUP信号。
  • disown 示例1(如果提交命令时已经用“&”将命令放入后台运行,则可以直接使用“disown”)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    [root@pvcent107 build]# cp -r testLargeFile largeFile &amp;
    [1] 4825
    [root@pvcent107 build]# jobs
    [1]+  Running                 cp -i -r testLargeFile largeFile &amp;
    [root@pvcent107 build]# disown -h %1
    [root@pvcent107 build]# ps -ef |grep largeFile
    root      4825   968  1 09:46 pts/4    00:00:00 cp -i -r testLargeFile largeFile
    root      4853   968  0 09:46 pts/4    00:00:00 grep largeFile
    [root@pvcent107 build]# logout

6.通过screen来实现稳定的后台运行
screen是建立一个新的全屏虚拟会话终端,这个会话只有在手动输入exit的时候才会退出,在这个会话里执行的命令不用担心HUP信号会对我们的进程 造成影响,因此也不用给每个命令前都加上“nohup”或“setsid”了,非常适合我们有规划的执行大量的后台任务,可以非常方便的让我们对这些后台 任务进行管理。

使用方法:
screen //立即创建并进入一个会话。
screen -dmS {name} //建立一个处于断开模式下的会话,并根据我们的需要指定其会话名称。
screen -list //列出所有会话。
screen -r {name} //进入指定会话。
ctrl +ad //输入快捷键ctrl +a和d,可暂时退出当前会话。
exit //进入指定会话后执行exit即可关闭该会话。

screen 示例

1
2
3
4
5
6
7
8
9
[root@pvcent107 ~]# screen -dmS Urumchi
[root@pvcent107 ~]# screen -list
There is a screen on:
        12842.Urumchi   (Detached)
1 Socket in /tmp/screens/S-root.
 
[root@pvcent107 ~]# screen -r Urumchi
 
<a name="screen1"></a><strong>1. 未使用 screen 时新进程的进程树</strong>
1
2
3
4
5
6
7
8
9
[root@pvcent107 ~]# ping www.google.com &amp;
[1] 9499
[root@pvcent107 ~]# pstree -H 9499
init─┬─Xvnc
     ├─acpid
     ├─atd
     ├─2*[sendmail]
     <strong>├─sshd─┬</strong>─sshd───bash───pstree
     │      <strong> └─sshd───bash───ping</strong>
1  

我们可以看出,未使用 screen 时我们所处的 bash 是 sshd 的子进程,当 ssh 断开连接时,HUP 信号自然会影响到它下面的所有子进程(包括我们新建立的 ping 进程)。

1 <a name="screen2"></a><strong>2. 使用了 screen 后新进程的进程树</strong>
Shell
1
2
3
4
5
6
7
8
9
[root@pvcent107 ~]# screen -r Urumchi
[root@pvcent107 ~]# ping www.ibm.com &amp;
[1] 9488
[root@pvcent107 ~]# pstree -H 9488
init─┬─Xvnc
     ├─acpid
     ├─atd
     <strong>├─screen───bash───ping</strong>
     ├─2*[sendmail]

猜你喜欢

转载自blog.csdn.net/yuanchunsi/article/details/79295721