I want to run nohup node service using cron job on CentOS 7. For this I created a file /home/guest/start_indexjs mentioned below
#!/bin/bash
if pgrep -f "index.js" >/dev/null; then
echo "index.js is Running."
else
echo "index.js is Stopped."
nohup node /root/demo/index.js > /root/index-nohup.log &
fi
I login with ssh root and run following command:
cd /home/guest/
chown root start_indexjs
chgrp -R root start_indexjs
chmod +x start_indexjs
I want to run /home/guest/start_indexjs
every 1 minute. For this I added the following line into /etc/crontab
:
*/1 * * * * root /home/guest/start_indexjs > /var/log/start_indexjs.log
I created a /var/log/start_indexjs.log
with following permission
-rwxrwxr-x 1 root root 179 Aug 12 12:31 start_indexjs
then restart crond service using below command
systemctl restart crond.service
After all this my cron job running but when /home/guest/start_indexjs > /var/log/start_indexjs.log
running in cron job my "index.js" not running there is no process is running with index.js using following command
ps -ef | grep index.js