如何让进程在Linux后台运行
在Linux中,如果要让进程在后台运行,一般情况下,我们在命令后面加上&即可,实际上,这样是将命令放入到一个作业队列中了:
[root@localhost /]# ./test.sh &
[1] 17208
然后我们就可以用以下命令进行查看:
[root@localhost /]# jobs -l
[1] 17208 Running ./test.sh &
对于已经在前台执行的命令,也可以重新放到后台执行首先按ctrl z暂停已经运行的进程,然后使用bg命令将停止的作业放到后台运行:
[root@localhost /]# bg %1
[1] ./test.sh &
[root@localhost /]# jobs -l
[1] 22794 Running ./test.sh &
但是如上方到后台执行的进程,其父进程还是当前终端shell的进程,而一旦父进程退出,则会发送hangup信号给所有子进程,子进程收到hangup以后也会退出。如果我们要在退出shell的时候继续运行进程,则需要使用nohup忽略hangup信号,或者setsid将将父进程设为init进程(进程号为1)
[root@localhost /]# echo $$
21734
[root@localhost /]# nohup ./test.sh &<br />
[1] 29016
[root@localhost /]# ps -ef | grep test
515 29710 21734 0 11:47 pts/12 00:00:00 /bin/sh ./test.sh
515 29713 21734 0 11:47 pts/12 00:00:00 grep test
[root@localhost /]# setsid ./test.sh &
[1] 409
[root@localhost /]# ps -ef | grep test
515 410 1 0 11:49 ? 00:00:00 /bin/sh ./test.sh
515 413 21734 0 11:49 pts/12 00:00:00 grep test
上面的试验演示了使用nohup/setsid加上&使进程在后台运行,同时不受当前shell退出的影响。那么对于已经在后台运行的进程,该怎么办呢?可以使用disown命令:
[root@localhost /]# ./test.sh &
[1] 2539
[root@localhost /]# jobs -l
[1] 2539 Running ./test.sh &
[root@localhost /]# disown -h %1
[root@localhost /]# ps -ef | grep test
515 410 1 0 11:49 ? 00:00:00 /bin/sh ./test.sh
515 2542 21734 0 11:52 pts/12 00:00:00 grep test
另外还有一种方法,即使将进程在一个subshell中执行,其实这和setsid异曲同工。方法很简单,将命令用括号() 括起来即可:
[root@localhost /]# (./test.sh &)
[root@localhost /]# ps -ef | grep test
515 410 1 0 11:49 ? 00:00:00 /bin/sh ./test.sh
515 12483 21734 0 11:59 pts/12 00:00:00 grep test
注:本文试验环境为Red Hat Enterpr
相关文档:
Linux
/ Linux文件系统的入口,也是处于最高一级的目录;
/bin 基础系统所需要的那些命令位于此目录,也是最小系统所需要的命令;比如 ls、cp、mkdir等命令;功能和/usr/bin类似,这个目录中的文件都是可执行的,普通用户都可以使用的命令。做为基础系统所需要的最基础的命令就是放在这里。
/boot Linux的内核及引导系 ......
首先,服务器GCC要有,不然什么都不能做.能够用gcc -v来查看是否安装了GCC,
#gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info
--enable-shared --enable-threads=posix --disable-checki ......
今天看到师兄工位上有本Linux Kernel In a Nutshell,还是全英的影印版,所以就借过来看看,发现现在看英文的书渐渐适应了,还是比较容易的,书中开始介绍如何编译Linux内核,Linux内核大三的时候也学过编译过,但是当时编译比较简单,这次决定再重头到尾好好编译一下,首先下载linux kernel,再用secureCRT上传到Ubuntu 8. ......
一.什么是NS 2
NS 2是一种针对网络技术的源代码公开的、免费的软件模拟平台,研究人员使用它可以很容易的进行网络技术的开发,而且发展到今天,它所包含的模块非常丰富,几乎涉及到了网络技术的所有方面。
NS 2(Network Simulator, version
2)是一种面 ......
<!--
@page { margin: 2cm }
P { margin-bottom: 0.21cm }
A:link { so-language: zxx }
-->
所谓“阅兵”就是检阅部队的意思。
Linux
大军(发行版)可谓“散兵游勇”,谈何大阅兵?此言差矣。
......