linux 命令:nohup 详解
nohup 命令的功能是在不掛端的情況下執(zhí)行命令,默認(rèn)會(huì)輸出一個(gè) nohup.out 的文件。
用法:
nohup COMMAND [ARG]...
nohup OPTION
如果標(biāo)準(zhǔn)輸入是終端,則將其從不可讀的文件中重定向。 如果標(biāo)準(zhǔn)輸出是終端,則盡可能將輸出附加到“nohup.out”,否則附加到“$HOME/nohup.out”。 如果標(biāo)準(zhǔn)錯(cuò)誤是終端,則將其重定向到標(biāo)準(zhǔn)輸出。 要將輸出保存到 FILE,請(qǐng)使用“nohup COMMAND > FILE”。
選項(xiàng)
--help? ? ? ? 幫助文檔
--version? ? ? ? 版本信息
使用示例:
寫一個(gè)實(shí)例腳本,每一秒輸出一個(gè)數(shù)字,數(shù)字自動(dòng)增加:
# cat nohuptest.sh #!/bin/bash count=0 while [[ $count -lt 10000 ]] doecho $countsleep 1((count++)) done首先在前臺(tái)執(zhí)行一下,看看是什么效果:
# sh nohuptest.sh 0 1 2 3 4 5 ^C在 linux 系統(tǒng)中,^C(Ctrl + C)發(fā)出的是 SIGINT 信號(hào),可以終止進(jìn)程,更多關(guān)于信號(hào)的內(nèi)容,請(qǐng)看linux 命令:kill 詳解_yspg_217的博客-CSDN博客
在接收到 SIGINT 信號(hào)后,前臺(tái)執(zhí)行的進(jìn)程立刻就終止了。
下面用 nohup 試一下:
# nohup sh nohuptest.sh nohup: ignoring input and appending output to ‘nohup.out’^C # cat nohup.out 0 1 2 3 4 5# ps -ef | grep nohup #如果使用 nohup 接啟動(dòng)命令,雖然輸出是從終端轉(zhuǎn)到了 nohup.out 中,但是前臺(tái)還是被占用,無(wú)法執(zhí)行其他操作。但是在操作了 ^C 后,雖然可以進(jìn)行其他操作,進(jìn)程也停止了。
# nohup sh nohuptest.sh & [1] 9153 # nohup: ignoring input and appending output to ‘nohup.out’# cat nohup.out 0 # cat nohup.out 0 1 # cat nohup.out 0 1 2 ...# ps -ef | grep nohup root 9153 6178 0 14:38 pts/0 00:00:00 sh nohuptest.sh使用 & 后,輸出一個(gè)進(jìn)程ID,進(jìn)程在后臺(tái)執(zhí)行,前臺(tái)不受影響。查看 nohup.out 文件,發(fā)現(xiàn)數(shù)字在逐漸增加。
# nohup sh nohuptest.sh > /dev/null & [1] 10334 # nohup: ignoring input and redirecting stderr to stdout# cat nohup.out #可以把輸出重定向到 /dev/null,/dev/null 是一個(gè)黑洞,重定向到它的數(shù)據(jù)都會(huì)被扔掉。查看 nohup.out,發(fā)現(xiàn)是空文件。
總結(jié)
以上是生活随笔為你收集整理的linux 命令:nohup 详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: OpenReports中文支持方案
- 下一篇: linux下添加用户的命令--usera