Run script on startup, shutdown or reboot on Ubuntu
Isn’t there always a need to run some command or program at startup? It’s easy on Windows with it’s startup folder, but what about on a Linux server? What’s the proper method to run a script at startup on Ubuntu?
Linux has different run levels, each designating a different user group with certain permissions. Some are exclusive for system processes, while others are available to regular users and admins. The file containing startup commands and the one we look at in this example is update-rc.d.
Run script on startup on Ubuntu
You can use update-rc.d for start-only or stop-only scripts, following these steps:
Start script called “startup_script” on startup (note the dot at the end of the line) :
# update-rc.d -f startup_script start 99 2 3 4 5 .
– start is the argument given to the command (start, stop).
– 99 is the start priority of the script (1 = first one, 99= last one)
– 2 3 4 5 are the runlevels at which to run the script
The dot at the end of the line has significance, read more here: /etc/rcS.d/README
Start startup_script on shutdown and reboot : # update-rc.d -f startup_script start 90 0 6 .
Stop startup_script on halt and reboot : # update-rc.d -f startup_script reboot 90 0 6 .
To run the script as a daemon, use the skeleton file located at “/etc/init.d/skeleton”
To know which runlevel you are running, type: $ runlevel
Read more about runlevels here.