LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 02-07-2010, 11:48 AM   #1
zee#
LQ Newbie
 
Registered: Feb 2010
Posts: 5

Rep: Reputation: 0
bash script - check process is running, if not start


I am using Sphinx search on my webserver and it quits after a certain amount of time leaving my search page broken.

Here is a bash script that I want to run every 10mins via cron:

Code:
#!/bin/bash
if pgrep searchd | grep "[0-9]" 
then
  donothing=0
else
  /usr/local/sphinx/bin/searchd
fi
I cannot get the syntax exactly right, where am I going wrong?

Cheers for any help.
 
Old 02-07-2010, 01:27 PM   #2
HasC
Member
 
Registered: Oct 2009
Location: South America - Paraguay
Distribution: Debian 5 - Slackware 13.1 - Arch - Some others linuxes/*BSDs through KVM and Xen
Posts: 329

Rep: Reputation: 55
try this:
Code:
#!/bin/bash

SPHINXPID=$(pgrep searchd)

if [[ -z "$SPHINXPID" ]]; then
  /usr/local/sphinx/bin/searchd
fi

unset SPHINXPID
Just ask if searchd is *not* running, then start it if that's the case.

Last edited by HasC; 02-07-2010 at 01:53 PM. Reason: Corrections for code
 
1 members found this post helpful.
Old 02-07-2010, 01:44 PM   #3
zee#
LQ Newbie
 
Registered: Feb 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks, I got this though (while searchd is running):
Code:
./start_searchd.sh: line 4: [: 13604: binary operator expected
./start_searchd.sh: line 8: unset: `13604': not a valid identifier
./start_searchd.sh: line 8: unset: `20434': not a valid identifier
 
Old 02-07-2010, 01:53 PM   #4
HasC
Member
 
Registered: Oct 2009
Location: South America - Paraguay
Distribution: Debian 5 - Slackware 13.1 - Arch - Some others linuxes/*BSDs through KVM and Xen
Posts: 329

Rep: Reputation: 55
Try again, did some corrections. Just some syntax errors, sorry

Last edited by HasC; 02-07-2010 at 01:56 PM.
 
Old 02-07-2010, 02:09 PM   #5
zee#
LQ Newbie
 
Registered: Feb 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Cheers, still nothing. if i run the script, it still isnt starting searchd.

no error messages this time.

I think i need to grep the output for any PID's, like this:

Code:
echo $variable | grep "[^0-9]" > /dev/null; echo $?
If the return value is 0, there is a non numeric charachter

I have tried a few times, but got nothing.

What does the '-z' in the if statement do, is that just "if not"?
 
Old 02-07-2010, 02:34 PM   #6
HasC
Member
 
Registered: Oct 2009
Location: South America - Paraguay
Distribution: Debian 5 - Slackware 13.1 - Arch - Some others linuxes/*BSDs through KVM and Xen
Posts: 329

Rep: Reputation: 55
First, the program gets the PID of searchd and stores it on SPHINXPID envvar. But if searchd isn't running, SPHINXPID will stay empty.

Then the program checks that $SPHINXPID is empty (that's the "-z" for, read "man bash"). If the envvar is empty (searchd isn't running), then the program starts it.
 
Old 02-07-2010, 03:09 PM   #7
zee#
LQ Newbie
 
Registered: Feb 2010
Posts: 5

Original Poster
Rep: Reputation: 0
yeh -z is for 0 length string, bt I think it might return one blank new line.

What else can I use instead of -z to check for any 5 digit number?

is it possible to return a 1 or 0 if the reg exp "[0-9]{5}" matches?
 
Old 02-07-2010, 03:41 PM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by zee# View Post
What else can I use instead of -z to check for any 5 digit number?
Nope. The process ID could be a 4-digit number as well. Indeed, the syntax of the original code is correct and the cause of the problem has to be searched somewhere else.

Looking at the sphynx documentation:
Quote:
searchd is not designed to be run either from a regular script or command-line calling, but instead either as a daemon to be called from init.d
this means that launching searchd as a command could not be the right way. Moreover it should require root privileges to act as a daemon.

More important, you should investigate why searchd stops if it is not supposed to do. If it crashes, it could even leave a lock behind, that prevent the system from running further sessions.

An aside note related to the code posted in the OP: you don't really need to grep for digits, since the if <command> syntax checks the exit status of the command and acts accordingly. Since pgrep returns 0 when find matches and 1 otherwise, this is enough. Moreover, you can reverse the expression using negation (an exclamation mark) so that you can avoid the "do-nothing" command:
Code:
if ! pgrep searchd > /dev/null
then
  <do something to restart the search daemon>
fi
Note the redirection of the output from pgrep to /dev/null. This is to avoid an unnecessary standard output that would be sent to you by mail from the cron daemon (if not redirected).
 
1 members found this post helpful.
Old 02-07-2010, 05:50 PM   #9
zee#
LQ Newbie
 
Registered: Feb 2010
Posts: 5

Original Poster
Rep: Reputation: 0
colucix thanks alot for explaining, was very helpful.
I did try the code you suggested but the if statement would not execute, regardless of wether or not searchd was running.

Added the searchd daemon to init.d, this article was useful:

http://serverfault.com/questions/861...-ubuntu-server

Thanks!

Last edited by zee#; 02-07-2010 at 05:52 PM.
 
  


Reply


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
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
Bash script to check for dead process MaffooClock Other *NIX 12 05-18-2007 05:14 AM
script to check if process is dead or running rspurlock *BSD 6 04-12-2004 11:32 PM
[script] check for a running process mikshaw Linux - Software 2 01-13-2004 08:33 PM


All times are GMT -5. The time now is 10:41 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