CdSdw
CdSdw

Reputation: 359

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!

Upvotes: 3

Views: 14317

Answers (2)

shekhar swami
shekhar swami

Reputation: 11

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

ssh [email protected] -p 22 -t "pgrep -fl while.sh"

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

Upvotes: 1

matteo martelli
matteo martelli

Reputation: 136

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

Upvotes: 8

Related Questions