Linux Services (systemd, systemctl)
Fedora 15 introduced
systemd as a replacement for the previous
sysvinit service management. Since RHEL7 and Oracle Linux 7 are based on Fedora 19, the switch from
systemd is now part of the Enterprise Linux distributions. This article is a rework of the previous Linux Service article, bringing it up to date.
- Backwards Compatibility
- Starting and Stopping Services
- Enabling and Disabling Services
- Creating Linux Services
The transition to using
systemd is made easier by the fact the
chkconfig commands are still available. They have been reworked to call the equivalent
systemctl commands. What's more, they sometimes output the command they are redirecting too, making learning the new
systemd commands much easier.
# service nfs restart Redirecting to /bin/systemctl restart nfs.service #
On Fedora, this backwards compatibility also extends to the
system-config-services tool, which is still available. This package is not available in the RHEL7/OL7 distributions.
Starting and Stopping Services
sysvint these actions were achieved using the
service command, with the service definitions located in the "/etc/init.d" directory. Under
systemd, the service definitions are located in the "/lib/systemd/system/" directory.
# ls /lib/systemd/system/httpd.service /lib/systemd/system/httpd.service #
systemctl command is used to stop, start, restart and check the status of a specified service. Unlike the
service command, most
systemctl commands do not produce any status output on the command line. Services can be referenced with or without the ".service" suffix.
# systemctl stop httpd # systemctl stop httpd.service # systemctl start httpd # systemctl start httpd.service # systemctl restart httpd # systemctl restart httpd.service # systemctl status httpd # systemctl status httpd.service
The output from the
systemctl status command is quite different to that of the
# systemctl status httpd httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled) Active: active (running) since Sun 2014-04-20 09:26:18 BST; 32min ago Main PID: 16314 (httpd) Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec" CGroup: /system.slice/httpd.service ├─16314 /usr/sbin/httpd -DFOREGROUND ├─16315 /usr/sbin/httpd -DFOREGROUND ├─16316 /usr/sbin/httpd -DFOREGROUND ├─16317 /usr/sbin/httpd -DFOREGROUND ├─16318 /usr/sbin/httpd -DFOREGROUND └─16319 /usr/sbin/httpd -DFOREGROUND Apr 20 09:26:18 localhost.localdomain systemd: Starting The Apache HTTP Se... Apr 20 09:26:18 localhost.localdomain httpd: AH00558: httpd: Could not... Apr 20 09:26:18 localhost.localdomain systemd: Started The Apache HTTP Ser... Hint: Some lines were ellipsized, use -l to show in full. #
Use the following commands to get the status of all services.
# # All loaded and active services. # systemctl list-units --type service # # All loaded services # systemctl list-units --type service --all # # All available services # systemctl list-unit-files --type service
Enabling and Disabling Services
chkconfig command was used to perform these actions. Under
systemctl command is used to enable and disable services to auto-start at reboot.
# systemctl enable httpd ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service' # # systemctl disable httpd rm '/etc/systemd/system/multi-user.target.wants/httpd.service' #
On Fedora the "Service Configuration" dialog is available from the menu (System > Administration > Services) or directly from the command line by running the
The "Enable" and "Disable" buttons are used to toggle the auto-start on reboot for each service. The "Start", "Stop" and "Restart" buttons affect the current state of the service.
Creating Linux Services
As an example, in this section we will create a new service to automatically start/stop an Oracle database. This assumes the Oracle database is not using Oracle Restart and the "startup.sh" and "shutdown.sh" scripts are already present, as described here.
Create the service file called "/lib/systemd/system/dbora.service".
[Unit] Description=The Oracle Database Service After=syslog.target network.target [Service] Type=simple ExecStart=/bin/su oracle -c "/home/oracle/scripts/startup.sh >> /home/oracle/scripts/startup_shutdown.log 2>&1" & ExecStop=/bin/su oracle -c "/home/oracle/scripts/shutdown.sh >> /home/oracle/scripts/startup_shutdown.log 2>&1" [Install] WantedBy=multi-user.target
Create a symbolic link to the "/etc/systemd/system/" directory.
# ln -s /lib/systemd/system/dbora.service /etc/systemd/system/dbora.service
systemd so it can see the new service.
# systemctl daemon-reload
Start the service and enable so it is automatically restarted on reboot.
# systemctl start dbora.service # systemctl enable dbora.service ln -s '/usr/lib/systemd/system/dbora.service' '/etc/systemd/system/multi-user.target.wants/dbora.service' # systemctl status dbora.service dbora.service - The Oracle Database Service Loaded: loaded (/lib/systemd/system/dbora.service; enabled) Active: inactive (dead) Apr 20 12:53:53 localhost.localdomain systemd: Starting The Oracle Database Service... Apr 20 12:53:54 localhost.localdomain systemd: Started The Oracle Database Service. Apr 20 12:53:54 localhost.localdomain su: (to oracle) root on none Apr 20 12:53:54 localhost.localdomain su: (to oracle) root on none #
For more information see:
- RHEL Documentation
- Managing Services with systemd (RHEL)
- systemd (Fedora Project)
- Linux Services (service, chkconfig, system-config-services)
Hope this helps. Regards Tim...