CdSdw CdSdw - 4 months ago 11
Linux Question

Check if remote process is running (linux)

I have the following code that will check if a script (named

my_script.sh
) is running:

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.

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!

Answer

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.

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

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