Monitor Servers and Reboot SolusVM via Cronjob Bash Script

UPDATE: I’ve created a tutorial on how to do this with a serverless Lambda function, which is a much more robust option. View the tutorial here.

I’ve been looking for a way to monitor my various VPS web servers and to automatically reboot them if something goes wrong but I was never able to find a simple solution. I’ve used Monit — but at times it has failed to keep my servers up and running. So I’ve created my own bash script to do the job and thought I would share it.

First, you will obviously need a seperate VPS you can use to monitor your other VPS servers (I’m using an easy TurnKey Linux on mine which is Debian-based so you may need to modify the script based on what you are running).

Then, create API credentials in SolusVM for each of the servers you would like to monitor:

You’ll then need to copy the API KEY and the API HASH that is generated for each server so you can use it in the bash script we will create to run in crontab.

Next, connect to the server you are going to use to monitor your other servers via SSH and create a new bash script (you can name it whatever you want, for example: monitor.sh) somewhere on your server — preferably not somewhere that is exposed to the wider internet (a good place might be in your etc folder)

Then open the file with your favorite command line editor and copy and paste this script into the file (I prefer using nano to edit files but you can use whatever you’d like), and edit the script to match your VPS information as well as the location of your server’s SolusVM API’s command.php, etc. (you can ask your host for this location).

Save the script and then change the permission on the script so only the owner can run it (read it and edit it):

Then you can test the script to make sure everything is working (try shutting down one of the servers you want to monitor and then run the script to make sure everything is working) by running:

Once you varify the script is working, you can then create a new cronjob to run the script at various intervals (maybe every 15 minuites) to ensure your servers will keep up and running.

Personally, I like using nano as my editor rather than vim — so if you are like me you can run this command so you can edit the cronjob in nano:

Otherwise if you are fine with the default editor you can just use:

And add this line (edit it to match the script’s location and how often you want to run the job — in this example, every 15 minutes).

And there you have it!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store