[root@HYXD ~]# nohup ./ren.sh & -&代表后台运行
[1] 56833
[root@HYXD ~]# nohup: 忽略输入并把输出追加到"nohup.out" --enter后输入jobs查看后台程序
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@HYXD ~]# jobs -jobs查看后台运行的程序
[1]+ Running nohup ./ren.sh &
[root@HYXD ~]# cat ren.sh
/bin/sleep 50s
[root@HYXD ~]#
[root@HYXD ~]# fg 1 -调回前台运行 crtl+z停止运行
nohup ./ren.sh
[root@HYXD ~]# jobs
[1]+ Stopped nohup ./ren.sh
[root@HYXD ~]# ps aux |grep ./ren
root 56839 0.0 0.1 106056 1272 pts/1 T 09:23 0:00 /bin/sh ./ren.sh
root 56843 0.0 0.0 103248 876 pts/1 S+ 09:25 0:00 grep ./ren
[root@HYXD ~]#
[root@HYXD ~]# kill -9 56839
[root@HYXD ~]# ps aux |grep ./ren
root 56845 0.0 0.0 103248 876 pts/1 S+ 09:26 0:00 grep ./ren
[1]+ 已杀死 nohup ./ren.sh
[root@HYXD ~]#
[root@HYXD ~]# jobs -此时无后台程序 jobs -l查看
[root@HYXD ~]#
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@HYXD ~]# ./ren.sh &
[1] 56851
[root@HYXD ~]# ./1.sh &
[2] 56853
[root@HYXD ~]# jobs
[1]- Running ./ren.sh & -代表需要等待后台1进程执行完在执行2
[2]+ Running ./1.sh &
[root@HYXD ~]# cat ren.sh
/bin/sleep 50s
[root@HYXD ~]# cat 1.sh
/bin/sleep 50s
[root@HYXD ~]#
[root@HYXD ~]# ./ren.sh &
[1] 56874
[root@HYXD ~]# ./1.sh &
[2] 56876
[root@HYXD ~]# jobs
[1]- Running ./ren.sh &
[2]+ Running ./1.sh &
[root@HYXD ~]# jobs
[1]- Done ./ren.sh
[2]+ Running ./1.sh &
[root@HYXD ~]# jobs
[2]+ Running ./1.sh &
[root@HYXD ~]# jobs
[2]+ Running ./1.sh &
[root@HYXD ~]# jobs
[2]+ Done ./1.sh
[root@HYXD ~]# jobs
[root@HYXD ~]#
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bg将一个在后台暂停的命令,变成继续执行!
[root@HYXD ~]# ./ren.sh
^Z
[1]+ Stopped ./ren.sh
[root@HYXD ~]# bg 1
[1]+ ./ren.sh &
[root@HYXD ~]# jobs
[1]+ Running ./ren.sh &
[root@HYXD ~]#
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nohup和&有什么区别呢?
&:将任务放到后台 ,即使关闭xshell退出当前session依然继续运行,但标准输出和标准错误信息会丢失
nohup:将任务放到后台,关闭标准输入,前台不再能够接收任何输入(标准输入),重定向标准输出和标准错误到当前目录下的文件nohup.out,即使关闭xshell退出当前session依然继续运行。
nohup和&的缺点:
如果你要在一个shell会话里面执行多个命令和脚本,那么要每个命令和脚本都要加nohup和&非常麻烦,所以才有了screen和TMUX等工具。
本文出自 “任志远@IT分享” 博客,谢绝转载!
原文地址:http://renzhiyuan.blog.51cto.com/10433137/1833834