In bash: Determine if MySQL is running?

Problems with the Mac OS X version of XAMPP, questions, comments, and anything related.

In bash: Determine if MySQL is running?

Postby caltuna » 10. May 2009 16:50

I have a cron script (run as root) that turns on MySQL, does a reload of a database from a dump file (downloaded from my server several times a day,) and closes MySQL.

Code: Select all
#=== XAMP part of code
/Applications/XAMPP/xamppfiles/xampp startmysql >/dev/null
sleep 60
/Applications/XAMPP/xamppfiles/bin/mysql -u root -h localhost sugar < /users/al/documents/sugar.bak
sleep 60
/Applications/XAMPP/xamppfiles/xampp stopmysql  >/dev/null


Is there a way to determine if MySQL is already running, and if so, do not try to start it OR stop it? Anyone here a bash scripting guru?

Why do I want to do this? Sometimes I'm running XMAP when this backup procedure starts and I don't want to close MySQL if it was running before the script started. Trying to start is while running is no problem. But closing it while I'm using it in the foreground is not good. So I want to do a simple status check and not open or close it if it is already going. I run this script five or six times a day... sometimes I've got XAMP open... sometimes not. If the script runs when I'm working on XAMPP and it closes MySQL, boy to I get a ton of errors on the next database read or write!! It wakes you up quickly!! (Of course all I have to do is turn it back on with the XAMPP control... but it would be nice not to have to.)

Thanks,

ANC
caltuna
 
Posts: 111
Joined: 05. May 2009 16:35

Re: In bash: Determine if MySQL is running?

Postby make-fun » 19. May 2009 00:14

On a Linux you can use
Code: Select all
nmap 127.0.0.1 | grep mysql | wc -l
which will return 1 if MySQL is running. Should be close to the MAC syntax I hope.

Cheers
make-fun
 
Posts: 31
Joined: 11. March 2009 04:21

Re: In bash: Determine if MySQL is running?

Postby caltuna » 19. May 2009 00:30

There is an nmap for the Mac. I'll have to download it and give it a try. I thought there might be a low-overhead way to grep ps -aux with mysql in an IF statement, but my bash scripting is mostly forgotten. Even if you don't have mysql running you still get a "hit:

Code: Select all
al:~ al$ ps -aux |grep mysql
al        4823   0.0  0.0     8796     80  p1  R+    4:34PM   0:00.00 grep mysql


There was a time when I could figure a bash solution out... but that boat sailed years ago!

Thank you.
caltuna
 
Posts: 111
Joined: 05. May 2009 16:35

Re: In bash: Determine if MySQL is running?

Postby make-fun » 19. May 2009 00:58

Oh well for me it's the MAC days that lie in the past ;-)

How about a
Code: Select all
top -b -n 1 | grep mysql

or do a
Code: Select all
ps aux |grep mysql | grep -v "grep mysql"
make-fun
 
Posts: 31
Joined: 11. March 2009 04:21

Re: In bash: Determine if MySQL is running?

Postby caltuna » 19. May 2009 05:17

Code: Select all
ps -aux |grep mysql | grep -v "grep mysql"


... will work just fine. Thanks.

You traded a Mac for Linux and after about five years running Linux, I traded Linux for Mac... which I look at as the "better Linux"... although it comes at a steep price!

Thanks again.
caltuna
 
Posts: 111
Joined: 05. May 2009 16:35

Re: In bash: Determine if MySQL is running?

Postby make-fun » 19. May 2009 05:46

Cool, you're welcome

Just a note on my post…
Code: Select all
ps -aux |grep mysql | grep -v "grep"
will do

And I forgot to mention this one:
Code: Select all
cat /var/run/mysqld/mysqld.pid | wc -l
as every app creates a pid you should be able to use this as an indication—not sure on the MAC again…

Cheers & good luck
make-fun
 
Posts: 31
Joined: 11. March 2009 04:21

Re: In bash: Determine if MySQL is running?

Postby caltuna » 19. May 2009 15:10

Code: Select all
#!/bin/sh
t=$(/bin/ps -aux | /usr/bin/grep  mysql | /usr/bin/grep -v "grep mysql")
x=""
if [ "$t" != "$x" ] ; then
# running: do not open or close close mysql
else
#not running: open it or close it here
fi


The above should work. I've not tried it with mysql but it works for "lookupd" which is always running on the Mac. It is a bit convoluted, but my bash scripting is a thing of my long-ago past! The Mac with XMAMPP is offline right now... should be back up later today or tomorrow and I can report back.

The "pid" file idea with the wc (word count) command (built in with the Mac) might be a better but I'm not sure where XAMPP hides the pids... but it won't be hard to find out. I'm surprised there isn't a mysql command to arrive at the status of the server instead of having to hack the system to figure it out!

Thanks for your help.

Al
caltuna
 
Posts: 111
Joined: 05. May 2009 16:35

Re: In bash: Determine if MySQL is running?

Postby make-fun » 19. May 2009 23:33

Semms like I'm a bit rusty myself…

Try a:
Code: Select all
pidof mysqld
and see if it returns a pid…

Then your code could look like this
Code: Select all
#!/bin/sh
t=$(pidof mysqld)
if [ $t != "" ] ; then
echo IS running: do not open or close close mysql
### Your Code
else
echo NOT running: open it or close it here
### Your Code
fi


Maybe I should get a MAC in a VM—feel like I lost touch :)

Cheers
make-fun
 
Posts: 31
Joined: 11. March 2009 04:21

Re: In bash: Determine if MySQL is running?

Postby caltuna » 19. May 2009 23:39

No such command with Mac's OS X. They give you a lot of them, but not all of them. I'm sure I could find one somewhere and compile it. Or I could do this: http://www.afp548.com/article.php?story ... 6094324710
but I keep away from messing with profiles and stuff.

ANC
caltuna
 
Posts: 111
Joined: 05. May 2009 16:35

Re: In bash: Determine if MySQL is running?

Postby skor » 31. May 2009 02:34

How about:
Code: Select all
/Applications/XAMPP/xamppfiles/bin/mysqladmin status
skor
 
Posts: 2
Joined: 31. May 2009 02:30

Re: In bash: Determine if MySQL is running?

Postby caltuna » 07. June 2009 06:56

skor wrote:How about:
Code: Select all
/Applications/XAMPP/xamppfiles/bin/mysqladmin status


How would I test that since if it not running all I get back is:

Code: Select all
/Applications/XAMPP/xamppfiles/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock' exists!


Maybe I could grep for something? My bash scripting is long gone!

Al
caltuna
 
Posts: 111
Joined: 05. May 2009 16:35


Return to XAMPP for Mac OS X

Who is online

Users browsing this forum: No registered users and 6 guests