LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 12-15-2012, 04:28 AM   #1
skate
Member
 
Registered: Aug 2003
Location: Bulgaria
Distribution: OpenSuse 10.3, Debian 4.0r3 "Etch", FreeBSD 7.1, Ubuntu
Posts: 210

Rep: Reputation: 30
Post 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.
 
Old 12-15-2012, 11:21 AM   #2
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch
Posts: 421

Rep: Reputation: 18
Quote:
Originally Posted by skate View Post
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
 
Old 12-15-2012, 11:42 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,264
Blog Entries: 54

Rep: Reputation: 2841Reputation: 2841Reputation: 2841Reputation: 2841Reputation: 2841Reputation: 2841Reputation: 2841Reputation: 2841Reputation: 2841Reputation: 2841Reputation: 2841
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; }
 
2 members found this post helpful.
Old 12-17-2012, 02:13 AM   #4
skate
Member
 
Registered: Aug 2003
Location: Bulgaria
Distribution: OpenSuse 10.3, Debian 4.0r3 "Etch", FreeBSD 7.1, Ubuntu
Posts: 210

Original Poster
Rep: Reputation: 30
Thank you very much. Have a nice day
 
  


Reply

Tags
bash, process, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python/Bash script to check conditions for running "poweroff" command gnuub Programming 3 12-04-2012 03:31 AM
Script to check PID from file and check whether process is running or not rajkiran183 Linux - Newbie 5 10-19-2012 11:28 AM
[SOLVED] bash script - check process is running, if not start zee# Linux - Newbie 8 02-07-2010 05:50 PM
Bash script to check if process is running and send email depam Linux - Newbie 2 04-08-2009 12:11 AM
Obtain ip address and check for running process via Bash Script? xconspirisist Programming 10 09-12-2008 01:18 PM


All times are GMT -5. The time now is 06:47 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration