网站制作学习网Linux→正文:linux 命令后台运行 &
字体:

linux 命令后台运行 &

Linux 2016/1/12 15:11:43  点击:不统计

http://www.forasp.cn/
 经常做一些后台常驻内存运行的命令或者程序,比如shell python等,在程序中是个while死循环

 
1.在linux登录用户状况下运营后台命令,我们以运营一个php test.php命令为例
test.php代码
<?php
while(true){
//dosomething
if($true){
break;
}
}
?>
 
forasp-linux root#> php test.php
 
//上面的命令行会一直等待用户进行处理或者等着内部true的条件生效,用户端按ctrl+c结束等待
 
 
当我们希望在后台运行则主要 在命名末尾添加一个 & 
forasp-linux root#> php test.php &
[1] 8683   //这里显示的后台的进程号
forasp-linux root#> 
 
这样命令就在后台执行了
forasp-linux root#> ps aux|grep test
root      8683  0.0  0.4 328244  9012 pts/0    T    14:45   0:00 php test.php
root      8708  0.0  0.0 103244   880 pts/0    S+   14:46   0:00 grep test
forasp-linux root#> #可以看到对应的进行
forasp-linux@root#> fg 
php test.php
 
//按ctrl+c 退出
forasp-linux root#>exit
fg命令是将后台运行的程序进行前台显示
 
当我们退出当前账号的时候会有个提示
[1]+  Stopped                 php test.php  
 
退出之后就不再运行了。所以这个&号是在登录的时候后台运行,当用户退出的时候结束运行
 
 
2.怎么才能使后台运行的命令一直不退出呢,或者说是退出登录的时候不退出后台运行
nohup 命令
语法 nohup php test.php [>somefile] & 如果不指定输出文件则为nohup.out文件
forasp-linux@root#> nohup php test.php &
[1] 9009
forasp-linux@root#> nohup: 忽略输入并把输出追加到"nohup.out"
forasp-linux root#> ps aux|grep test
root      8683  0.0  0.4 328244  9012 pts/0    T    14:55   0:00 php test.php
root      8708  0.0  0.0 103244   880 pts/0    S+   14:57   0:00 grep test
forasp-linux root#> #可以看到对应的进行
然后我们再退出当前用户用其他用户查询发现程序仍然在执行。

网站http://www.制forasp作.cn

·上一篇:poll_create() failed (23: Too many open files in system) >>    ·下一篇:sftp-server process_write: write failed >>
推荐文章
最新文章