0

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
fedorqui
  • 275,237
  • 103
  • 548
  • 598
user3441151
  • 1,880
  • 6
  • 35
  • 79
  • does the script run stand alone? That is, does it work when you execute manually `/home/guest/start_indexjs`? (would be a good idea to add a .sh extension to the script, so that it is more clear that it is so). – fedorqui Aug 12 '16 at 08:31
  • Also, it may happen that `pgrep` matches itself. – fedorqui Aug 12 '16 at 08:36
  • @fedorqui yes script run stand alone. it work when you execute manually "/home/guest/start_indexjs" – user3441151 Aug 12 '16 at 09:03
  • Could you also clarify the last sentence _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_? It is a bit unclear what is happening. – fedorqui Aug 12 '16 at 10:02
  • @fedorqui When my "*/1 * * * * root /home/guest/start_indexjs > /var/log/start_indexjs.log" run. After that there is no process is running with "index.js" – user3441151 Aug 12 '16 at 10:05
  • @fedorqui /home/guest/start_indexjs file could not start my "nohup node /root/demo/index.js > /root/index-nohup.log &" when this file execute with crontab. – user3441151 Aug 12 '16 at 10:07
  • Uhms, this is strange. I would debug by doing something like `touch /tmp/sample_file` in the `if` condition to see if it enters there. Also, you may want to use `crontab -e` to set crontab instead of writing to `/etc/crontab` directly. It is safer – fedorqui Aug 12 '16 at 10:25
  • @fedorqui please suggest me how can i run "nohup node /root/demo/index.js > /root/index-nohup.log &" command using bash/shell script. – user3441151 Aug 12 '16 at 12:14
  • I would give a try to `nohup node /root/demo/index.js &` alone. If it works, then add the redirect to the log file. Also interesting to check [Node.js as a background service](http://stackoverflow.com/q/4018154/1983854) – fedorqui Aug 12 '16 at 12:19
  • So did you find out what is the issue? – fedorqui Aug 24 '16 at 14:19
  • @fedorqui No, I did not find any issue. If you get any issue then please help me out to fix this. – user3441151 Aug 25 '16 at 06:10

0 Answers0