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