Ask a Question
Advertise on boostr.in
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).
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:
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.
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/mysqld.pid --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:
Aug 30, 2015
to add a comment.
Related Hot Questions
Government Jobs Opening