LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   req: Bash Script check if process command is running if not run it >> (http://www.linuxquestions.org/questions/linux-software-2/req-bash-script-check-if-process-command-is-running-if-not-run-it-4175441539/)

skate 12-15-2012 04:28 AM

req: Bash Script check if process command is running if not run it >>
 
I am looking for a bash script to look for a command if is running in the process tree, if not to run it.
I found lot of scripts over the net but all of them are for service/deamon, which is not working for me.
This is the command that I am running in a background:
ssh -f -N -L 8885:127.0.0.1:8887 some@host -p 22


for example in process tree:
user 7611 0.0 0.0 5048 628 ? S<s 12:18 0:00 ssh -f -N -L 8885:127.0.0.1:8887 some@host -p 22

Any help will be appreciated!

Thanks.

porphyry5 12-15-2012 11:21 AM

Quote:

Originally Posted by skate (Post 4849747)
I am looking for a bash script to look for a command if is running in the process tree, if not to run it.
I found lot of scripts over the net but all of them are for service/deamon, which is not working for me.
This is the command that I am running in a background:
ssh -f -N -L 8885:127.0.0.1:8887 some@host -p 22


for example in process tree:
user 7611 0.0 0.0 5048 628 ? S<s 12:18 0:00 ssh -f -N -L 8885:127.0.0.1:8887 some@host -p 22

Any help will be appreciated!

Thanks.

Something like
Code:

if ! ps aux|grep '...'; then ...; fi
where ... is your command

unSpawn 12-15-2012 11:42 AM

With 'pgrep' you can match whatever part of the process string you want (no need for ugly "ps|grep" kludges):
Code:

pgrep -f ':127.0.0.1:8887 some@host'
or the be precise:
Code:

pgrep -f 'ssh -f -N -L 8885:127.0.0.1:8887 some@host -p 22' >/dev/null 2>&1 ||\
 { [ -z $SSH_AGENT_PID ] || autossh -M 30000 -L8885:127.0.0.1:8887  -f -N host; }


skate 12-17-2012 02:13 AM

Thank you very much. Have a nice day :)


All times are GMT -5. The time now is 05:12 AM.