2011-05-29 48 views
9

我有一个很大的脚本叫做mandacalc,我想总是用nohup命令运行。如果我从命令行调用它:如何在一个bash脚本中包含nohup?

nohup mandacalc & 

一切都很快运行。但是,如果我尝试在我的命令中包含nohup,那么每次执行它时我都不需要输入它,我收到一条错误消息。

到目前为止,我尝试这些选项:

nohup (
command1 
.... 
commandn 
exit 0 
) 

也:

nohup bash -c " 
command1 
.... 
commandn 
exit 0 
" # and also with single quotes. 

到目前为止,我只得到抱怨的nohup命令执行的错误信息,或对其他报价在脚本中使用。

欢呼声。

+0

你需要给的完整路径可能NOHUP? '哪nohup' – wilbbe01 2011-05-29 16:13:17

回答

1

为什么不制作一个包含nohup ./original_script的脚本?

16

尝试把这个在你的脚本的开头:

#!/bin/bash 

case "$1" in 
    -d|--daemon) 
     $0 < /dev/null &> /dev/null & disown 
     exit 0 
     ;; 
    *) 
     ;; 
esac 

# do stuff here 

如果你现在开始​​脚本作为参数,它会自动重新启动,从当前的外壳分离。

您仍然可以通过在没有此选项的情况下启动脚本来“在前台”运行脚本。

+8

Bash是如此奇怪 – 2014-06-25 13:26:57

3

在你的bash(或首选的shell)启动文件创建一个同名的别名:

别名mandacalc = “nohup的mandacalc &”

1

只要把trap '' HUP在脚本的beggining。

此外,如果它创建子进程someCommand&你将不得不改变他们nohup someCommand&正常工作......我一直在研究这个很长一段时间,这两个(陷阱和nohup的)的唯一组合在我的工作xterm关闭太快的特定脚本。

3

有一个很好的答案在这里:http://compgroups.net/comp.unix.shell/can-a-script-nohup-itself/498135

#!/bin/bash 

### make sure that the script is called with `nohup nice ...` 
if [ "$1" != "calling_myself" ] 
then 
    # this script has *not* been called recursively by itself 
    datestamp=$(date +%F | tr -d -) 
    nohup_out=nohup-$datestamp.out 
    nohup nice "$0" "calling_myself" "[email protected]" > $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 $1 
fi 

### the rest of the script goes here 
. . . . . 
相关问题