Search code examples
node.jsubuntucronforever

Make Node Server Restart Proof


What I have now is a node server (with forever.js in Ubuntu 14.04 LTS) which is needed to start each time server PC starts.

So, to do that what we need is this command (every time PC start or restarts)-

forever start /var/www/websocket/websocket.js

If I run this command in direct command line, it works fine.

But I want it automatically start when server PC turn on or restart.

So what I have done (according to https://stackoverflow.com/a/13388741/2193439 ) is-

Run crontab -e and put this code in the console-

@reboot forever start /var/www/websocket/websocket.js

like this-

enter image description here

And to check my corn log, I have done this-

sudo grep --color -i cron /var/log/syslog

And found something like this-

enter image description here

But I am finding the server is not running by this-

forever list

and having - No forever processes running

enter image description here

But if I run this-

forever start /var/www/websocket/websocket.js

And then run this-

forever list

Then I am having this-

enter image description here

And I am confirming you that crontab is also running because if I change this-

@reboot forever start /var/www/websocket/websocket.js

To this-

@reboot cd /var/www/websocket/ && touch cron_try.txt

I am having the file each time PC restarts.


I have already tried this-

  1. Automatically start forever (node) on system restart
  2. Automatically restart node server
  3. http://www.hacksparrow.com/make-forever-reboot-proof-with-cron.html
  4. https://github.com/foreverjs/forever/issues/58

And this-

cronjob does not execute a script that works fine standalone

Is almost my problem. But I had set it during reboot and for Node forever.js.

So it does not solve my problem.


Can anyone please help?


Solution

  • I have solved this by this way-

    First, find the forever location by this-

    which forever
    

    And get this-

    /usr/local/bin/forever
    

    And then put this path in crontab like this-

    @reboot /usr/local/bin/forever start /var/www/websocket/websocket.js
    

    And we are done :)