Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save renatoargh/f84d1c3884d6c59a3a1b to your computer and use it in GitHub Desktop.

Select an option

Save renatoargh/f84d1c3884d6c59a3a1b to your computer and use it in GitHub Desktop.

Revisions

  1. renatoargh renamed this gist Feb 18, 2015. 1 changed file with 9 additions and 2 deletions.
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,22 @@
    Restarting Your Node.js App at Reboot
    # Restarting Your Node.js App at Reboot

    Cronjob can prevent your application and your users from unexpected downtimes.

    Create a file called starter.sh in your application's home folder and copy the following code:

    ```bash
    #!/bin/sh

    if [ $(ps -e -o uid,cmd | grep $UID | grep node | grep -v grep | wc -l | tr -s "\n") -eq 0 ]
    then
    export PATH=/usr/local/bin:$PATH
    forever start --sourceDir /path/to/your/node/app main.js >> /path/to/log.txt 2>&1
    fi
    ```

    where main.js should be replaced with your application's main script.
    Where main.js should be replaced with your application's main script.

    Don't forget to make `starter.sh` executable with `chmod +x starter.sh`

    This useful snippet has been taken from here

    @@ -21,7 +25,10 @@ To start this script at each reboot you need to edit the crontab with this comma
    crontab -e
    and append the following code to this file

    ```bash
    @reboot /path/to/starter.sh
    ```

    Now set the absolute path to your starter.sh file.

    Tip: Navigate where your starter.sh file is located and print the current directory with pwd.
  2. renatoargh created this gist Nov 27, 2014.
    31 changes: 31 additions & 0 deletions Restarting Your Node.js App at Reboot with Crontab.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    Restarting Your Node.js App at Reboot

    Cronjob can prevent your application and your users from unexpected downtimes.

    Create a file called starter.sh in your application's home folder and copy the following code:

    #!/bin/sh

    if [ $(ps -e -o uid,cmd | grep $UID | grep node | grep -v grep | wc -l | tr -s "\n") -eq 0 ]
    then
    export PATH=/usr/local/bin:$PATH
    forever start --sourceDir /path/to/your/node/app main.js >> /path/to/log.txt 2>&1
    fi

    where main.js should be replaced with your application's main script.

    This useful snippet has been taken from here

    To start this script at each reboot you need to edit the crontab with this command:

    crontab -e
    and append the following code to this file

    @reboot /path/to/starter.sh
    Now set the absolute path to your starter.sh file.

    Tip: Navigate where your starter.sh file is located and print the current directory with pwd.

    Repeat the steps above for each of your domains/services.

    ///backed up form https://www.digitalocean.com/community/tutorials/how-to-host-multiple-node-js-applications-on-a-single-vps-with-nginx-forever-and-crontab