Linux 运行后台任务的方法

linux下几种运行后台任务的方法
https://juejin.cn/post/7048846327966334989
https://blog.csdn.net/beeworkshop/article/details/108610678
https://bbs.huaweicloud.com/blogs/350725

 

nohup指令

nohup(no hang up),意思就是不挂断运行,用nohup运行命令可以使命令永久执行下去,和用户终端没有关系,断开SSH不影响运行,nohup捕获了SIGHUP,并做了忽略处理,因此当屏幕关闭,断网等造成ssh中断时进程不会退出。但是ctrl+c可以关闭关闭该进程。因此大多数情况同时使用nohup和&启动的程序,ctrl+c和关闭终端都无法关闭。在缺省情况下所有输出都被重定向到一个名为nohup.out的文件中。

nohup指令基本使用格式:

nohup Command [ Arg ... ] [ & ]

 

举例

后台不中断执行./test.sh,stdout输出给out.log,stderr输出给err.log

nohup ./test.sh > out.log 2>err.log &

 

相关的数字含义如下:

0 – stdin (standard input),

1 – stdout (standard output),显然 nohup command > out.log 等价于 nohup command 1> out.log,是缺省行为。

2 – stderr (standard error)

可能你也会见到这种写法,其含义是把stderr也重定向给stdin

nohup ./test.sh > out.log 2>&1 &

[root@localhost ~]# nohup find / -print > /root/file.log &


$ nohup sh test.sh &
# 直接关闭当前终端,再打开一个查看
$ ps -few|grep test.sh

 

比如本地代码的更新和编译命令,

可以将需要手动输入参数的变成固定参数直接写死在脚本里的,如:

./build.sh xx debug xx

 

posted @ 2023-08-24 14:26  petercao  阅读(66)  评论(0编辑  收藏  举报