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 RemainAfterExit=yes User=oracle Group=oinstall ExecStart=/home/oracle/scripts/startup.sh >> /home/oracle/scripts/startup_shutdown.log 2>&1 & ExecStop=/home/oracle/scripts/shutdown.sh >> /home/oracle/scripts/startup_shutdown.log 2>&1 [Install] WantedBy=multi-user.target
Thanks to Javier and Mark in the comments for their suggestions about using
Group in this file.
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; linked) Active: active (exited) since Tue 2015-03-24 10:55:28 GMT; 5min ago Process: 6145 ExecStart=/home/oracle/scripts/startup.sh >> /home/oracle/scripts/startup_shutdown.log 2>&1 & (code=exited, status=0/SUCCESS) Main PID: 6145 (code=exited, status=0/SUCCESS) Mar 24 10:56:27 ol7-121.localdomain startup.sh: SQL> ORACLE instance started. Mar 24 10:56:27 ol7-121.localdomain startup.sh: Total System Global Area 1140850688 bytes Mar 24 10:56:27 ol7-121.localdomain startup.sh: Fixed Size 2923584 bytes Mar 24 10:56:27 ol7-121.localdomain startup.sh: Variable Size 419431360 bytes Mar 24 10:56:27 ol7-121.localdomain startup.sh: Database Buffers 704643072 bytes Mar 24 10:56:27 ol7-121.localdomain startup.sh: Redo Buffers 13852672 bytes Mar 24 10:56:31 ol7-121.localdomain startup.sh: Database mounted. Mar 24 10:56:33 ol7-121.localdomain startup.sh: Database opened. Mar 24 10:56:33 ol7-121.localdomain startup.sh: SQL> Disconnected from Oracle Database 12c Enterprise Edition Re...ction Mar 24 10:56:33 ol7-121.localdomain startup.sh: With the Partitioning, OLAP, Advanced Analytics and Real Applica...tions Hint: Some lines were ellipsized, use -l to show in full. #
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...