How to Create and Execute New Service in SystemD - Shell Script
Steps to Create and Execute New Service Units in SystemD Using Shell Script
SystemD is a system service manager in linux. Creating and executing new service units in SystemD using shell scripting is explained in this session.
To view service units
Execute the below command in graphical.target level, to have a look at service units.
root@linuxhelp:~# systemctl --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
accounts-daemon.service loaded active running Accounts Service
anacron.service loaded active running Run anacron jobs
apparmor.service loaded active exited LSB: AppArmor initialization
apport.service loaded active exited LSB: automatic crash report g
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
cgmanager.service loaded active running Cgroup management daemon
colord.service loaded active running Manage, Install and Generate
cron.service loaded active running Regular background program pr
cups-browsed.service loaded active running Make remote CUPS printers ava
cups.service loaded active running CUPS Scheduler
dbus.service loaded active running D-Bus System Message Bus
Now you can see the status for all services that are currently running in your system.
To check the status for a specific service
Run the following command to check the status for a specific service.
root@linuxhelp:~# systemctl status cron.service ? cron.service - Regular background program processing daemon Loaded: loaded (/lib/systemd/system/cron.service enabled vendor preset: enabled) Active: active (running) since Thu 2016-04-21 18:23:23 IST 18h ago Docs: man:cron(8) Main PID: 845 (cron) CGroup: /system.slice/cron.service ??845 /usr/sbin/cron -f Apr 21 18:23:23 linuxhelp systemd[1]: Started Regular background program pro...n. Apr 21 18:23:23 linuxhelp systemd[1]: Starting Regular background program pr..... Apr 21 18:23:23 linuxhelp cron[845]: (CRON) INFO (pidfile fd = 3) Apr 21 18:23:24 linuxhelp cron[845]: (CRON) INFO (Running @reboot jobs) Hint: Some lines were ellipsized, use -l to show in full.
In above, the status of cron.service is checked, it informs that cron service is enabled and executing all the time.
To immediately start and stop a service
If a service is enabled or disabled once it will automatically starts and stops in the next boot. To immediately start and stop a service without rebooting run the following commands.
Example
root@linuxhelp:~# systemctl stop cron.service root@linuxhelp:~# systemctl status cron.service ? cron.service - Regular background program processing daemon Loaded: loaded (/lib/systemd/system/cron.service enabled vendor preset: enabled) Active: inactive (dead) since Fri 2016-04-22 12:53:00 IST 1s ago Docs: man:cron(8) Process: 845 ExecStart=/usr/sbin/cron -f $EXTRA_OPTS (code=killed, signal=TERM) Main PID: 845 (code=killed, signal=TERM) Apr 21 18:23:23 linuxhelp systemd[1]: Started Regular background program pro...n. Apr 21 18:23:23 linuxhelp systemd[1]: Starting Regular background program pr..... Apr 21 18:23:23 linuxhelp cron[845]: (CRON) INFO (pidfile fd = 3) Apr 21 18:23:24 linuxhelp cron[845]: (CRON) INFO (Running @reboot jobs) Apr 22 12:53:00 linuxhelp systemd[1]: Stopping Regular background program pr..... Apr 22 12:53:00 linuxhelp systemd[1]: Stopped Regular background program pro...n. Hint: Some lines were ellipsized, use -l to show in full.
root@linuxhelp:~# systemctl start cron.service root@linuxhelp:~# systemctl status cron.service ? cron.service - Regular background program processing daemon Loaded: loaded (/lib/systemd/system/cron.service enabled vendor preset: enabled) Active: active (running) since Fri 2016-04-22 12:53:12 IST 2s ago Docs: man:cron(8) Main PID: 4084 (cron) CGroup: /system.slice/cron.service ??4084 /usr/sbin/cron -f Apr 22 12:53:12 linuxhelp systemd[1]: Started Regular background program pro...n. Apr 22 12:53:12 linuxhelp systemd[1]: Starting Regular background program pr..... Apr 22 12:53:12 linuxhelp cron[4084]: (CRON) INFO (pidfile fd = 3) Apr 22 12:53:12 linuxhelp cron[4084]: (CRON) INFO (Skipping @reboot jobs -- ...p) Hint: Some lines were ellipsized, use -l to show in full.
To enable or disable a service
Run the following command to enable or disable a service
Example
root@linuxhelp:~# systemctl disable cron.service Synchronizing state for cron.service with sysvinit using update-rc.d... Executing /usr/sbin/update-rc.d cron defaults Executing /usr/sbin/update-rc.d cron disable root@linuxhelp:~# systemctl enable cron.service Synchronizing state for cron.service with sysvinit using update-rc.d... Executing /usr/sbin/update-rc.d cron defaults Executing /usr/sbin/update-rc.d cron enable
To see the link files for unit services
Go to /etc/systemd/system/ directory path to see the link files for unit services.
Example
root@linuxhelp:~# cd /etc/systemd/system/
root@linuxhelp:/etc/systemd/system# ls
bluetooth.target.wants kexec.target.wants
dbus-org.bluez.service multi-user.target.wants
dbus-org.freedesktop.Avahi.service network-online.target.wants
dbus-org.freedesktop.ModemManager1.service plymouth-log.service
dbus-org.freedesktop.nm-dispatcher.service plymouth.service
default.target.wants poweroff.target.wants
display-manager.service printer.target.wants
display-manager.service.wants reboot.target.wants
getty.target.wants shutdown.target.wants
graphical.target.wants sockets.target.wants
halt.target.wants suspend.target.wants
hibernate.target.wants sysinit.target.wants
hybrid-sleep.target.wants syslog.service
Here you can get some link files of service units and also some directories of the target.wants
This directory /etc/systemd/system/multi-user.target.wants/ contains all the files which is need for multi-user target.
Example:
root@linuxhelp:/etc/systemd/system# ls multi-user.target.wants/
anacron.service cups.path pppd-dns.service
avahi-daemon.service ModemManager.service remote-fs.target
cgmanager.service NetworkManager.service rsyslog.service
cgproxy.service plymouth-quit.service ufw.service
cron.service plymouth-quit-wait.service vsftpd.service
cups-browsed.service postgresql.service whoopsie.service
To create a service unit
To create a service unit with the name create.service and make it executable for all users.
Example
root@linuxhelp:/etc/systemd/system# vim create.service
Add the following lines in create.service
[Unit] Description = This will Create a new file After = network.target [Service] ExecStart = /usr/local/bin/touch.sh [Install] WantedBy = multi-user.target
root@linuxhelp:/etc/systemd/system# chmod a+x create.service
To create a unit of service type, set it to be loaded after the network.target and we need every-time the service begins to run a bash script with the name " touch.sh" that we are going to create.
If the service is enabled, a symbolic link will be automatically created to that service within the " multi-user.target.wants" and if you disable this, then the link will be deleted.
To enable and check
Run the following command to enable and check it.
Example
root@linuxhelp:/etc/systemd/system# systemctl enable create.service
Created symlink from /etc/systemd/system/multi-user.target.wants/create.service to /etc/systemd/system/create.service.
root@linuxhelp:/etc/systemd/system# ls multi-user.target.wants/
anacron.service cups.path remote-fs.target
avahi-daemon.service ModemManager.service rsyslog.service
cgmanager.service NetworkManager.service ufw.service
cgproxy.service plymouth-quit.service vsftpd.service
create.service plymouth-quit-wait.service whoopsie.service
cron.service postgresql.service
cups-browsed.service pppd-dns.service
Now the create.service is ready for next booting, but we need to create a script file called touch.sh under the path /usr/local/bin as we mentioned above.
Example
root@linuxhelp:/etc/systemd/system# vim /usr/local/bin/touch.sh
Adding the below line within vim and save it
#!/bin/bash touch /root/file
and make the script file will be executed by all users by running the below command.
Example
root@linuxhelp:/etc/systemd/system# chmod a+x /usr/local/bin/touch.sh
If you don' t have time to wait until next boot, just start the service for the present session using below command.
Example
root@linuxhelp:/etc/systemd/system# systemctl start create.service
After starting the service check whether the file is created under /root directory as we mentioned in our script file.
Example
root@linuxhelp:/etc/systemd/system# ls /root/
file
Comments ( 0 )
No comments available