How to run a shell script at startup

2018-12-31 19:51发布

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 runs 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.

15条回答
像晚风撩人
2楼-- · 2018-12-31 20:13
  • Add your script to /etc/init.d/ directory
  • Update your rc run-levels: $ update-rc.d myScript.sh defaults NN where NN is the order in which it should be executed. 99 for example will mean it would be run after 98 and before 100.
查看更多
千与千寻千般痛.
3楼-- · 2018-12-31 20:16

Another option is to have an @reboot command in your crontab.

Not every version of cron supports this, but if your instance is based on the Amazon Linux AMI then it will work.

查看更多
伤终究还是伤i
4楼-- · 2018-12-31 20:18

For Debian 9 see https://askubuntu.com/questions/228304/how-do-i-run-a-script-at-start-up. It is helped me. Short version for Debian 9: add commands (as root) to /etc/rc.local

/path_to_file/filename.sh ||  exit 1   # Added by me
exit 0

Probably, /path_to_file/filename.sh should be executable (I think so).

查看更多
登录 后发表回答