How to run a shell script at startup

forever, init.d, linux, node.js

Solution

The file you put in `/etc/init.d/` have to be set to executable with:

chmod +x /etc/init.d/start_my_app

As pointed out by @meetamit, if it still does not run you might have to create a symbolic link to the file in `/etc/rc.d/`

ln -s /etc/init.d/start_my_app /etc/rc.d/

Please note that on the latest versions of Debian, this will not work as your script will have to be LSB compliant (provide at least the following actions: start, stop, restart, force-reload, and status): https://wiki.debian.org/LSBInitScripts

As a note, you should always use the absolute path to files in your scripts instead of the relative one, it may solve unexpected issues:

/var/myscripts/start_my_app

Finally, make sure that you included the shebang on top of the file:

#!/bin/sh

Problem

On an Amazon S3 Linux instance, I have two scripts called `start_my_app` and `stop_my_app` which start and stop forever (which in turn runs my Node.js application). I use these scripts to manually start and stop my Node.js application. So far so good. My problem: I also want to set it up such that `start_my_app` is run whenever the system boots up. I know that I need to add a file inside `init.d` and I know how to symlink it to the proper directory within `rc.d`, but I can't figure out what actually needs to go inside the file that I place in `init.d`. I'm thinking it should be just one line, like, `start_my_app`, but that hasn't been working for me.

Original source