I want to run nohup node service
using a 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 minute. For this I added the following line into /etc/crontab
:
*/1 * * * * root /home/guest/start_indexjs > /var/log/start_indexjs.log
I created /var/log/start_indexjs.log
with the following permissions:
-rwxrwxr-x 1 root root 179 Aug 12 12:31 start_indexjs
Then I restarted 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
run, in cron job, my index.js
didn't run. There is no process running with index.js
using following command:
ps -ef | grep index.js