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.
In the file you put in
/etc/init.d/
you have to set it executable with:Thanks to @meetamit, if this does not run you have to create a symlink to
/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:
And don't forget to add on top of that file:
Enter
cron
usingsudo
: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:
Run your test script without cron to make sure it actually works.
Make sure you saved your command in cron, use
sudo crontab -e
Reboot the server to confirm it all works
sudo @reboot
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:
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
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.
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)
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 forchkconfig
. Example, the following script is used to start a java application as user oracle.The name of the script is
/etc/init.d/apex
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,and you can use service start/stop apex