How to check and restart service automatically in vps
Last updated
In certain cases, it is not uncommon for us to meet suddenly one serve (for example: Apache, Nginx, MySQL etc.) suddenly down or stopped because of some things that cause our applications or websites to be down and inaccessible.
Many things that cause why the service is down among them is due to a large traffic or overload on the server. Of course, the best way to solve this is to do debugging and solve root issues. However, because we have not found the root issue or do not know the solution, it is not uncommon we have to activate the services manually.
How to activate a down manual service
Activating a down service is very simple by starting or restarting the service. For example, if we want to activate the Apache service on Centos 7, then we can run the command:
systemctl start httpd
Or it could be with
systemctl start apache2
For MySQL, we can also activate it by running the command:
systemctl start mysql
Etc. However, this method must be done by logging in to the server and running it manually so that it will not infrequently become a problem if we are not in front of our laptop or computer.
Activate the down service automatically
To activate service automatically, we can use Cron assistance by checking the service status every minute or a certain period according to our needs and then activating the service if it is down.
To do this, run the following command to create a new cron:
crontab -e
Next, add the following command line:
* * * * * systemctl status httpd > /dev/null || systemctl start httpd
The example above means that we will check every minute the status of the Apache Service (HTTPD) and if it is down, the system will activate the HTTPD or Apache service.
If you want to change it to five (5) minutes, then we can change it to:
*/5 * * * * systemctl status httpd > /dev/null || systemctl start httpd
Command || (OR) The point is that if the status of httpd down or failed or the value is above 0, then the next command, namely the start of httpd will be continued.
This command is the opposite of && which will only execute the next command if the first command is above 0 or in this case successfully executed.
Easy isn’t it, so that we don’t need to panic if our mysql or apache is suddenly down regularly.
Hope it is useful.
Game Center
Game News
Review Film
Rumus Matematika
Anime Batch
Berita Terkini
Berita Terkini
Berita Terkini
Berita Terkini
review anime
Comments are closed, but trackbacks and pingbacks are open.