检查进程是否 运行 或不是 Linux

Check whether a process is running or not Linux

这是我的代码:

#!/bin/bash
ps cax | grep testing > /dev/null

while [ 1 ]
do
if [ $? -eq 0 ]; then
    echo "Process is running."
    sleep 10
else
    nohup ./testing.sh &
    sleep 10
fi

done

我运行它作为nohup ./script.sh &

它说 nohup: failed to run command './script.sh': No such file or directory

怎么了?

确保您的脚本保存为 script.sh 和你执行 nohup ./script.sh & 来自 script.sh 所在的同一目录。 您也可以通过

为 script.sh 授予可执行权限
chmod 776 script.sh

nohup ./script.sh &

运行 作为

nohup sh ./script.sh &

文件 script.sh 根本不存在于您发出命令的目录中。
如果它确实存在并且不可执行,您将得到:
`nohup: 未能 运行 命令 './script.sh': 权限被拒绝

对于Linux上的每个新创建的脚本,您应该首先更改权限,因为您可以使用

查看权限详细信息
ls -lah

以下内容可能对您有所帮助:

#!/bin/bash

while [ 1 ];
do
date=`date`
pid=`ps -ef | grep "your process"  | grep -v grep | awk -F' ' '{print }'`
if [[ -n $pid ]]; then
    echo "$date - processID $pid is running."
else
    echo "$date - the process is not running"
    # script to restart your process
   say: start the process
fi

sleep 5m
done