Reply To: Free / Very Cheap VPS

spitfire
Participant

Horizon: 0.00 HZ
11th October 2014 at 8:03 am

“coinfused, post: 3352, member: 824” wrote: the thing I am looking for is realtime monitoring. since the app is java based, it crashes sometimes on lower end machines unexpectedly. I need the process to be restarted right away, so I dont have the hallmark down for a few hours. autostart scripts at boot time im good at :) Thank you

You can also set up a cron job to handle starting the server if it’s not running, that’s what I’m doing. I have it run every 10 minutes and if it’s not running it’ll restart. Here’s the code that I’m using and how to use it:

1. create a file called “nhzcron.sh” and open it in any text editor, if creating or editing the file in windows you need to make sure you remove the windows line endings or it won’t run. You can do this using vi or dos2unix in linux and google can easily point you in the right direction.

2. Inside the file add the following code making sure to change the path to your nhz ./run.sh

Code:

'.str_replace('
', '', '
#!/bin/bash
. /etc/profile
if /bin/ps aux | /bin/grep ./run.sh | /bin/grep -v grep
then
echo "NHZ server already running"
else
echo "NHZ server about to start"
cd /your/absolute/path/to/nhz/
./run.sh >/dev/null 2>&1
fi
').'

Note: the >/dev/null 2>&1 will hide the console output from being put into a log file if you log your cron job. Definitely handy as the log file could get pretty big if it logs output all the time. Instead, the log file will only log if the server is running and it’s pid or if it’s about to start the server.

3. Upload the file to your server taking note of the absolute path to the file.

4. ssh into server and type “chmod +x nhzcron.sh” to make your script executable (if you edited in windows be sure to run “dos2unix nhzcron.sh” or any other method to remove the windows line endings)

5. Type “crontab -e” into your ssh terminal

6. add the following line make sure to change the path to the absolute path noted above

'.str_replace('
', '', '
*/10 * * * * sh /your/absolute/path/to/nhzcron.sh >> /absolute/path/to/log/file.log 2>&1
').'


Note the */10 will run it every 10 minutes to check if the server is running changing this to */5 will run every 5 minutes, and so on.

That’s it, if everything is set right the cron should run @ 10, 20, 30, 40 ,50, 00 of the hour or whatever time frame you specify. And auto start upon server restart/startup