Start Services Automatically in CentOS 7

By | February 19, 2016

One of the problems we all face as system administrators is that making sure every critical service is up and running at all times in our servers. If a critical service such as the database server service crashes, application that are dependent on the service wouldn’t function. For example, the server which I have hosted this website itself has an issue with the database server service where it crashes from time to time and I have to manually start it. At first I didn’t mind much about it since almost all the time I had internet access so I could connect to the server and start the service. But the real pain started when I was away for the weekend and the database server service crashed and the site was inaccessible for the entire weekend.

We do have monitoring solutions to alert when such downtimes happen and help us take necessary actions accordingly. But what if one of these alerts go missing to our eye? What if you are the only system administrator and you are away from your systems, (just like what happened to me)? Or you may have many other reasons to make this is an automated task.

Yes, this is not a new topic. This was a topic that has been in the industry for a longer time period and has so many answers too. Many resources in the internet would help you with so many different solutions. For example, writing a bash script and adding it to your cron tab. But what if you’re new to Linux and do not have adequate knowledge in scripting? Fear not! I will tell you a simple method to automate this process and how to get the work done easily.

In this post I will be discussing about how to get this done in a CentOS 7 distro and I will show you a video demo where I will show you how have I configured the database server service to auto start if the service crashed.

I am not going to talk about the steps of installing the database server. I will assume you have already installed the database server.

  1. Once you have installed the database server, or any service for that matter, you might need to make sure that the service is started automatically with the OS. To check if your service is configured to start with the OS, you can run below command.
    systemctl is-enabled servicename
  2. If the above command returned the output “disabled”, then you have to enable it. Run below command to enable auto start of the service with the OS.
    systemctl enable servicename
  3. Now that you have enabled the service to start with the OS, you would want to make it start again if in case the service crashes while running. To achieve this, we will be editing the relevant unit file of the service you would want to start automatically after a crash.
    1. Navigate to /etc/systemd/system/
    2. From the list of service unit files, open the relevant unit file of your service with a text editor. You will have to run this as sudo if you’re not logged in as the root.
    3. Then, check for the configuration block [Service] to see if there’s a directive similar to one underlined in red in below screenshot.service-unit-service-directivesIf you already have above directive configured as seen in above screenshot, you’re good to go. Otherwise, add the directive to the [Service] directives.
    4. After you saved the file, run below command to reload systemd to reload the configuration changes you’ve made.
      systemctl daemon-reload
    5. Now to test the functionality, we’ll be forcefully killing the service to simulate a crash situation. First, run below command and find the PID relevant to your running service.
      systemctl status servicename
    6. As you can see in the below screenshot, the PID of the service can be identified under Main PID
      checking the service pid
    7. Now run below command to kill the service process forcefully.
      kill -9 pidnumber
    8. After killing the service, do the step 3.5 again to check the service running status. Look for the Main PID again. You’ll notice that it has a new PID number. Reason is that after you killed the process, it automatically started the service and took a new process ID.

I hope this post would help you and above instructions are demonstrated in below video. The service I’m modifying here is the MariaDB service on CentOS 7.

Leave a Reply