Start a service or execute a script on boot on Ubuntu/Fedora

I have a couple of servers with different Linux distributions (Ubuntu/Fedora) on my hands, which normally work continuously well. However, from time to time they reboot as they are hosted in a shared farm of virtual servers. It was in the first of those reboots when we noticed that neither Apache, neither MySQL servers were automatically launched on start-up, which should be the default configuration in an environment like ours.

So, I’d like to add another new trick to my the knowledge pool, which I expect to be useful for others as well: how to configure scripts and services to run after the system boots?

Run levels

First of all, we need to know something about run levels in Linux systems. The numbers and their meaning differ from one system to another, but their main purpose remains:

  1. Single-user mode
  2. Multi-user mode
  3. Multi-user mode with networking
  4. Reserved for special purposes
  5. Adds display manager to level 3

Thus, the ‘normal’ run level we should use in a server environment is 3, which starts all the services, but does not care about X11 – which makes sense. This can be a first source of error if the default run level is not correctly selected, or it is tweaked to mimic another one which is the one that should be used instead. Anyway, to change this parameter we can go through: /etc/inittab and edit the line


After verifying this setting, we can go for the unit configurations for every service on every level.

Services, daemons and servers

We can use the tool sysv-rc-conf under Ubuntu to easily mark or unmark the installed services that should run automatically with every run level. Normally, these configurations are correct, but it is no harm to double check, right? Under Fedora we can use chkconfig for the same purpose.

Custom scripts and applications

There is still another way to configure the services and software that we want to be auto-executed on system start. Under /etc/rcX.d (where X is the run level) there are some simbolic links to all the commands, scripts and applications that should run with every run level. Then, an advanced way to configure the servers that we want to launch with the machine, or even better, the custom scripts that we want to execute with a wide range of purposes (maintenance, a specific folder mount, whatever), is to create a symbolic link under one of these folders.

To create the link, the naming Sxx or Syy must be respected, where xx and yy are numbers which complement 100 (like K36mysqld and S64mysqld). The number is the order in which the command will be executed, and the letter (well ‘S’, well ‘K’) tells the system whether is a Start or a Kill action, that’s to say, if the command specified should be executed when entering or when exiting the selected run level.

As an example:

/etc/rc3.d/S99custom_mount -> /etc/init.d/


There are other possibilities to get the same result, like editing /etc/rc.local file, where we can write the commands we want to execute when the system has finished booting. Taking a look to the run level bootstrap default configuration, we may find:

/etc/rc3.d/S99rc.local -> ../init.d/rc.local

Which means that the commands stated in this file will be executed at the very end of the system load in run level 3, that’s to say, after MySQL, Apache and the rest of servers and services. Despite the simplicity of this alternative, I prefer the one exposed above, since it gives a full control on what is being executed, where, and when. And, of course, it gives a better comprehension of Linux environments, which is one of the primary targets of this article.

Did you enjoy it? Leave a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *