question
monitor service with bash
introduce
On the server, our services may be hung up for a variety of reasons, such as mongodb Out of memory, and the system is kill, resulting in the service can not work properly. So we need a simple monitoring script check_service.sh
to view the status of our service in real time. If the service state is stopped, we can try to restart the service automatically.
The script can accept a parameter, which is the name of the service. The name of the service can use the service command to view, start and stop the state. For example, check the status of the MySQL service.1
2$bash /home/shiyanlou/check_service.sh mysql
Is Running
If the MySQL service is not running, the MySQL service is started and the following output information is printed:1
2$bash /home/shiyanlou/check_service.sh mysql
Restarting
If the service does not exist, the error information is output:1
2$bash /home/shiyanlou/check_service.sh notfoundservice
Error: Service Not Found
target
- The script is named
check_serive.sh
, and the path must be/home/shiyanlou/check_service.sh
- The script can
accept a parameter
, the parameter is the name of the service, and the check_service.sh service name can be called in this way
- If the service is running “is Running”, if the service is stopped, the service is started, and if the service does not exist, output error information
/home/shiyanlou/check_service.sh mysql
is put into crontab once a day, which can ensure that MySQL service can be restarted when it is hung up, and we need to manually start cron service.
Hint
- You can use
sudo service xxx start/status/stop
for service management - Use of command line position parameters
- Service status information use can be judged by grep
Knowledge point
- Bash process control
- The control of service and process
- Crontab
keypoint
1 |
|
1 |
|
view the process
ps -ef
grep -v
exclude the grep processwc -l
count row numnetstat -at|grep mysql
pstree
show the derived relation between the proceses in a tree diagram way
start crontab
sudo service rsyslog start
sudo cron -f &