
How to Auto-start Services on Boot in Linux?


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 httpd
- 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
chkconfig
with--add
parameter
[[email protected] init.d]# chkconfig --add httpd
[[email protected] init.d]# chkconfig httpd on
- Confirm script is added successfully with
--list
[[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]#
That’s all! 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 /usr/lib/systemd/system/
)
[[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 systemctl
[[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.
Ubuntu
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.
Head here you want to install/upgrade PHP-FPM 5.6 on CentOS 6.x.
- Tagged in:
- Linux
More great readings on Sysadmin
-
Looking For Traceroute on RHEL 8? Try TracepathAbhishek Nair on June 14, 2022
-
6 Best Switch Port Monitoring ToolsDurga Prasad Acharya on June 12, 2022
-
Windows 10/11 Random Shutdown: How to Find the CauseHitesh Sant on May 30, 2022
-
7 Best Server Configuration Monitoring and Auditing ToolsTalha Khalid on May 28, 2022
-
8 Best FTP Server Software for Windows for Secure TransferSatish Shethi on May 24, 2022
-
OSI Model Layers: An Introduction GuideAmrita Pathak on May 13, 2022
Join Geekflare Newsletter
Every week we share trending articles and tools in our newsletter. More than 10,000 people enjoy reading, and you will love it too.