Recently I encountered a situation where my mysql and nginx stopped and had to be restarted manually. It doesn't have a specific stop time, I checked and couldn't find the cause. So I need a cron command to run and check if mysql and nginx status is stopped and then turn it back on. Please help me do this.
Thank you!
How to set up automatic restart of nginx and mysql via cron?
- Edited
- Best Answerset by aaP_kienvictory
Hello, it should be shut down by the system. It is recommended that you check whether the server memory is sufficient.
Start command:
/etc/init.d/nginx start
/etc/init.d/mysqld start
- Edited
aaPanel_Kern
I don't know anymore, my Server uses 6cpu and 16gb of ram. I checked and saw that the CPU and RAM never exceeded the 30% threshold. Actually I can't find the cause of this, it doesn't happen often just occasionally so I need to set a cron command to run every minute to check and turn it back on if it's turned off.
When I run your command I get this message, I know this is not an error, but it shows up so does it have any effect?
Hello, I suggest you check the system log to see if there is any relevant shutdown information?
I currently see no record of shutting down nginx and mysql. Let me see if it registers next time.
aaPanel_Kern /www/wwwroot/domain.com/game.php I have to set cron in it, it is Openlitespeed server, I am very worried, no one is helping, does any of you know, please help.
- Edited
This Code work well
#!/bin/bash
Check MySQL server status
service_status=$(sudo /etc/init.d/mysqld status)
If the service is not running, restart it
if [[ $service_status == "stopped" ]]; then
echo "MySQL server is down. Restarting..."
sudo /etc/init.d/mysqld start
# Check the status again after attempting to start
if [[ $(sudo /etc/init.d/mysqld status) == *"running"* ]]; then
echo "MySQL server restarted successfully."
else
echo "Failed to restart MySQL server."
fi
else
echo "MySQL server is running."
fi
Better is to create a systemd service in such a way that if the service stopped it will auto restart. This is the best way to do.