相关疑难解决方法(0)

使用bash shell脚本检查程序是否正在运行?

这是一个bash脚本的示例,它检查某些正在运行的进程(守护程序或服务),并在没有此类进程运行时执行特定操作(重新加载,发送邮件).

check_process(){
        # check the args
        if [ "$1" = "" ];
        then
                return 0
        fi

        #PROCESS_NUM => get the process number regarding the given thread name
        PROCESS_NUM='ps -ef | grep "$1" | grep -v "grep" | wc -l'
        # for degbuging...
        $PROCESS_NUM
        if [ $PROCESS_NUM -eq 1 ];
        then
                return 1
        else
                return 0
        fi
}

# Check whether the instance of thread exists:
while [ 1 ] ; do
        echo 'begin checking...'
        check_process "python test_demo.py" # the thread …
Run Code Online (Sandbox Code Playgroud)

shell ps

19
推荐指数
2
解决办法
10万
查看次数

标签 统计

ps ×1

shell ×1