One of the essential for a system administrator to know is how to configure service at boot, so when a server gets a reboot, they start automatically.
There could be various reasons for server reboot, including the following.
- Scheduled for weekly, monthly
- Unexpected due to hardware/kernel issue
By doing the right configuration, you don’t have to start them manually each time you reboot.
A little bit of automation. Isn’t it?
The following examples are for two popular distros tested on DigitalOcean servers.
CentOS or RHEL 6.x
In the following example, I have taken an Apache HTTP server, but the procedure remains the same for any other services you wish to start at boot in Red Hat Enterprise Linux (RHEL) or CentOS 6 version.
You can keep any script file name, and here I’ve kept
- Become a root user on your Linux server
- Create or copy your script under /etc/init.d/
[[email protected] init.d]# ls -ltr httpd -rwxr-xr-x. 1 root root 3371 Jan 6 08:56 httpd [[email protected] init.d]#
We will use
chkconfig utility which is available default on Linux or CentOS.
- Add script to start on boot using
[[email protected] init.d]# chkconfig --add httpd [[email protected] init.d]# chkconfig httpd on
- Confirm script is added successfully with
[[email protected] init.d]# chkconfig --list httpd httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off [[email protected] init.d]#
httpd script will be called to start services on Linux boot.
In case you need to disable the auto-start service then you can use the following commands
chkconfig httpd off chkconfig --del httpd
RHEL or CentOS 7.x/8.x
The procedure to configure services on boot in RHEL 7 is slightly different than RHEL 6. It uses systemd to manage the services.
Most of the software like Apache, PHP, MySQL, Nginx scripts are added in services when you install it.
Let’s take an example of PHP-FPM.
First thing first, let’s see the status of php-fpm (this assume you already have scripts in
[[email protected] ~]# systemctl status php-fpm php-fpm.service - The PHP FastCGI Process Manager Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled) Active: inactive (dead) [[email protected] ~]#
As you can see the status is disabled which means it’s not configured to start on boot.
Let’s enable php-fpm to start on boot by using
[[email protected] ~]# systemctl enable php-fpm Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service. [[email protected] ~]#
Now, let’s see the status
[[email protected] ~]# systemctl status php-fpmphp php-fpm.service - The PHP FastCGI Process Manager Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled) Active: inactive (dead) [[email protected] ~]#
php-fpm is all set to start on boot. Let’s test it by rebooting the server.
If you ever need to disable starting services on boot, then you can use the below command
systemctl disable php-fpm
You may also prefer to check out this post explaining systemd and auto-starting services on boot.
Configuring auto-start services in Ubuntu is slightly different. Let’s say the script name is Nginx
- Login to Ubuntu server with root
- Copy the script in /etc/init.d/ folder
- Execute the below command
update-rc.d nginx defaults
- Reboot the server to ensure services are started.
This has helped me and I believe it will be beneficial to you as well.
System administration is always fun and challenging, and if you are looking to supercharge your career in it, then you may refer to this Udemy course.
Next, check out how to find Linux sudden reboot reason.