How to run a shell script at startup

On an amazon linux instance, I have two scripts called start_my_app and stop_my_app which start and stop forever (which in turn run my node.js app). I use these scripts to manually start and stop my node app. 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 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.


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): https://wiki.debian.org/LSBInitScripts

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

Set a crontab for this

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

after every startup it will run the test script.


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 root :

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.

链接地址: http://www.djcxy.com/p/90030.html

上一篇: 如何正确杀死Mono程序

下一篇: 如何在启动时运行一个shell脚本