Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to run a shell script at startup

On an Amazon S3 Linux case, I have two scripts called start_my_app and stop_my_app what start and stop everlastingly (which thus runs my Node.js application). I utilize these scripts to manually begin and stop my Node.js application. No issues up until now.

My concern: I additionally need to set it up with the end goal that start_my_app is run at whatever point the system boots up. I realize that I need to add a file inside init.d and I know how to symlink it to the appropriate directory inside rc.d, yet I can't sort out what quite to go inside the record that I place in init.d. I'm figuring it ought to be only one line, as, start_my_app*, however that hasn't been working for me.
by

3 Answers

akshay1995
In the file you put in /etc/init.d/ you have to set it executable with:

chmod +x /etc/init.d/start_my_app

Thanks to @meetamit, if this does not run you have to create a symlink to /etc/rc.d/

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

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

As a note, you should put the absolute path of your script instead of a relative one, it may solves unexpected issues:

/var/myscripts/start_my_app

And don't forget to add on top of that file:

#!/bin/sh
RoliMishra
Set a crontab for this

#crontab -e
@reboot /home/user/test.sh

after every startup it will run the test script.
pankajshivnani123
A simple approach is to add a line in /etc/rc.local :

/PATH/TO/MY_APP &
or if you want to run the command as a special user :

su - USER_FOOBAR -c /PATH/TO/MY_APP &
(the trailing ampersand backgrounds the process and allows the rc.local to continue executing)

If you want a full init script, debian distro have a template file, so :

cp /etc/init.d/skeleton /etc/init.d/your_app
and adapt it a bit.

Login / Signup to Answer the Question.