How not to test that mysqld is alive
code.openark.org
code.openark.org
Still, I think they're doing it wrong. For keeping daemons alive I rely on http://cr.yp.to/daemontools.html .
If you use daemontools, this code is already written for you.
It's obvious now, that regexp does not match, for example, the "]".
It works because the text to match is no longer included in the command line invoked to do the matching. Whether or not 'grep [m]ysqld' appears in the list produced by ps is irrelevant, as [m]ysqld does not match "m]ysqld". i.e. the 'm' matches, but then the match fails when it hits ']'.
tl;dr It's just a short way of matching "mysqld" without the matching pattern itself being "mysqld".
If I found someone doing this on one of my production database machines, I think I'd have to dig out the paintball gun and start chasing them around, especially if the person run around with the title of sysadmin. (I'd cut a slight bit of slack if they were a webdev.)
Anyone who's ever done 'ps | grep' will have noticed that the grep sometimes appears, sometimes doesn't.
Course you could do 'ps | grep | grep -v grep'
EDIT:
test.sh:
for i in {1..1000}
do
ps | grep grep | wc -l
done
./test.sh | sort | uniq -c 13 0
987 1
Which would indeed suggest 1.3%. Likely dependent on how quickly you're running it, if it's in memory already etc etc /etc/init.d/mysql start
instead of "restart," their script would have worked fine. The spurious starts when mysqld was already started would have been ignored.Fortunately this is easy:
$ echo "select now();" | mysql
now()
2009-10-05 19:24:22run: mysqladmin ping
output: mysqld is alive