Linux 使用 nohup 调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16435629/
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
Call a function using nohup
提问by sajad
I am trying to call a function using nohuplike this:
我正在尝试使用nohup这样的方法调用函数:
function1(){
while true
do
echo "function1"
sleep 1
done
}
nohup function1 &
# ...... some other code
but may be the function isn't seen by nohupand I get this error:
但可能是该功能没有被看到nohup,我收到此错误:
nohup: failed to run command `function1' : No such file or dictionary
I don't want to create new sh file for my function. How can I fix this?
我不想为我的函数创建新的 sh 文件。我怎样才能解决这个问题?
回答by suspectus
nohupapplies to commands and not to script functions.
nohup适用于命令而不是脚本函数。
For example, the script (say func.sh) that contains function1() should call the function-:
例如,包含 function1() 的脚本(比如 func.sh)应该调用函数:
function1(){
while true
do
echo "function1"
sleep 1
done
}
function1
Now call the script func.sh with nohupin the background-:
现在nohup在后台调用脚本 func.sh :
nohup ./func.sh &
If you need to disable the hangup signal from within the script use the shell built-in trap. The example ignores SIGHUP but can be used to ignore others (e.g. SIGINT).
如果您需要从脚本中禁用挂断信号,请使用 shell 内置的trap. 该示例忽略 SIGHUP 但可用于忽略其他(例如 SIGINT)。
trap "" HUP # script will ignore HANGUP signal
回答by Adam Siemion
Since nohupmust be supplied with a filename not a function as a workaround this is what can be done:
由于nohup必须提供文件名而不是函数作为解决方法,因此可以这样做:
function1(){
while true
do
echo "function1"
sleep 1
done
}
echo "$@" | grep -q -- "--nohup" && function1 || nohup function1(){ local msg=${*:-function1}; echo msg=$msg; }
nohup -- sh -c "$(typeset -f function1); function1 MESSAGE" >nohup.log 2>&1 0</dev/null &
"$@" --nohup &
So when this script gets called with the current arguments:
因此,当使用当前参数调用此脚本时:
`echo "$@" | grep -q -- "--nohup"will return an error status sonohup $0 "$@" --nohup &will be invoked, which will invoke this script passing the current arguments and a new argument--nohup
`echo "$@" | grep -q -- "--nohup"将返回错误状态,所以nohup $0 "$@" --nohup &将被调用,这将调用此脚本并传递当前参数和新参数--nohup
And when this script is called with argument --nohup
当这个脚本用参数调用时 --nohup
`echo "$@" | grep -q -- "--nohup"will return with zero status (success) sofunction1will be invoked
`echo "$@" | grep -q -- "--nohup"将返回零状态(成功)所以function1将被调用
回答by scavenger
Yes ! It is possible however tricky, and strictly bash > v2 compatible :
是的 !无论多么棘手,并且严格 bash > v2 兼容,这是可能的:
function background {
echo TEST
}
export -f background
nohup bash -c background &
And don't forget "typeset" is bash deprecated in favor of "declare" (though I don't entirely agree with this).
并且不要忘记“排版”被bash弃用而支持“声明”(尽管我不完全同意这一点)。
回答by Lachezar
I find a working solution for me - define the function in a file (e.g. .functions) then run the function with nohup:
我为我找到了一个可行的解决方案 - 在文件中定义函数(例如.functions),然后使用 nohup 运行该函数:
nohup bash -c "source .functions; function1" &
nohup bash -c "source .functions; function1" &
Tested on Ubuntu 13.04.
在 Ubuntu 13.04 上测试。
回答by user7813184
Another solution:
另一种解决方案:
(trap '' HUP INT
while true
do
echo "function1"
sleep 1
done
) </dev/null 2>&1 1>nohup.out &
回答by James Carey
Instead of using nohup, which is tailored for files, you can implement the same result like this:
您可以像这样实现相同的结果,而不是使用为文件量身定制的 nohup:
##代码##As I tend to launch these processes from a parent script and the parent script may do other work if the parent process is interrupted I want the child process to ignore the INT signal and keep on running.
因为我倾向于从父脚本启动这些进程,如果父进程被中断,父脚本可能会做其他工作,所以我希望子进程忽略 INT 信号并继续运行。

