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

时间:2022-02-24 16:24:16

I have the following code that will check if a script (named my_script.sh) is running:

我有以下代码来检查脚本(名为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

However, I'd like to modify this to check if the process is running on another machine, without ssh'ing directly, since this breaks me out of the current script if I'm running it in a loop.

但是,我想要修改它来检查进程是否在另一台机器上运行,而不直接使用ssh,因为如果我在循环中运行它,这将使我脱离当前的脚本。

In essence, I'd like to do this:

本质上,我想这样做:

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

Any help would be appreciated. Thanks!

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

2 个解决方案

#1


2  

I am not sure this is what you are looking for but you can execute the check command in the remote machine direcly inside a ssh call. The return value should correspond to the return value of the command invoked in the remote machine.

我不确定这是否是您正在寻找的,但是您可以在ssh调用中直接在远程机器中执行check命令。返回值应该与远程机器中调用的命令的返回值相对应。

Of course you need a passwordless authentication method enabled (e.g. ssh keys).

当然,您需要启用无密码身份验证方法(例如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

#2


0  

This is simple solution to check program is running or not on remote location.

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

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

ssh root@192.168.22.12 - p22 -t“pgrep -fl while.sh”

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

#1


2  

I am not sure this is what you are looking for but you can execute the check command in the remote machine direcly inside a ssh call. The return value should correspond to the return value of the command invoked in the remote machine.

我不确定这是否是您正在寻找的,但是您可以在ssh调用中直接在远程机器中执行check命令。返回值应该与远程机器中调用的命令的返回值相对应。

Of course you need a passwordless authentication method enabled (e.g. ssh keys).

当然,您需要启用无密码身份验证方法(例如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

#2


0  

This is simple solution to check program is running or not on remote location.

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

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

ssh root@192.168.22.12 - p22 -t“pgrep -fl while.sh”

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