{"id":7468,"date":"2021-06-22T16:48:41","date_gmt":"2021-06-22T16:48:41","guid":{"rendered":"https:\/\/linuxways.net\/?p=7468"},"modified":"2021-06-22T16:48:41","modified_gmt":"2021-06-22T16:48:41","slug":"how-to-run-shell-script-as-systemd-service-in-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/how-to-run-shell-script-as-systemd-service-in-linux\/","title":{"rendered":"How to Run Shell Script as Systemd Service in Linux"},"content":{"rendered":"<p>Systemd is a service manager that provides you features of managing daemons, process tracking, mount, automount. It uses a Linux control group to manage the task. It replaces the older initd and is available in Debian, Fedora, RedHat, ubuntu, centos, arch Linux distributions.<\/p>\n<p>In this article, I am going to show you how to create a service using systemd. I will make a manual script which will act like a process to find disk utilization of the Linux system.<\/p>\n<p>Let\u2019s get started. Make a bash script that redirects date and disk utilization in a file. You can create files in any location. Here I am going to make in executable directory \/usr\/bin<\/p>\n<pre>$ sudo vim \/usr\/bin\/script.sh<\/pre>\n<p>Copy and paste the following script and save your file.<\/p>\n<pre>#!\/bin\/bash\r\n\r\n# Script generates disk utilization by the system and store in a file\r\n\r\nwhile true\r\n\r\ndo\r\n\r\ndate &gt;&gt; \/var\/storage-monitor.txt\r\n\r\nsudo du -sch \/ &gt;&gt; \/var\/storage-monitor.txt\r\n\r\nsleep 120\r\n\r\ndone\r\n<\/pre>\n<p>Make the file executable by running the following command.<\/p>\n<pre>$ chmod +x \/usr\/bin\/script.sh<\/pre>\n<p>Now, let\u2019s make a service for running the script. Just create a file in the following directory. Note you can give any name but it must end with .service extension.<\/p>\n<pre>$ vim \/etc\/systemd\/system\/monitor-disk.service<\/pre>\n<p>And add the following,<\/p>\n<pre>[Unit]\r\n\r\nDescription=My disk monitoring service\r\n\r\nDocumentation=https:\/\/www.kernel.org\/\r\n\r\n#After=networking.service\r\n\r\n[Service]\r\n\r\nType=simple\r\n\r\nUser=root\r\n\r\nGroup=root\r\n\r\nTimeoutStartSec=0\r\n\r\nRestart=on-failure\r\n\r\nRestartSec=30s\r\n\r\n#ExecStartPre=\r\n\r\nExecStart=\/usr\/bin\/script.sh\r\n\r\nSyslogIdentifier=Diskutilization\r\n\r\n#ExecStop=\r\n\r\n[Install]\r\n\r\nWantedBy=multi-user.target\r\n<\/pre>\n<p>Explanation<\/p>\n<ul>\n<li>The [Unit] section consists of description, documentation details. Here I have mentioned \u2018After\u2019 which states the service that we are going to create must be running first.<\/li>\n<li>[Service] Section defines the service type, username, group, what to do in failure, restart timeout. The main is \u2018ExecStart\u2019 which says to start our script file. You can also define \u2018ExecStartPre\u2019 to define anything before the actual script file. \u2018SyslogIdentifier\u2019 is the keyword to identify our service in syslog. Similarly , ExecStop is the instruction to say what to do to stop the service.<\/li>\n<li>[Install] section is used to define different levels of target in the system.<\/li>\n<\/ul>\n<p>Just save the file and start the service using the systemctl command.<\/p>\n<pre>$ systemctl start monitor-disk.service<\/pre>\n<p>Check the service status using systemctl status command. You can also see service name, file form where systemd service is loaded, documentation, process running and logs.<\/p>\n<pre>$ systemctl status monitor-disk.service<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"843\" height=\"346\" class=\"wp-image-7469\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-626.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-626.png 843w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-626-300x123.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-626-768x315.png 768w\" sizes=\"auto, (max-width: 843px) 100vw, 843px\" \/><\/p>\n<p>Verify that your script is correctly working by looking into the file defined in the script file.<\/p>\n<pre>$ cat \/var\/storage-monitor.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"570\" height=\"343\" class=\"wp-image-7470\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-627.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-627.png 570w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-627-300x181.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-627-501x300.png 501w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/p>\n<p>You can also check the enable, disable facility of the systemd manager.<\/p>\n<pre>$ systemctl enable monitor-disk.service<\/pre>\n<pre>$ systemctl disable monitor-disk.service<\/pre>\n<p>Also, check by stopping and restarting the service. No error should be thrown.<\/p>\n<pre>$ systemctl stop monitor-disk.service<\/pre>\n<pre>$ systemctl restart monitor-disk.service<\/pre>\n<p>Systemd also enables default logging in syslog. So, you can view the live log of the service using the following command. Search the keyword \u2018SyslogIdentifier=Diskutilization\u2019<\/p>\n<p>you denied in the above service file.<\/p>\n<pre>$ tail -f \/var\/log\/syslog<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"869\" height=\"160\" class=\"wp-image-7471\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-628.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-628.png 869w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-628-300x55.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-628-768x141.png 768w\" sizes=\"auto, (max-width: 869px) 100vw, 869px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>This article shows you how to run your own shell script as a systemd service. We hope you learnt how to make your own service. Please feel free to send your feedback.<\/p>","protected":false},"excerpt":{"rendered":"<p>Systemd is a service manager that provides you features of managing daemons, process tracking, mount, automount. It uses a Linux control group to manage the task. It replaces&hellip;<\/p>","protected":false},"author":1,"featured_media":7566,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,5,83,165,2],"tags":[19],"class_list":["post-7468","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-shell-script"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/7468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=7468"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/7468\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/7566"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=7468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=7468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=7468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}