4. disown 如果我们未加任何处理就已经提交了命令,这时想加 nohup 或者 setsid 已经为时已晚,只能通过作业调度和 disown 来解决这个问题了。让我们来看一下 disown 的帮助信息: disown [-ar] [-h] [jobspec ...] Without options, each jobspec is removed from the table of active jobs. If the -h option is given, each jobspec is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP. If no jobspec is present, and neither the -a nor the -r option is supplied, the current job is used. If no jobspec is supplied, the -a option means to remove or mark all jobs; the -r option without a jobspec argument restricts operation to running jobs. The return value is 0 unless a jobspec does not specify a valid job. 可以看出,我们可以用如下方式来达成我们的目的。 a. 用disown -h jobspec 来使某个作业忽略HUP信号。 b. 用disown -ah 来使所有的作业都忽略HUP信号。 c. 用disown -rh 来使正在运行的作业忽略HUP信号。 需要注意的是,当使用过 disown 之后,会将把目标作业从作业列表中移除,我们将不能再使用jobs来查看它,但是依然能够用ps -ef查找到它。 但是还有一个问题,这种方法的操作对象是作业,如果我们在运行命令时在结尾加了"&"来使它成为一个作业并在后台运行,那么就万事大吉了,我们可以通过jobs命令来得到所有作业的列表。但是如果并没有把当前命令作为作业来运行,如何才能得到它的作业号呢?答案就是用 CTRL-z(按住Ctrl键的同时按住z键)了! CTRL-z 的用途就是将当前进程挂起(Suspend),然后我们就可以用jobs命令来查询它的作业号,再用bg jobspec 来将它放入后台并继续运行。需要注意的是,如果挂起会影响当前进程的运行结果,慎用此方法。 示例: root@ubuntu-server:/mnt/ftp# jobs [1]+ Running ./sync_mirror.sh & root@ubuntu-server:/mnt/ftp# disown -h %1 root@ubuntu-server:/mnt/ftp# ps -ef|grep sync_mirror.sh root 8650 22271 0 15:08 pts/0 00:00:00 grep sync_mirror.sh root 16748 22271 0 Mar31 pts/0 00:00:00 /bin/sh -e ./sync_mirror.sh