I am new to Shell scripting on Linux and would like to write a shell script that runs every two hours, checks if there is a service running on port 3001 and if not, restarts the service, in this case a Node.js application with Gulp.
I need this shell script to run every 2 hours in order to check if the application is still running and if not, restart the application, because the Virtual Server environment, in which the application is running, is unstable and shuts down from time to time for no reason.
So the Shell script must contain the command "gulp serve". It should run in the folder of the Node application and act as if a person would enter the command "gulp serve" by command line in the project folder. What would be the best way to do that? Any hints and comments would be appreciated, thanks!
This is what I have found so far as a starting point:
#!/bin/bash ###edit the following service=Node.js firstname.lastname@example.org ###stop editing host=`hostname -f` if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 )) then echo "$service is running" else /etc/init.d/$service start if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 )) then subject="$service at $host has been started" echo "$service at $host wasn't running and has been started" | mail -s "$subject" $email else subject="$service at $host is not running" echo "$service at $host is stopped and cannot be started!!!" | mail -s "$subject" $email fi fi