检查进程是否正在运行Linux

检查进程是否正在运行Linux,linux,process,Linux,Process,这是我的密码: #!/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:无法运行命令'/script.sh':没有这样的文件

这是我的密码:

#!/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:无法运行命令'/script.sh':没有这样的文件或目录


有什么问题吗?

请确保您的脚本保存为script.sh 以及从script.sh所在的同一目录执行nohup./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 $2}'`
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

我以nohup./script.sh运行它&它说nohup:failed to run command`./script.sh':没有这样的文件或目录,所以我想知道我的代码是错误的还是没有说明明显的问题,但是您的脚本实际名为
script.sh
?它是否存在于当前目录中?您想在这里做什么
ps cax | grep testing>/dev/null
这似乎是在搜索文件名中包含单词
testing
的进程,但结果被丢弃。实际上,我以前已经执行过上述命令,但它似乎给出了相同的结果建议使用符号文件访问模式,rathen比古老的和容易出错的数字更重要。例如,
chmod+x script.sh
更具可读性且不易出错。