Organizational Research By

Surprising Reserch Topic

How To Configure a Linux Service to Start Automatically After a Crash or Reboot

Step 1 ‚ÄĒ Connecting to Your Debian 6 Droplet

Now we'll run through a practical example, using MySQL.

From the DigitalOcean control panel, create a Debian 6.0 x64 Droplet with 1 GB of RAM.

Once the Droplet has been initialized, use SSH to connect to the server (Windows users can connect using a tool like PuTTY).

ssh sammy@your_server_ip
In the following instructions, we assume your account has sudo privileges.

Step 2 ‚ÄĒ Installing MySQL

We'll use MySQL as our test service. Execute the next command to install MySQL Server:

sudo apt-get install mysql-server -y
A graphical screen like the one shown below will appear asking for a new root password. Provide that:

Provide a root password for MySQL

Repeat the password in the next prompt:

Repeat root password at prompt

Press ENTER to confirm.

Lines will scroll by as MySQL is installed. Once the installation completes, run the following command to harden your installation:

This will ask for the current root password. Press N to keep the same password. Then, press Y to remove the anonymous user, disable remote root login, and remove the test database. Finally, press Y to reload the privilege tables.

Our MySQL installation should now be complete.

To check if the service is running, execute this command:

service mysql status
The output will show a few lines of information, one of which will show how long the MySQL service has been running (uptime).


/usr/bin/mysqladmin Ver 8.42 Distrib 5.1.73, for debian-linux-gnu on x86_64 . . . Uptime: 4 days 18 hours 58 min 27 sec Threads: 1 Questions: 18 Slow queries: 0 Opens: 15 Flush tables: 1 Open tables: 8 Queries per second avg: 0.0.

Step 3 ‚ÄĒ Configuring MySQL to Auto-start After Reboot

By default, MySQL is already set to start after a reboot.

You should see this symlink to MySQL's init script in the /etc/rc2.d directory. Note that you should NOTtry to create these symlinks manually; use the update-rc.d command to enable and disable services.

ls -l /etc/rc2.d

lrwxrwxrwx 1 root root 15 Jul 31 07:09 S02mysql -> ../init.d/mysql

As long as there is an S script under the default runlevel directory for the service, init will start the service when the server boots.

So, MySQL should be running. Now it's time to verify that it will auto-start at boot time. Reboot the machine with this command:

sudo reboot
Once the server comes back online, connect to it with SSH.

Run the service mysql status command again. Again, the service will be shown as running. This means the service automatically starts when the operating system boots.

Not all services will be like this, though. In those cases we will have to manually configure the services for auto-restart. For Debian, the update-rc.d command lets you add (or remove) services to be automatically started at boot.

Let's disable the MySQL service and then see how to re-enable it for auto start. Let's run this command to disable MySQL:

sudo update-rc.d mysql disable
To test, reboot the server again.

sudo reboot
Connect to your server with SSH.

Try to connect to MySQL using the MySQL client tool:

mysql -u root -p
You will receive this message:


ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Re-enable the service:

sudo update-rc.d mysql enable
The output will be:


update-rc.d: using dependency based boot sequencing

If you are running a CentOS system with System V, the commands will use chkconfig rather than update-rc.d.

Note that enabling a service for auto-start at boot time does not automatically start it if it is stopped. To start MySQL, run this command:

sudo service mysql start
Step 4 ‚ÄĒ Configuring MySQL to Auto-start After Crash

Now that our service is running again, let's see if it automatically starts after a crash. With System V, it willNOT come up automatically by default.

We will emulate a crash by killing the process abruptly. Find its process ID by executing the following command:

ps -ef | grep mysql
The output will be similar to this:


root 1167 1 0 07:21 pts/0 00:00:00 /bin/sh /usr/bin/mysqld_safe mysql 1292 1167 0 07:21 pts/0 00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/ --socket=/var/run/mysqld/mysqld.sock --port=3306 root 1293 1167 0 07:21 pts/0 00:00:00 logger -t mysqld -p daemon.error root 1384 1123 0 07:21 pts/0 00:00:00 grep mysql

The main processes that run MySQL are mysqld_safe and mysqld. mysqld_safe is the parent process ofmysqld.

In our example here, we can see they have process IDs 1167 and 1292 respectively. Their process numbers are highlighted in red above.

Let's emulate a crash with a kill -9 command. Make sure to use your own process IDs:

sudo kill -9 1167
sudo kill -9 1292
Check the service status:

sudo service mysql status
The output will show that the service has stopped:


asked Aug 30, 2015 in LINUX by rajesh
0 votes

Related Hot Questions

2 Answers

0 votes
MySQL is stopped..

Now that our service has crashed, how do we bring it up? Of course we can restart it, but that would be a manual process; we want restarting to be automatic. To make MySQL auto restart after a crash, we have to edit the /etc/inittab file.

We will talk about the /etc/inittab file in greater detail in Part 2, but for now, let's understand that it's the first file System V init reads when booting up.

Among other things, /etc/inittab decides how a process should behave if it crashes. For some processes it's meant to respawn the service again. We need to ensure MySQL is among those services. So let's make a copy of it first:

sudo cp /etc/inittab /etc/inittab.orig
A note of caution: be extremely careful when editing the /etc/inittab file. If you make a mistake in your commands or delete any existing configuration, the system may not come up when you reboot it.

Open /etc/inittab with a text editor:

sudo nano /etc/inittab
At the end of the file, add this line:


ms:2345:respawn:/bin/sh /usr/bin/mysqld_safe
So what are we doing here?

Well, we are putting a command in the /etc/inittab file to respawn the mysqld_safe process when it crashes. It has four fields, each one separated from the other by a colon (:).

ms: The first two characters specify an id for the process.
2345: The second field specifies the runlevels it's supposed to apply to. In this case, it's for runlevels 2, 3, 4, and 5
respawn: The third field specifies the action (we are respawning the service)
/bin/sh /usr/bin/mysqld_safe: Finally, the fourth field is the process (the command to execute)
We'll come back to the /etc/inittab file in more detail in Part 2, and see how it helps with auto-starting MySQL after a crash. For now, save the file and exit the editor.

Start the service again:

sudo service mysql start
Reboot the server so the change takes effect:

sudo reboot
Now, repeat the commands to locate the process numbers, kill the processes, and check the status again, starting with ps -ef | grep mysql.

Wait for five minutes or so and then execute the command:

sudo service mysql status
You should see that this time, MySQL has started automatically after the crash.

That's it! MySQL will now automatically start after a service crash or system reboot.

Auto-starting Services with Upstart

Upstart is another init method, which was first introduced in Ubuntu 6. It became the default in Ubuntu 9.10, and was later adopted into RHEL 6 and its derivatives. Google's Chrome OS also uses Upstart.

Ubuntu 9.10 to Ubuntu 14.10, including Ubuntu 14.04
CentOS 6
While it's going strong on the current LTS version of Ubuntu (14.04 at the time of writing), it's being phased out everywhere in favor of systemd, which we cover in the last section.

Upstart is better than System V in handling system services, and it's also easy to understand. However, we will not dive deeply into Upstart in this part of the tutorial; there is a very good tutorial on Upstart in the DigitalOcean community.

Today we will primarily focus on Upstart configuration files and see how to use them to auto-start services.

Upstart uses configuration files to control services. The files are under the /etc/init directory. The files have plain text content with easy-to-read sections called stanzas. Each stanza describes a different aspect of the service and how it should behave.

By default, most standard applications you can install, such as Nginx or MySQL, will start after reboot and also start after a crash by default, so you don't have to do anything to make this work. They will come with their own init scripts in /etc/init already.

For custom applications, you will have to set this up yourself. To learn how to create your own custom Upstart script, read the introductory Upstart tutorial referenced earlier.

Auto-start Checklist for Upstart

This section is a quick reference to make sure your service is set to automatically start.

Configuration Checklist

Make sure the service has a functional Upstart init script located at /etc/init/service.conf
The /etc/init/service.conf file should contain a line like start on runlevel [2345] to enable automatic starting after a reboot
The /etc/init/service.conf file should also contain a line like respawn to enable the service to respawn after a crash
Make sure there is no override file for the service: /etc/init/service.override
(There would be one only if you or another admin made one earlier)

Stop, then start, the service:
sudo initctl stop service
sudo initctl start service
Reboot the server.
sudo reboot

To test that these are working, you can:

Reboot the server, then verify that the service is up
Search for the process number:
ps -ef | grep service
Kill the process:
sudo kill -9 process_number
Within a few seconds, verify that the service is back up
answered Aug 30, 2015 by rajesh
0 votes

Step 1 ‚ÄĒ Connecting to Your CentOS 7 Droplet

We'll use CentOS 7 and MySQL to show you how to configure systemd services.

Create a Droplet with 1 GB of RAM and choose CentOS 7 x64 as its base image.

Use SSH to connect to the server (Windows users can connect using a tool like PuTTy).

  • ssh sammy@your_server_ip

We assume you are using an account with sudo privileges.

Step 2 ‚ÄĒ Installing MySQL

Run the following commands to download and install the MySQL Community server repo:

  • sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm

Install MySQL server:

  • sudo yum install mysql-server -y

Once the installation completes, start the mysqld service.

(Note that this is not the same as when we installed MySQL under Debian and Ubuntu. In the other distributions, MySQL started automatically.)

Also, we are using a new systemd command called systemctl to control the service:

  • sudo systemctl start mysqld

Next, run the mysql_secure_installation command.

  • mysql_secure_installation

In this case the MySQL root password will be empty, so you should choose to create a new password. Provide the same answers to the other prompts as you did when installing under Debian or Ubuntu (see the earlier Debian section for details).

Step 3 ‚ÄĒ Configuring MySQL to Auto-start After Reboot

By default, MySQL is configured to start automatically after a reboot. Let's look at how this works.

To check whether the mysqld daemon was configured to start automatically at boot time, execute thesystemctl command with the is-enabled option:

  • sudo systemctl is-enabled mysqld.service

The result will be:




Let's reboot the machine:

  • sudo reboot

When the server comes back up, connect to it with SSH.

Execute the following command to check the service status:

  • sudo systemctl status mysqld.service

The output will show the service is running:



mysqld.service - MySQL Community Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled) Active: active (running) since Fri 2015-07-31 21:58:03 EDT; 1min 52s ago Process: 662 ExecStartPost=/usr/bin/mysql-systemd-start post (code=exited, status=0/SUCCESS) Process: 625 ExecStartPre=/usr/bin/mysql-systemd-start pre (code=exited, status=0/SUCCESS) Main PID: 661 (mysqld_safe) ...

To disable the service, run the following command:

  • sudo systemctl disable mysqld.service

This will not stop the service, but disable it. In fact, the output shows the symbolic links have been deleted:



rm '/etc/systemd/system/' rm '/etc/systemd/system/mysql.service'

If you'd like, you can reboot the server and test again to see if the MySQL server is running (it won't be).

Or, execute systemctl is-enabled again; we should get a response of disabled:

  • sudo systemctl is-enabled mysqld.service



Enable the service again:

  • sudo systemctl enable mysqld.service

The output will show the symlinks being recreated:



ln -s '/usr/lib/systemd/system/mysqld.service' '/etc/systemd/system/mysql.service' ln -s '/usr/lib/systemd/system/mysqld.service' '/etc/systemd/system/'

Your service will automatically start after a reboot.

Step 4 ‚ÄĒ Configuring MySQL to Auto-start After Crash

Now we will see how MySQL is configured to auto-start after a crash.

First, open the mysqld.service unit file in an editor (remember, systemd services use unit files for configuration):

  • sudo nano /etc/systemd/system/

At the end of the file, there is a directive for restart:





The value of the Restart parameter is set to always. This means MySQL service will restart for clean or unclean exit codes or timeouts.

That's where an automatic restart is defined in systemd.

Just like the respawn directive in Upstart, the Restart parameter in systemd defines how the service should behave if it crashes.

Not all systemd services have this capability enabled by default; to make a service come up after a crash, all we have to do is to add this extra directive under the [Service] section of the service unit file. If the section header does not exist, we have to add the [Service] header too.

To emulate a crash, first exit the editor and then check the MySQL process ID:

  • sudo systemctl status mysqld.service


mysqld.service - MySQL Community Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled) Active: active (running) since Fri 2015-07-31 21:58:03 EDT; 1h 7min ago Main PID: 661 (mysqld_safe) ...

Kill this process with a kill -9 signal. In our case the main PID is 661; replace the PID with your own:

  • sudo kill -9 661

Check the status:

  • sudo systemctl status mysqld.service

The output will show that MySQL has restarted with a new PID (in our case the new Process ID is 11217):



mysqld.service - MySQL Community Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled) Active: active (running) since Fri 2015-07-31 23:06:38 EDT; 1min 8s ago Process: 11218 ExecStartPost=/usr/bin/mysql-systemd-start post (code=exited, status=0/SUCCESS) Process: 11207 ExecStartPre=/usr/bin/mysql-systemd-start pre (code=exited, status=0/SUCCESS) Main PID: 11217 (mysqld_safe) ... ...

So you see that the service comes up even after a 

answered Aug 30, 2015 by rajesh