-   Linux - Newbie (
-   -   bash script - check process is running, if not start (

zee# 02-07-2010 11:48 AM

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:


if pgrep searchd | grep "[0-9]"

I cannot get the syntax exactly right, where am I going wrong?

Cheers for any help.

HasC 02-07-2010 01:27 PM

try this:


SPHINXPID=$(pgrep searchd)

if [[ -z "$SPHINXPID" ]]; then


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

zee# 02-07-2010 01:44 PM

Thanks, I got this though (while searchd is running):

./ line 4: [: 13604: binary operator expected
./ line 8: unset: `13604': not a valid identifier
./ line 8: unset: `20434': not a valid identifier

HasC 02-07-2010 01:53 PM

Try again, did some corrections. Just some syntax errors, sorry

zee# 02-07-2010 02:09 PM

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:


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"?

HasC 02-07-2010 02:34 PM

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.

zee# 02-07-2010 03:09 PM

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?

colucix 02-07-2010 03:41 PM


Originally Posted by zee# (Post 3855966)
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:

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:

if ! pgrep searchd > /dev/null
  <do something to restart the search daemon>

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).

zee# 02-07-2010 05:50 PM

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:


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