如果正在运行一个进程,而且希望在退出当前终端后该进程不会结束,这时可以使用 nohup 命令来实现这个功能。nohup 可以让退出终端后相应的进程仍然继续运行。
如果使用 nohup 命令提交作业,在缺省情况下,该作业的所有输出都被重定向到一个名为 nohup.out 的文件中,除非另外指定了这个输出文件: nohup command > myout.file 2>&1
下面的脚本每隔 3 秒输出 hello,脚本内容为:
[Bash shell] 纯文本查看 复制代码 #!/bin/sh
while true
do
echo "hello"
sleep 3
done
exit 0
现在用 nohup 运行这个脚本: nohup ./nohup.sh > myout.file 2>&1
然后退出该 shell ,再重新登录,执行以下命令: # ps x |grep nohup.sh
7501 ? S 0:00 /bin/sh ./nohup.sh
7589 pts/2 S+ 0:00 grep nohup.sh
可以看到,该脚本仍然在运行。
在日常使用中,一般会在 nohup 命令行的末尾加上 "&" 符号将命令放入后台执行。 |