Check if remote process is running (linux)

11,388

Solution 1

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

Solution 2

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
Share:
11,388
CdSdw
Author by

CdSdw

Updated on June 04, 2022

Comments

  • CdSdw
    CdSdw almost 2 years

    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!