如何在bash脚本中包含nohup? [英] How to include nohup inside a bash script?

查看:532
本文介绍了如何在bash脚本中包含nohup?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 mandacalc 的大型脚本,该脚本始终与 nohup 命令一起运行.如果我从命令行以以下方式调用它:

I have a large script called mandacalc which I want to always run with the nohup command. If I call it from the command line as:

nohup mandacalc &

一切迅速进行.但是,如果我尝试在命令中包含 nohup ,那么我不必每次执行时都键入它,就会收到错误消息.

everything runs swiftly. But, if I try to include nohup inside my command, so I don't need to type it everytime I execute it, I get an error message.

到目前为止,我尝试了以下选项:

So far I tried these options:

nohup (
command1
....
commandn
exit 0
)

还有:

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

到目前为止,我只收到错误消息,抱怨有关 nohup 命令的实现或脚本中使用的其他引号.

So far I only get error messages complaining about the implementation of the nohup command, or about other quotes used inside the script.

欢呼.

推荐答案

尝试将其放在脚本的开头:

Try putting this at the beginning of your script:

#!/bin/bash

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

# do stuff here

如果现在以--daemon作为参数启动脚本,它将重新启动与当前shell分离的脚本.

If you now start your script with --daemon as an argument, it will restart itself detached from your current shell.

您仍然可以通过不使用此选项启动脚本来在前台"运行脚本.

You can still run your script "in the foreground" by starting it without this option.

这篇关于如何在bash脚本中包含nohup?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆