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 19:52

You can do it :

chmod +x PATH_TO_YOUR_SCRIPT/start_my_app 

then use this command

update-rc.d start_my_app defaults 100

Please see this page on Cyberciti.

查看更多
孤独寂梦人
3楼-- · 2018-12-31 19:53

Set a crontab for this

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

after every startup it will run the test script.

查看更多
若你有天会懂
4楼-- · 2018-12-31 19:57

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.

查看更多
余生请多指教
5楼-- · 2018-12-31 19:58

This simple solution worked for me on an Amazon Linux instance running CentOS. Edit your /etc/rc.d/rc.local file and put the command there. It is mentioned in this file that it will be executed after all other init scripts. So be careful in that regards. This is how the file looks for me currently.enter image description here. Last line is the name of my script.

查看更多
临风纵饮
6楼-- · 2018-12-31 19:59

for some people this will works You could simply add the following command into System > Preferences > Startup Applications:

bash /full/path/to/your/script.sh
查看更多
浅入江南
7楼-- · 2018-12-31 20:02

Just have a line added to your crontab..

Make sure the file is executable:

chmod +x /path_to_you_file/your_file

To edit crontab file:

crontab -e

Line you have to add:

@reboot  /path_to_you_file/your_file

That simple!

查看更多
登录 后发表回答