自守护 bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18954527/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:39:31  来源:igfitidea点击:

Self-daemonizing bash script

bashdaemon

提问by pepoluan

I want to make a script to be self-daemonizing, i.e., no need to invoke nohup $SCRIPT &>/dev/null &manually on the shell prompt.

我想让脚本自我守护,即不需要nohup $SCRIPT &>/dev/null &在 shell 提示符下手动调用。

My plan is to create a section of code like the following:

我的计划是创建一段如下所示的代码:

#!/bin/bash
SCRIPTNAME="
if [[  != "--daemonize" ]]; then  
" ... # Preps are done above if [[ "" != "--daemonize" ]]; then nohup "$SCRIPTNAME" --daemonize "${PARAMS[@]}" &>/dev/null & exit $? fi # Rest of the code are the actual procedures of the daemon

Is this wise? Do you have better alternatives?

这是明智的吗?你有更好的选择吗?

采纳答案by konsolebox

Here are things I see.

这是我看到的东西。

nohup $SCRIPTNAME --daemonize "${PARAMS[@]}" &>/dev/null &

Shouln't that be == --daemonize?

不应该是 == --daemonize?

(
    Codes that run in daemon mode.
) </dev/null >/dev/null 2>&1 &
disown

Instead of calling your script again, you could just summon a subshell that's placed in a background:

无需再次调用您的脚本,您只需调用一个放置在背景中的子外壳即可:

function daemon_mode {
    Codes that run in daemon mode.
}

daemon_mode </dev/null >/dev/null 2>&1 &
disown

Or

或者

##代码##