centos后台运行程序(nohup+screen)

如题所述

第1个回答  2022-07-31

linux系统经常需要程序在后台运行,程序不随着会话的关闭或用户的退出而关闭。

有两种常用的后台运行程序的方式: nohup 和 screen 。

当前操作系统环境: centos7

(1)后台不挂起执行xxx.sh。标准输出、错误输出,输出到xxx.out文件

此种情况,程序会再后台执行, 退出用户 或关闭客户终端,不影响程序运行。如想关闭程序,可通过 kill 命令关闭。

(2)后台不挂起执行xxx.sh。标准输出、错误输出,均不输出

(1)输入输出重定向

linux环境下以下数字代表不同的含义:

默认情况下,标准输出、标准错误, 均会输出到控制台 。

默认所有的输出都是“标准输出”,除非将输出重定向到“标准错误”,才会出现“标准错误”输出

如 echo 'error msg' > &2 ,代表将输出结果 error msg 由 标准输出 重定向输出到 标准错误 中。

输出执行结果时,默认重定向的是标准输出的信息,如下,执行结果是一样的:

为什么错误输出到标准输出,写作 2>&1, 而不是写作 2>1 ?

因为: 2>1 表示将“标准错误”输出到文件 1 中,这里的 1 代表的是文件“1”,而不是标准输出。需写成 &1 ,表示为标准输出。

(2)&(后台执行)

命令 末尾 的 & 代表程序后台执行,执行完成后,仍然会将输出结果输出到指定的输出目标。

如只是简单在执行命令后加上&,程序每次执行输出, 仍然会将程序输出到控制台 (并不是全部执行完后才输出)。如下:

(3)nohup(不挂起)

使程序运行时不挂起, 不向 tty 输出信息。nohup 只是不将程序输出到前台,并不是完全在后台运行脱离当天会话,如果关闭会话,当前程序仍然会关闭。

常结合末尾的 & 一起使用。

如命令末尾不加上 & ,则当前会话窗口会等待运行的程序运行完毕,才可执行其他命令,如下:

上述命令,为执行test.sh(脚本见 9 测试脚本 ),并将 标准输出 重定向到 out.log 文件,标准错误重定向到 error.log 文件。

生成的日志文件如下:

screen(窗口管理器)可以创建后台会话窗口。退出当前会话后,后台会话窗口仍而在运行,可以随意切换到后台窗口中。

以下命令查看官方文档:

[root@w001 zhang]# screen -help
Use: screen [-opts] [cmd [args]]
or: screen -r [host.tty]

Options:
-4 Resolve hostnames only to IPv4 addresses.解析主机名为IPv4地址
-6 Resolve hostnames only to IPv6 addresses.解析主机名到IPv6地址
-a Force all capabilities into each window's termcap.强制所有功能进入每个窗口的termcap。
-A -[r|R] Adapt all windows to the new display width & height.使所有窗口适应新的显示宽度和高度。
-c file Read configuration file instead of '.screenrc'.文件读取配置文件,而不是'.screenrc'。
-d (-r) Detach the elsewhere running screen (and reattach here).断开其他正在运行的屏幕(并在此重新连接)。
-dmS name Start as daemon: Screen session in detached mode.名称启动作为守护进程:屏幕会话在分离模式。
-D (-r) Detach and logout remote (and reattach here).断开和注销远程(并在此重新连接)。
-D -RR Do whatever is needed to get a screen session.执行获取屏幕会话所需的所有操作。
-e xy Change command characters.修改命令字符
-f Flow control on, -fn = off, -fa = auto.流量控制on, -fn = off, -fa = auto。
-h lines Set the size of the scrollback history buffer.设置滚动历史缓冲区的大小。
-i Interrupt output sooner when flow control is on.当流控制打开时,中断输出的速度更快。
-l Login mode on (update /var/run/utmp), -ln = off.登录模式on (update /var/run/utmp), -ln = off。
-ls [match] or -list Do nothing, just list our SockDir [on possible matches].什么都不做,只是列出我们的SockDir[可能的匹配]。
-L Turn on output logging.打开输出日志记录。
-m ignore STY变量,创建一个新的屏幕会话。
-O Choose optimal output rather than exact vt100 emulation.选择最优输出,而不是精确的vt100仿真。
-p window Preselect the named window if it exists.窗口预选命名窗口,如果它存在。
-q Quiet startup. Exits with non-zero return code if unsuccessful.安静的启动。如果不成功,则使用非零返回码退出。
-Q Commands will send the response to the stdout of the querying process.命令将响应发送到查询过程的stdout。
-r [session] Reattach to a detached screen process.重新连接到一个分离的屏幕进程。
-R Reattach if possible, otherwise start a new session.如果可能,重新连接,否则启动一个新会话。
-s shell Shell to execute rather than shell。
-S sockname Name this session <pid>.sockname instead of <pid>.<tty>.<host>. 将此会话命名为。sockname而不是<pid>.<tty>.<host>。
-t title Set title. (window's name).标题设置标题。(窗口的名字)。
-T term Use term as term用于windows,而不是“screen”。
-U Tell screen to use UTF-8 encoding.告诉screen使用UTF-8编码。
-v Print "Screen version 4.01.00devel (GNU) 2-May-06".打印"屏幕版本4.01.00devel (GNU) 2-May-06"。
-wipe [match] Do nothing, just clean up SockDir [on possible matches].什么都不做,只是清理SockDir[可能的匹配]。
-x Attach to a not detached screen. (Multi display mode).连接到未分离的屏幕。(多显示模式)。
-X Execute <cmd> as a screen command in the specified session.在指定会话中执行作为屏幕命令。