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:


###edit the following
###stop editing

host=`hostname -f`
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
echo "$service is running"
/etc/init.d/$service start
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
subject="$service at $host has been started"
echo "$service at $host wasn't running and has been started" | mail -s "$subject" $email
subject="$service at $host is not running"
echo "$service at $host is stopped and cannot be started!!!" | mail -s "$subject" $email

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.