I installed recently Rocket.Chat on a server. The next day I checked it – the service was not running. So I wrote a small script that checks if the application port 3000 is in use and run another script if not.
The scrip is quite simple. The core utility nc is used to check the port availability and set the value for the parameter PORTCHECK depending on the state. Next it uses an if statement to just echo a message if the port is in use, otherwise it run the Rocket.Chat starting script.
Here is the code on that script /opt/rocketwatchdog.sh:
PORTCHECK=$(nc -z -w 3 localhost 3000; echo $?)
if [ “$PORTCHECK” = 0 ]; then
echo “RocketChat server is already running”
echo “$DATESTAMP App is running.” >> /var/log/watchdog.log
echo “RocketChat server is not running. Will try to start it.”
echo “$DATESTAMP RocketChat is not running, restart it.” >> /var/log/watchdog.log
You should add a cronjob that will check each minute is the server is running. Enter crontab -e to open cron jobs list, add at the end:
* * * * * /bin/sh /opt/rocketwatchdog.sh
Here is the script to start Rocket.Chat /opt/rocketchat.sh:
nohup /usr/local/bin/node main.js > /var/log/rocketchat.log 2>&1 &