加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在后台运行当前的bash脚本

发布时间:2020-12-15 18:57:01 所属栏目:安全 来源:网络整理
导读:通常我会添加“”在背景中开始我的过程的角色,例如: user@pc:~$my_script 但是如何在没有“”的背景下制作它性格? #!/bin/bash#What can I add here to hide current process ($$) and to release focus ?start_server(){ #my script here with infinite l
通常我会添加“&”在背景中开始我的过程的角色,例如:
user@pc:~$my_script &

但是如何在没有“&”的背景下制作它性格?

#!/bin/bash

#What can I add here to hide current process ($$) and to release focus ?

start_server()
{   
    #my script here with infinite loop ...
}

多谢你们.

#!/bin/bash

if [[ "$1" != "--nodaemon" ]]; then
    ( "$0" --nodaemon "$@" </dev/null &>/dev/null & )
else
    shift
fi

#...rest of script

这样做是检查它的第一个参数是否是“–nodaemon”,如果是这样的话,在背景中使用参数“–nodaemon”将其自身(“$0”)关闭,这将阻止它尝试重新-background本身处于一种无限循环中.

请注意,将此作为脚本中的第一件事将使其始终在后台运行.如果它只需要在某些条件下进入后台(例如,当使用参数“start”运行时),则必须相应地调整它.也许是这样的:

#!/bin/bash

start_server()
{   
    #my script here with infinite loop ...
}

if [[ "$1" = "start" ]]; then
    ( "$0" start-nodaemon </dev/null &>/dev/null & )
elif [[ "$1" = "start-nodaemon" ]]; then
    start_server
elif #.....

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读