A few days ago found that the site is not open, prompted the database connection error, I log on to Linux found Mysqld services do not know when to stop. Luckily, I even found out.
So I decided to write a shell script to automatically detect the Mysqld service every minute, and start the MYSQLD service if the service is found.
That is: Execute the detection script through the Cron Timer task
Writing a test script/root/bin/check
The code is as follows |
Copy Code |
#!/bin/bash
check= ' pgrep MySQL '
If [-N "$check"]; Then Exit Else date=$ (date + "%y-%m-%d%h:%m:%s") /etc/init.d/mysqld start Echo ' Error:mysqld at ' $date Fi |
Ming: You can also modify check= ' pgrep MySQL ' such as check= ' Pgrep httpd ' or check= ' pgrep nginx ' to monitor other services
Edit Cron Execution
Crontab-e
The cron content is as follows, where a line is commented out and optionally written
#* * * * * sh/root/bin/check >/dev/null 2>&1 #执行后不发送结果邮件
* * * * * * Sh/root/bin/check >>/root/bin/log.txt #执行后发送结果到指定文件
Because a cron sends a message to/var/spool/mail/root for a long time after each execution if there is output, it can cause a lot of useless records in this file.
We can write with >/dev/null 2>&1 ignore results
Of course, you can use >>/root/bin/log.txt to write to the specified file (to create a good file beforehand)
Character:
Cron Command line brief description
Time-Sharing Day and Moon Week command line
Example: */5 * * * * cmd #表示每5分钟执行一次命令
Command > FileName redirect standard output to a new file
Command >> filename REDIRECT standard output to a file (append)
Command 1 > Fielname redirect standard output to a file
Command > FileName 2>&1 redirects the standard output to a file with standard errors
Command 2 > FileName redirect standard errors to a file
Command 2 >> filename REDIRECT standard output to a file (append)
Command >> filename 2>&1 redirects the standard output to a file (append) with the standard error
Command < filename >filename2 the command command to the filename file as the standard input to the filename2 file as the standard output
command < filename input command to filename file as standard
Command << delimiter reads from the standard input until it encounters the delimiter delimiter
Command <&m the file descriptor m as the standard input
Command >&m redirects standard output to the file descriptor m
Command <&-turn off the standard input
Command 2>&1 redirects the command command standard error to standard output
The following shell uses a while-do loop to check if the loader process is running and, if not, to start, so that the process of crashing the dead is restarted in time.
Two points must be noted:
1, PS |grep a process must add its road strength, otherwise easily grep to the wrong result;
2, must use-V to remove the grep command itself from the result, otherwise the result is not empty.
code is as follows |
copy code |
#!/bin/sh #===================== #YuanHui. He #khler @163.com #===================== While: do echo "Current DIR." $PWD stillrunning=$ (ps-ef |grep "$PWD/loader" |grep-v "grep") IF ["$stillR Unning "]; Then echo "TWS service is already started by another way" echo "Kill it D then startup by this shell, the other wise this shell would loop out this message annoyingly " kill-9 $pidof $PWD/loader Else echo "TWS service is not started" Echo "Starting service ..." $PWD/loader echo "TWS service was exited!" fi sleep Done |
If you start this shell and discover that the process already exists and that the process was started in a different way instead of the shell, it will continue to remind you to find the process, either by starting the service with this shell, or by killing the service that was started in a different way. That's what the above statement does:
Kill-9 $pidof $PWD/loader
Add a shell script to monitor Nginx
Nginx Although the number of concurrent processing than Apache really stronger, but its php-cgi model is not too stable, this online also has a friend summary, I also felt in the implementation project.
We have a payment machine, occasionally the following: php-cgi process suddenly disappeared, causing the PHP script can not be accessed; even more incredible is that php-cgi opened there are two ports in the listening, inexplicable its seconds suddenly have a php-cgi port was closed, Causing all requests to be on one port,
The result is a PHP script access exception.
Basically, I wrote a solution to monitor the shell script, which automatically restores the Nginx service, regardless of the situation. The script runs normally on a production environment:
The code is as follows |
Copy Code |
#!/bin/bash # # filename:webservermonitor.sh # function: Monitor the Nginx php-cgi is normal # Author: Brother V # Run: webservermonitor.sh & # # php-cgi listening IP and port v_php_cgi_port= "127.0.0.1:9000 127.0.0.1:9001" # nginx Restart Script v_nginx= "/usr/local/nginx/sbin/restart.sh" # log file V_log= "/tmp/webservermonitor.log" # function Definition: Restart Nginx function Restart_nginx () { echo "-----' date '-----' >> $V _log echo "------------------" >> $V _log echo "' PS aux |grep ' nginx '" >> $V _log echo "------------------" >> $V _log echo "' PS aux |grep ' php-cgi '" >> $V _log echo "------------------" >> $V _log echo "' Netstat-nlpt | grep ' php-cgi ' ">> $V _log echo "------------------" >> $V _log $V _nginx >> $V _log } # Circular execution, not using crontab, the state is Crontab min unit is minutes, time is too long While: Todo # 1: First detect if the Nginx main process exists V_nginx_num= ' PS Axu |grep ' NGINX ' |grep-v ' grep ' |wc-l ' If [$V _nginx_num-lt 1];then Restart_nginx Continue Fi # 2: Re-check php-cgi if there is a process exists V_php_cgi_num= ' PS Axu |grep ' php-cgi ' |grep-v ' grep ' |wc-l ' If [$V _php_cgi_num-lt 1];then Restart_nginx Continue Fi # 3: Again to determine whether the port is normal For PORT in $V _php_cgi_port Todo v_num= ' eval ' netstat-nlpt | grep ' ${port} ' | Wc-l "' If [$V _num-lt 1];then Restart_nginx Continue Fi Done # hibernate Sleep 5 Done |
PS: Of course, now some servers also provide methods like Dnspod to provide a domain name monitoring function, you can monitor whether the server is normal OH.