Linux环境下如何让进程在后台运行

系统 Linux 系统运维
如果只是临时有一个命令需要长时间运行,什么方法能最简便的保证它在后台稳定运行呢?我们的解决办法就有两种途径:要么让进程忽略 HUP 信号,要么让进程运行在新的会话里从而成为不属于此终端的子进程。

一. nohup / setsid / &

使用场景:如果只是临时有一个命令需要长时间运行,什么方法能最简便的保证它在后台稳定运行呢?

我们的解决办法就有两种途径:要么让进程忽略 HUP 信号,要么让进程运行在新的会话里从而成为不属于此终端的子进程。

解决方法:

1.nohup

只需在要处理的命令前加上 nohup 即可,标准输出和标准错误缺省会被重定向到 nohup.out 文件中。一般我们可在结尾加上"&"来将命令同时放入后台运行,也可用>filename 2>&1来更改缺省的重定向文件名。

[root@pvcent107 ~]# nohup ping www.ibm.com & 
[1] 3059 
nohup: appending output to `nohup.out
[root@pvcent107 ~]# ps -ef |grep 3059 
root      3059   984  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 ~]# 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

 

2. setsid

setsid 的使用也是非常方便的,也只需在要处理的命令前加上 setsid 即可。

[root@pvcent107 ~]# setsid ping www.ibm.com 
[root@pvcent107 ~]# ps -ef |grep www.ibm.com 
root     31094     1  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 ~]# 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

 

上例中我们的进程 ID(PID)为31094,而它的父 ID(PPID)为1(即为 init 进程 ID),并不是当前终端的进程 ID。请将此例与nohup 例中的父 ID 做比较。

3. &

将一个或多个命名包含在“()”中就能让这些命令在子 shell 中运行中 当我们将"&"也放入“()”内之后,我们就会发现所提交的作业并不在作业列表中,也就是说,是无法通过jobs来查看的。

[root@pvcent107 ~]# (ping www.ibm.com &) 
[root@pvcent107 ~]# ps -ef |grep www.ibm.com 
root     16270     1  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 ~]# 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

 

新提交的进程的父 ID(PPID)为1(init 进程的 PID),并不是当前终端的进程 ID。因此并不属于当前终端的子进程,从而也就不会受到当前终端的 HUP 信号的影响了。

二. disown

使用场景:如果事先在命令前加上 nohup 或者 setsid 就可以避免 HUP 信号的影响。但是如果我们未加任何处理就已经提交了命令,该如何补救才能让它避免 HUP 信号的影响呢?

解决方法:这时想加 nohup 或者 setsid 已经为时已晚,只能通过作业调度和 disown 来解决这个问题了

  • 用disown -h jobspec来使某个作业忽略HUP信号。
  • 用disown -ah 来使所有的作业都忽略HUP信号。
  • 用disown -rh 来使正在运行的作业忽略HUP信号。

当使用过 disown 之后,会将把目标作业从作业列表中移除,我们将不能再使用jobs来查看它,但是依然能够用ps -ef查找到它。

disown 示例1(如果提交命令时已经用“&”将命令放入后台运行,则可以直接使用“disown”)

[root@pvcent107 build]# cp -r testLargeFile largeFile & 
[1] 4825 
[root@pvcent107 build]# jobs 
[1]+  Running                 cp -i -r testLargeFile largeFile & 
[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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

 

disown 示例2(如果提交命令时未使用“&”将命令放入后台运行,可使用 CTRL-z 和“bg”将其放入后台,再使用“disown”)

[root@pvcent107 build]# cp -r testLargeFile largeFile2 
 
[1]+  Stopped                 cp -i -r testLargeFile largeFile2 
[root@pvcent107 build]# bg %1 
[1]+ cp -i -r testLargeFile largeFile2 & 
[root@pvcent107 build]# jobs 
[1]+  Running                 cp -i -r testLargeFile largeFile2 & 
[root@pvcent107 build]# disown -h %1 
[root@pvcent107 build]# ps -ef |grep largeFile2 
root      5790  5577  1 10:04 pts/3    00:00:00 cp -i -r testLargeFile largeFile2 
root      5824  5577  0 10:05 pts/3    00:00:00 grep largeFile2 
[root@pvcent107 build]# 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

 

三: screen

使用场景: 我们已经知道了如何让进程免受 HUP 信号的影响,但是如果有大量这种命令需要在稳定的后台里运行,如何避免对每条命令都做这样的操作呢?

解决方案: 此时最方便的方法就是 screen 了。简单的说,screen 提供了 ANSI/VT100 的终端模拟器,使它能够在一个真实终端下运行多个全屏的伪终端。screen 的参数很多,具有很强大的功能,

  • 用screen -dmS (sessionName)来建立一个处于断开模式下的会话(并指定其会话名)。
  • 用screen -list 来列出所有会话。
  • 用screen -r (sessionName)来重新连接指定会话。
  • 用快捷键CTRL-a d 来暂时断开当前会话。

screen实例

[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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

 

当我们用“-r”连接到 screen 会话后,我们就可以在这个伪终端里面为所欲为,再也不用担心 HUP 信号会对我们的进程造成影响,也不用给每个命令前都加上“nohup”或者“setsid”了。

1.未使用 screen 时新进程的进程树

[root@pvcent107 ~]# ping www.google.com & 
[1] 9499 
[root@pvcent107 ~]# pstree -H 9499 
init─┬─Xvnc 
     ├─acpid 
     ├─atd 
     ├─2*[sendmail]     
     ├─sshd─┬─sshd───bash───pstree 
     │       └─sshd───bash───ping 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

 

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

2.使用了 screen 后新进程的进程树

[root@pvcent107 ~]# screen -r Urumchi 
[root@pvcent107 ~]# ping www.ibm.com & 
[1] 9488 
[root@pvcent107 ~]# pstree -H 9488 
init─┬─Xvnc 
     ├─acpid 
     ├─atd 
     ├─screen───bash───ping 
     ├─2*[sendmail] 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

 

而使用了 screen 后就不同了,此时 bash 是 screen 的子进程,而 screen 是 init(PID为1)的子进程。那么当 ssh 断开连接时,HUP 信号自然不会影响到 screen 下面的子进程了。

责任编辑:庞桂玉 来源: segmentfault
相关推荐

2017-03-17 16:10:24

linux进程后台

2021-06-10 09:56:09

Linux命令shell

2016-11-02 09:49:21

Linux命令进程

2019-06-26 08:00:39

Docker容器运行命令

2025-02-10 04:00:00

Linux进程Python

2024-01-26 10:56:47

go程序进程

2020-11-08 14:37:46

Python压缩文件代码

2022-01-10 09:05:32

Linux后台命令

2009-06-30 14:40:32

linux

2011-03-03 17:07:58

Pure-FTPd

2010-09-17 11:01:05

Java运行环境

2009-06-22 11:13:00

linuxJava运行环境

2014-07-31 12:07:15

Linux

2014-03-19 09:19:44

KDE应用GNOME

2016-08-23 10:17:42

2011-02-24 10:11:31

FireFTPFirefox

2020-12-18 09:15:16

LinuxVue命令

2010-03-08 17:53:36

Linux后台运行命令

2010-03-08 18:07:02

Linux后台运行命令

2010-03-08 18:18:38

Linux后台运行命令
点赞
收藏

51CTO技术栈公众号