如何在 bash 脚本中包含 nohup?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6168781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include nohup inside a bash script?
提问by felipebm
I have a large script called mandacalcwhich I want to always run with the nohupcommand. If I call it from the command line as:
我有一个名为mandacalc的大脚本,我想始终使用nohup命令运行它。如果我从命令行调用它:
nohup mandacalc &
everything runs swiftly. But, if I try to include nohupinside my command, so I don't need to type it everytime I execute it, I get an error message.
一切都进行得很快。但是,如果我尝试在我的命令中包含nohup,因此我不需要在每次执行时都输入它,我会收到一条错误消息。
So far I tried these options:
到目前为止,我尝试了这些选项:
nohup (
command1
....
commandn
exit 0
)
and also:
并且:
nohup bash -c "
command1
....
commandn
exit 0
" # and also with single quotes.
So far I only get error messages complaining about the implementation of the nohupcommand, or about other quotes used inside the script.
到目前为止,我只收到错误消息,抱怨nohup命令的实现,或脚本中使用的其他引号。
cheers.
干杯。
回答by
Try putting this at the beginning of your script:
尝试将其放在脚本的开头:
#!/bin/bash
case "" in
-d|--daemon)
alias mandacalc="nohup mandacalc &"
< /dev/null &> /dev/null & disown
exit 0
;;
*)
;;
esac
# do stuff here
If you now start your script with --daemon
as an argument, it will restart itself detached from your current shell.
如果您现在将脚本--daemon
作为参数启动,它将重新启动,与您当前的 shell 分离。
You can still run your script "in the foreground" by starting it without this option.
您仍然可以通过在没有此选项的情况下启动脚本来“在前台”运行脚本。
回答by Phil
Create an alias of the same name in your bash (or preferred shell) startup file:
在 bash(或首选 shell)启动文件中创建一个同名别名:
#!/bin/bash
### make sure that the script is called with `nohup nice ...`
if [ "" != "calling_myself" ]
then
# this script has *not* been called recursively by itself
datestamp=$(date +%F | tr -d -)
nohup_out=nohup-$datestamp.out
nohup nice "##代码##" "calling_myself" "$@" > $nohup_out &
sleep 1
tail -f $nohup_out
exit
else
# this script has been called recursively by itself
shift # remove the termination condition flag in
fi
### the rest of the script goes here
. . . . .
回答by Aquarius Power
Just put trap '' HUP
on the beggining of your script.
只要穿上trap '' HUP
你的剧本的开始。
Also if it creates child process someCommand&
you will have to change them to nohup someCommand&
to work properly... I have been researching this for a long time and only the combination of these two (the trap and nohup) works on my specific script where xterm closes too fast.
此外,如果它创建子进程,someCommand&
您将不得不将它们更改nohup someCommand&
为正常工作......我一直在研究这个很长一段时间,只有这两个(陷阱和 nohup)的组合适用于我的特定脚本,其中 xterm 也关闭快速地。
回答by dashohoxha
There is a nice answer here: http://compgroups.net/comp.unix.shell/can-a-script-nohup-itself/498135
这里有一个很好的答案:http: //compgroups.net/comp.unix.shell/can-a-script-nohup-itself/498135
##代码##回答by cnicutar
Why don't you just make a script containing nohup ./original_script
?
你为什么不制作一个包含nohup ./original_script
?