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

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
查看更多
墨雨无痕
3楼-- · 2018-12-31 20:03

Enter cron using sudo:

sudo crontab -e

Add a command to run upon start up, in this case a script:

@reboot sh /home/user/test.sh

Save:

Press ESC then :x to save and exit, or hit ESC then ZZ (that's shift+zz)

Test Test Test:

  1. Run your test script without cron to make sure it actually works.

  2. Make sure you saved your command in cron, use sudo crontab -e

  3. Reboot the server to confirm it all works sudo @reboot

查看更多
余欢
4楼-- · 2018-12-31 20:04

Create your own /init executable

This is not what you want, but it is fun!

Just pick an arbitrary executable file, even a shell script, and boot the kernel with the command line parameter:

init=/path/to/myinit

Towards the end of boot, the Linux kernel runs the first userspace executable at the given path.

Several projects provide popular init executables used by major distros, e.g. systemd, and in most distros init will fork a bunch of processes used in normal system operation.

But we can hijack /init it to run our own minimal scripts to better understand our system.

Here is a minimal reproducible setup: https://github.com/cirosantilli/linux-kernel-module-cheat/tree/f96d4d55c9caa7c0862991025e1291c48c33e3d9/README.md#custom-init

查看更多
长期被迫恋爱
5楼-- · 2018-12-31 20:07

if you want to put startup also you can use

first of all move your script /etc/init.d then chmod 777 /etc/init.d/your script name

after apply following command

update-rc.d your script defaults remove update-rc.d -f your script remove

at the startup you can see your app will run.

查看更多
公子世无双
6楼-- · 2018-12-31 20:10

The absolute easiest method if all you want to run is a simple script, (or anything) is if you have a gui to use system > preferences then startup apps.

just browse to the script you want and there you go. (make script executable)

查看更多
旧人旧事旧时光
7楼-- · 2018-12-31 20:12

This is the way I do it on red-hat systems

Put your script in /etc/init.d, owned by root and executable. At the top of the script, you can give a directive for chkconfig. Example, the following script is used to start a java application as user oracle.

The name of the script is /etc/init.d/apex

#!/bin/bash
# chkconfig: 345 99 10
# description: auto start apex listener
#
case "$1" in
 'start')
   su - oracle -c "cd /opt/apex ; java -jar apex.war > logs/apex.log 2>logs/apex_error.log &";;
 'stop')
   echo "put something to shutdown or kill the process here";;
esac

this says that the script must run at levels 3, 4 and 5 and the priority for start/stop is 99 and 10.

then, as user root you can use chkconfig to enable or disable the script at startup,

chkconfig --list apex
chkconfig --add apex

and you can use service start/stop apex

查看更多
登录 后发表回答