How to configure a service to automatically start or stop a service at boot in Linux

Home > Search > How-to
  by

INIT / SYSVINIT

The /etc/init.d/ directory contains files that are used to start or stop a service at boot, such as the firewall, the HTTP deamon, or networking. The contents of these files are a script that will start or stop the service when the system is initialized. Files in the /etc/init.d/ directory are used when the init or sysvinit initialization deamon. However, systems using the systemd or upstart initialization deamon will not use files in the /etc/init.d/ directory to automatically start or stop services at boot. Systemd and upstart systems have an alternative way to automatically start or stop services at boot.

[root@server1 ~]# ls -l /etc/init.d/
-rwxr-xr-x.  1  root  root  2989  Sep 16 2015  firewall
-rwxr-xr-x.  1  root  root  6581  Sep 16 2015  httpd
-rwxr-xr-x.  1  root  root  3581  Sep 16 2015  network

 

When a system is initialized, such as during boot, the INIT deamon will first read the /etc/inittab file. In this example, the system is configured to use runlevel 5. This tells the system to start all deamons that are configured as on at runlevel 5. The /etc/inittab file can be edited to permanently change the default runlevel of the system.

id:5:default

 

The chkconfig command without any options or with the --list option can be used to view the runlevel of init or sysvinit services. For example, if the system is using runlevel 1, both netconsole and network will be off. However, if the system is using runlevel 5, netconsole will be off and network will be on.

[root@server1 ~]# chkconfig --list
netconsole    0:off  1:off  2:off  3:off  4:off  5:off  6:off
network       0:off  1:off  2:on   3:on   4:on   5:on   6:off
. . .

 

The chkconfig service_name on or chkconfig service_name off commands can be used to turn a service on or off. This will turn the service on or off at every runlevel. For example, to turn the HTTPD service on or off.

[root@server1 ~]# chkconfig httpd on
[root@server1 ~]# chkconfig httpd off

 

To only change the runlevel status for certain runlevels, use the --level option. 

[root@server1 ~]# chkconfig httpd on --level 345

 

When using the chkconfig command to change the runlevel of a service, a file in the /etc/init.d/ directory will be updated to contain the new runlevel. For example, the prior commands turns on the HTTPD deamon for runlevels 3, 4, and 5. This command updates the /etc/init.d/httpd file to contain the following. Even though this line is commented out, this still turns on the HTTPD deamon for runlevels 3, 4, and 5.

#!/bin/sh
. . .
# chkconfig: 345 95 05	

 


SYSTEMD

The systemctl command can be used to automatically enable or disable a service at boot.

[root@server1 ~]# systemctl enable service_name
[root@server1 ~]# systemctl disable service_name

 

The systemctl list-unit-files command can be used to view the status of services when using systemd.

[root@server1 ~]# systemctl list-unit-files
abrt-ccpp.service            enabled
abrt-oops.service            disabled
abrt-pstoreoops.service      enabled
. . .

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments