
다음 크론 작업이 있고 5분마다 실행됩니다(실제로 작동합니다).
#!/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
mysql 상태(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.
mysql 상태를 실행할 때 (중지됨)
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.
내가 가진 유일한 문제는 서비스가 다시 시작되지 않은 경우에도 매번 서비스가 다시 시작되었다는 이메일을 보낸다는 것입니다. 서비스가 실제로 중지되었다가 다시 시작된 경우에만 전송하고 싶습니다.
내가 어느 부분을 잘못했는지 설명해줄 수 있는 사람 있어?
답변1
if 문에 몇 가지 문제가 있습니다. 조건 테스트의 시작 부분은 !
표현식이 "$(/usr/sbin/service mysql status)"
거짓인지 여부를 찾고 있음을 의미하며 이는 달성하려는 결과가 아닙니다. 서비스 상태 명령의 결과가 '시작/대기'를 포함하는 문자열이 아닌지 확인하고 싶습니다. !=
같지 않다고 말하는 가장 좋은 방법입니다.
또한 에서는 =~
표시될 것으로 예상되는 문자열의 일부만 제공하는 정규식을 기대합니다. if 문의 이중 괄호 구문은 쉘 글로빙을 지원하므로 를 사용하여 'start/waiting'을 검색할 수 있습니다 *start/waiting*
.
행을 변경하는 경우:
if [[ ! "$(/usr/sbin/service mysql status)" =~ "start/running" ]]
에게:
if [[ "$(/usr/sbin/service mysql status)" != *start/running* ]]
스크립트가 제대로 작동해야 합니다. 그렇지 않으면 스크립트가 실행될 때마다 서비스를 다시 시작하고 이메일을 보내는 것 같습니다.
편집하다:
다음은 이상적인 해결책이 아니며, 현재 직면한 상황에 도움을 드리고자 게시한 것입니다.
출력에 대처하려면 systemd-sysv-generator
명령문의 첫 번째 줄을 다음과 같이 바꿔 보십시오 if
.
if [[ "$(/usr/sbin/service mysql status)" = *inactive* ]]