检查远程进程是否为 运行 (linux)

Check if remote process is running (linux)

我有以下代码将检查脚本(名为 my_script.sh)是否为 运行:

ps cax | grep my_script.sh > /dev/null
if [ $? -eq 0 ]
then
    echo "Process is running."
else
    echo "Process is not running."
fi

但是,我想修改它以检查进程是否在另一台机器上 运行,而不是直接通过 ssh,因为如果我 运行 它在一个循环中。

本质上,我想这样做:

ssh otherMachine
ps cax | grep my_script.sh > /dev/null
if [ $? -eq 0 ]
then
    echo "Process is running."
else
    echo "Process is not running."
fi
ssh originalMachine

如有任何帮助,我们将不胜感激。谢谢!

我不确定这是否是您要查找的内容,但您可以在 ssh 调用中直接在远程计算机中执行检查命令。 return 值应对应于在远程计算机中调用的命令的 return 值。

当然,您需要启用无密码身份验证方法(例如 ssh 密钥)。

ssh otherMachine "ps cax | grep my_script.sh > /dev/null"
if [ $? -eq 0 ]
then
    echo "Process is running."
else
    echo "Process is not running."
fi

这是检查程序是否 运行 或不在远程位置的简单解决方案。

ssh root@192.168.22.12 -p 22 -t "pgrep -fl while.sh"

if [ $? -eq 0 ]; then echo "Process is running."; else   echo "Process is not running."; fi