I want to write a script for restart MySQL service whenever it is in stopped status. I don't know how to write the IF statement on this script.
Asked
Active
Viewed 2,189 times
2 Answers
2
@vastlysuperiorman
if [ ! $(ps aux | grep mysql) ] then service mysql restart fi
First, don't forget to protect your operand. See this reminder : bash : Illegal number
Then, you could simplify by :
[[ $(ps -ef | grep [m]ysql) ]] || service mysql restart
(I prefere to use ps -ef
because it's more POSIX compliant even if I always use [[
instead of test
or [
in Bash).
Finaly, it's better to check the status with service mysql status
. You could try something like instead :
[[ $(service mysql status) =~ running ]] || service mysql restart
@user33398
You could try to use this test in a loop or with watch
command for example. Or you could use more smart tools like cron
or jenkins
or nagios
to do that.

Community
- 1
- 1

Idriss Neumann
- 3,760
- 2
- 23
- 32
-
-
@user33398 Thank you to copy/paste the command that fails, I am not psychic ;) – Idriss Neumann Apr 26 '14 at 07:37
-
1@IdrissNeumann, `ps aux | grep mysql` will always return true. Instead what you need is `ps aux | grep [m]ysql`. Run both the commands and you will find the difference. – slayedbylucifer Apr 26 '14 at 07:45
-
Dear IDRISS NEUMAN! if [ ! $(ps aux | grep mysql) ] this one doesnot work and even if MySQL be stopped it shows MYSQL is there since GREP MYSQL is a process that contains MySQL within it. Dear SLAYEDBYLUCIFER ,[m]ysql is the correct one. i 'm going to check it – user33398 Apr 26 '14 at 08:28
-
@user33398 I said that is better to check the status of mysql with `[[ $(service mysql status) =~ running ]] || service mysql restart` instead ;) (it always works). – Idriss Neumann Apr 26 '14 at 09:06
-
@slayedbylucifer Try to run `echo "$(ps aux|grep mysql)"` intead of `ps aux|grep mysql` and you'll find the difference ;) . And `[[ string ]]` doesn't check if the result is true but if it's not empty (it's an equivalent of `-n` option of `test`). But I like your tip so I upvote :) – Idriss Neumann Apr 26 '14 at 09:10
-
1@IdrissNeumann, you are mistaken. Run these two commands and you will notice the difference: `if [[ $(ps -ef | grep mysql) ]] ; then echo hello; fi` **AND** `if [[ $(ps -ef | grep [m]ysql) ]] ; then echo hello; fi`. The 1st command will **ALWAYS** print `hello` because the if condition turns true always. Besides, waht you have mentioned in the comment above also follows what I am explaining here. – slayedbylucifer Apr 26 '14 at 11:16
-
@slayedbylucifer I've edited my answer, you can see the result of these commands in my OS ;) – Idriss Neumann Apr 26 '14 at 11:28
-
@slayedbylucifer You're right. I've tried on a second OS (mac OS with bash) and the process that corresponds of the `grep` command is returned in my `echo "$(ps-ef | grep mysql)"`. Do you know how to explain that difference of behavior ? Thanks in advance. – Idriss Neumann Apr 26 '14 at 11:42
-
Which Shell are you on? What is the shell version? Mine is `GNU bash, version 4.2.25(1)-release (i686-pc-linux-gnu)`. I tried it on RHEL and Ubuntu and it acted the way I mentioned above. So, My responses were only with respect to BASH. – slayedbylucifer Apr 26 '14 at 11:45
-
@slayedbylucifer The first test was done on an old Ubuntu server with GNU/bash 3.2.39. The second (which behaves as you describe) on Mac OS with GNU/bash 4.2.45. – Idriss Neumann Apr 26 '14 at 11:47
-
Hmmm.. I dont have a 3.2.39 at my disposal. May be, that what causing different behaviour. Anyway, A good practice is to upgrade the bash version (OR Ubuntu in a broader perspective). Also please update your answer accordingly as OP also probably running 4.2.25 version of BASH as he has mentioned in one the comments that the `ps ...` command always returns true. – slayedbylucifer Apr 26 '14 at 11:55
-
@slayedbylucifer Anyway, to be compatible between the different versions of Bash, it's better to do as you done ... (but for this case I still prefer to use `service mysql status` as in my answer. It still works in all cases). So I've corrected my post, thanks. – Idriss Neumann Apr 26 '14 at 11:57
0
No sense re-inventing the wheel. There are great software solutions out there that can do this for you. Monit is a good open source example.
If you do opt to write a script, a simple solution could be to do something like the following:
if [ ! $(ps aux | grep mysql) ]
then
service mysql restart
fi

vastlysuperiorman
- 1,694
- 19
- 27
-
`ps aux | grep mysql` will always return true. Instead what you need is `ps aux | grep [m]ysql`. Run both the commands and you will find the difference. – slayedbylucifer Apr 26 '14 at 07:46
-
1This is true. The grep would show up in the list. I'm going to have to advocate Idriss Neuman's answer on this one. He's right that `[[ $(service mysql status) =~ running ]] || service mysql restart` is probably the most effective solution (I'm new so I can't upvote). – vastlysuperiorman Apr 26 '14 at 13:53