Sometimes as a system admin, you may want to control a 3rd party application service using systemd target unit. This allows us to use the systemctl command to start, stop and view the status of the service. Also, you may want to combine several 3rd party app service into a single systemd target unit for administration simplicity.
In this post, we will see such an example where we will manage 4 application services using a single systemd target unit. This will allow us to run the four systemctl commands with just one call. For example:
# systemctl app start
Configuring Custom systemd target unit
1. Create a “Target Unit” file under /etc/systemd/system. It should look like this :
# vi /etc/systemd/system/app.target [Unit] Description=App Service Unit # This collection of apps should be started at boot time. [Install] WantedBy=multi-user.target
2. Then each customer instance should include “PartOf” in the [Unit] section and should also have an [Install] section so that enable and disable will work on the specific service :
# vi /etc/systemd/system/app1.service [Unit] Description= Instance of App Service PartOf=app.target [Service] Type=oneshot RemainAfterExit=yes ExecStart=/home/mark/bin/app-poc.sh start ExecStop=/home/mark/bin/app-poc.sh stop # When the service runs globally, make it run as a particular user for added security #User=myapp #Group=myapp # When systemctl enable is used, make this start when the App service starts [Install] WantedBy=app.target
3. Execute below commands to relaod the systemd and enable the new target units to start automatically after reboot.
# systemctl daemon-reload # systemctl enable app.target # systemctl enable app1.service # systemctl enable app2.service
How to Execute Scripts/Commands using /etc/rc.d/rc.local in CentOS/RHEL 7