Web development and Tech news Blog site. WEBISFREE.com

HOME > linux

How to automatically start services when restarting a Linux server

Last Modified : 03 Mar, 2023 / Created : 04 Mar, 2023
View Count

When using a Linux server, it often becomes necessary to restart the server unexpectedly. However, when this happens, the services registered on the server may not start automatically, requiring you to log in and start the services manually, which can be quite cumbersome. Wouldn't it be convenient if the services could start automatically? Let's learn how to make this possible!

! Starting services automatically

Servers can go down for various reasons, and restarting them is typically the solution. But do you really want to perform this task manually each time? Of course, it's doable, but it would be much more convenient if the services could start automatically. We'll explore a brief overview of how to make this happen for the desired service. (Personally, I've been a little lazy about implementing this in the past, but I recently decided that I couldn't put it off any longer and registered an automatic script.)

First, you need to identify which service you want to run. In this case, I want the mongod service to start automatically.

[ Note ]
"I tried to register an automatic script using the most well-known command, update-rc.d. It goes like this:"
> update-rc.d mongod defaults

However, there continued to be "Not found" errors. In addition, the service was not registered... errors such as "script not found" occurred as well. After searching around, it seems that for Ubuntu versions 15.10 and later, systemctl can be used as shown below. So let's take a look at how to use systemctl.

The method is simple. Just use the service name and enable together as shown below. However, I added ".service" after mongod.
> sudo systemctl enable mongod.service

Although I have used start and stop commands frequently with systemctl, I had never thought about using enable to start them automatically. However, now it seems easy to do so.

Now mongod has been successfully registered to start automatically. From now on, whenever the server restarts, the mongod service will start automatically.

@ Disable automatic service in reverse
On the other hand, you can also automatically unregister a registered service. In this case, use "disable".
> sudo systemctl disable mongod.service

The previously registered service will be changed to not function upon restart after being released. We have learned how to automatically execute services, which is surprisingly simple.

As a final note, restarting the server may require more than just one service to be restarted. You may need to register multiple services or perform other tasks. In such cases, creating a script file and running it may be the most effective method. We will post about this method later.
Perhaps you're looking for the following text as well?


Learning about the make command in Linux