1 votes

mysql cron job - Email uniquement au redémarrage

J'ai créé la tâche cron suivante, qui s'exécute toutes les 5 minutes (et qui fonctionne)

#!/bin/bash
PATH=/usr/sbin:/usr/bin:/sbin:/bin
if [[ "$(/usr/sbin/service mysql status)" != *start/running* ]]
then
    echo "MySQL restarted" | mail -a FROM:*@*  -s "[Marketing Server] Database Service" *@*
    sudo service mysql start
fi

Lors de l'exécution de mysql status (running)

mysql.service - LSB: Start and stop the mysql database server daemon Loaded: loaded (/etc/init.d/mysql) Active: active (exited) since Wed 2015-12-09 15:01:40 GMT; 21h ago Docs: man:systemd-sysv-generator(8) Process: 30829 ExecStop=/etc/init.d/mysql stop (code=exited, status=0/SUCCESS) Process: 30910 ExecStart=/etc/init.d/mysql start (code=exited,status=0/SUCCESS)

Dec 10 12:10:02 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:15:02 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:20:02 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:25:01 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:30:02 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:35:01 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:40:01 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:45:02 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:50:02 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:55:01 MarketingServer systemd[1]: Started LSB: Start and stop the .... Hint: Some lines were ellipsized, use -l to show in full.

lors de l'exécution de mysql status (stopped)

david@MarketingServer:~$ sudo service mysql stop ^[[Adavid@MarketingServer:~$ sudo service mysql status mysql.service - LSB: Start and stop the mysql database server daemon Loaded: loaded (/etc/init.d/mysql) Active: inactive (dead) since Thu 2015-12-10 13:03:26 GMT; 1s ago Docs: man:systemd-sysv-generator(8) Process: 5024 ExecStop=/etc/init.d/mysql stop (code=exited, status=0/SUCCESS) Process: 30910 ExecStart=/etc/init.d/mysql start (code=exited, status=0/SUCCESS)

Dec 10 12:35:01 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:40:01 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:45:02 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:50:02 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 12:55:01 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 13:00:02 MarketingServer systemd[1]: Started LSB: Start and stop the .... Dec 10 13:03:24 MarketingServer systemd[1]: Stopping LSB: Start and stop the.... Dec 10 13:03:24 MarketingServer mysql[5024]: * Stopping MySQL database serve...d Dec 10 13:03:26 MarketingServer mysql[5024]: ...done. Dec 10 13:03:26 MarketingServer systemd[1]: Stopped LSB: Start and stop the .... Hint: Some lines were ellipsized, use -l to show in full.

Le seul problème que j'ai est qu'il envoie un email indiquant que le service a été redémarré à chaque fois, même si ce n'est pas le cas. Je veux qu'il n'envoie que lorsque le service a réellement redémarré après s'être arrêté.

Quelqu'un peut-il m'expliquer quelle partie je me suis trompée ?

1voto

Arronical Points 18815

Il y a quelques problèmes dans votre déclaration "if". L'instruction ! au début du test de condition signifie que vous cherchez à savoir si l'expression "$(/usr/sbin/service mysql status)" est fausse, ce qui n'est pas le but recherché. Vous voulez vérifier que le résultat de la commande service status n'est pas une chaîne contenant "start/waiting". != est la meilleure façon de dire "pas égal à".

En outre, le =~ attend une expression régulière, dans laquelle vous fournissez simplement une partie de la chaîne que vous voulez voir. Comme la syntaxe des doubles crochets des instructions if prend en charge la globalisation Shell, vous pouvez rechercher le 'start/waiting' avec *start/waiting* .

Si vous modifiez la ligne :

if [[ ! "$(/usr/sbin/service mysql status)" =~ "start/running" ]]

A :

if [[ "$(/usr/sbin/service mysql status)" != *start/running* ]]

Votre script devrait fonctionner correctement, sinon, je pense que vous redémarrez le service, et que vous envoyez des e-mails, à chaque fois que le script s'exécute.

EDITAR:

Ce qui suit n'est pas une solution idéale, je le poste juste pour essayer d'aider dans la situation actuelle que vous rencontrez.

Pour tenter de faire face à la production de systemd-sysv-generator vous pouvez essayer de remplacer la première ligne du if avec :

if [[ "$(/usr/sbin/service mysql status)" = *inactive* ]]

SistemesEz.com

SystemesEZ est une communauté de sysadmins où vous pouvez résoudre vos problèmes et vos doutes. Vous pouvez consulter les questions des autres sysadmins, poser vos propres questions ou résoudre celles des autres.

Powered by:

X