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:


if pgrep -f "index.js" >/dev/null; then
echo "index.js is Running."
echo "index.js is Stopped."
nohup node /root/demo/index.js > /root/index-nohup.log &

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

1 Answer 1

  • In order to run script from crond you have to provide full path to all binaries, which are pgrep and node in your current case
  • nohup is not necessary. crond runs scripts without allocating terminal.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.