{"id":8844,"date":"2021-10-18T21:25:00","date_gmt":"2021-10-18T19:25:00","guid":{"rendered":"https:\/\/monodes.com\/predaelli\/?p=8844"},"modified":"2021-10-18T08:28:43","modified_gmt":"2021-10-18T06:28:43","slug":"how-to-run-shell-script-as-systemd-service-in-linux","status":"publish","type":"post","link":"https:\/\/monodes.com\/predaelli\/2021\/10\/18\/how-to-run-shell-script-as-systemd-service-in-linux\/","title":{"rendered":"How to Run Shell Script as Systemd Service in Linux ?"},"content":{"rendered":"<blockquote>\n<h4>How to Run Shell Script as Systemd Service in Linux ?<\/h4>\n<p>Here, we will make a manual script which will act like a process to find disk utilization of the Linux system.<\/p>\n<p>To begin, Make a bash script that redirects date and disk utilization in a file. You can create files in any location. We are going to make in executable directory \/usr\/bin:<\/p>\n<pre>$ sudo vim \/usr\/bin\/script.sh<\/pre>\n<p>Then, Copy and paste the following script and save your file:<\/p>\n<pre>#!\/bin\/bash\n# Script generates disk utilization by the system and store in a file\nwhile true\ndo\ndate &gt;&gt; \/var\/storage-monitor.txt\nsudo du -sch \/ &gt;&gt; \/var\/storage-monitor.txt\nsleep 120\ndone<\/pre>\n<p>Next, Make the file executable by running the following command.<\/p>\n<pre>$ chmod +x \/usr\/bin\/script.sh<\/pre>\n<p>Now, let&#8217;s 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 details:<\/p>\n<pre>[Unit]\nDescription=My disk monitoring service\nDocumentation=https:\/\/www.kernel.org\/\n#After=networking.service\n[Service]\nType=simple\nUser=root\nGroup=root\nTimeoutStartSec=0\nRestart=on-failure\nRestartSec=30s\n#ExecStartPre=\nExecStart=\/usr\/bin\/script.sh\nSyslogIdentifier=Diskutilization\n#ExecStop=\n[Install]\nWantedBy=multi-user.target<\/pre>\n<p>What is going on here is:<\/p>\n<ul>\n<li>The [Unit] section consists of description, documentation details. Here we have mentioned &#8216;After&#8217; 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 &#8216;ExecStart&#8217; which says to start our script file. You can also define &#8216;ExecStartPre&#8217; to define anything before the actual script file.&#8217;SyslogIdentifier&#8217; 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<\/blockquote>\n<p><em>From <a href=\"https:\/\/linuxapt.com\/blog\/496-run-shell-script-as-systemd-service-in-linux\">https:\/\/linuxapt.com\/blog\/496-run-shell-script-as-systemd-service-in-linux<\/a><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">How to Run Shell Script as Systemd Service in Linux ? Here, we will make a manual script which will act like a process to find disk utilization of the Linux system. To begin, Make a bash script that redirects date and disk utilization in a file. You can create files in any location. We&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"https:\/\/monodes.com\/predaelli\/2021\/10\/18\/how-to-run-shell-script-as-systemd-service-in-linux\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[278],"tags":[],"class_list":["post-8844","post","type-post","status-publish","format-standard","hentry","category-tricks"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6daft-2iE","jetpack-related-posts":[{"id":9912,"url":"https:\/\/monodes.com\/predaelli\/2022\/12\/14\/how-to-configure-wireless-wake-on-lan-for-linux-wifi-card\/","url_meta":{"origin":8844,"position":0},"title":"How to configure wireless wake-on-lan for Linux WiFi card","author":"Paolo Redaelli","date":"2022-12-14","format":false,"excerpt":"How to configure wireless wake-on-lan for Linux WiFi card - nixCraft I have Network Attached Storage (NAS) server that backups all my devices. However, I am having a hard time with my Linux-powered laptop. I cannot back up my laptop\/computer when it is in suspended or sleep mode. How do\u2026","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":7730,"url":"https:\/\/monodes.com\/predaelli\/2020\/11\/05\/7730\/","url_meta":{"origin":8844,"position":1},"title":"View this gist on GitHub\u2026","author":"Paolo Redaelli","date":"2020-11-05","format":false,"excerpt":"View this gist on GitHub Nel caso vada fuori linea vedi pagina 2 Signing VirtualBox Kernel Modules These are the steps I followed enable VirtualBox on my laptop without disabling UEFI Secure Boot. They're nearly identical to the process described on \u00d8yvind Stegard's blog, save for a few key details.\u2026","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"sparkles","src":"https:\/\/i0.wp.com\/github.githubassets.com\/images\/icons\/emoji\/unicode\/2728.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1143,"url":"https:\/\/monodes.com\/predaelli\/2016\/03\/14\/how-to-secure-nginx-with-lets-encrypt-on-ubuntu-14-04-digitalocean\/","url_meta":{"origin":8844,"position":2},"title":"How To Secure Nginx with Let&#8217;s Encrypt on Ubuntu 14.04 | DigitalOcean","author":"Paolo Redaelli","date":"2016-03-14","format":false,"excerpt":"Following https:\/\/letsencrypt.readthedocs.org\/en\/latest\/using.html#installation you can obtain a nice SSL certificate for your own webservers; yet for those who likes NGinx like me this guide How To Secure Nginx with Let's Encrypt on Ubuntu 14.04 | DigitalOcean is also useful In this tutorial, we will show you how to use Let's Encrypt\u2026","rel":"","context":"In &quot;Software Libero&quot;","block_context":{"text":"Software Libero","link":"https:\/\/monodes.com\/predaelli\/category\/software\/software-libero\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3701,"url":"https:\/\/monodes.com\/predaelli\/2018\/01\/24\/dpkg-how-to-install-scratch-2-on-ubuntu-16-10-or-17-04-64bit-ask-ubuntu\/","url_meta":{"origin":8844,"position":3},"title":"dpkg &#8211; How to install Scratch 2 on Ubuntu 16.10. or 17.04 (64bit)? &#8211; Ask Ubuntu","author":"Paolo Redaelli","date":"2018-01-24","format":false,"excerpt":"Today my daughter asked me to install Scratch. But not the tablet. I already did it. On the computer. I tought it was a breeze. Actually it was as simple as \"sudo apt install scratch\". Too bad I got an oldish 1.4 version and her book refers to version 2.0.\u2026","rel":"","context":"In &quot;Proprietary software&quot;","block_context":{"text":"Proprietary software","link":"https:\/\/monodes.com\/predaelli\/category\/software\/proprietary-software\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":315,"url":"https:\/\/monodes.com\/predaelli\/2015\/06\/12\/how-to-make-your-linux-pc-wake-from-sleep-automatically\/","url_meta":{"origin":8844,"position":4},"title":"How to Make Your Linux PC Wake From Sleep Automatically","author":"Paolo Redaelli","date":"2015-06-12","format":false,"excerpt":"From How to Make Your Linux PC Wake From Sleep Automatically I use sudo rtcwake -m\u00a0mem -l -t $(date +%s -d \u2018tomorrow 06:30\u2019) or sudo \/usr\/sbin\/rtcwake -m mem -s $(( 8 * 3600)) Want to put your Linux PC into sleep or hibernate mode and have it automatically wake at\u2026","rel":"","context":"In &quot;GNU\/Linux&quot;","block_context":{"text":"GNU\/Linux","link":"https:\/\/monodes.com\/predaelli\/category\/gnulinux\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2015\/06\/image101.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2015\/06\/image101.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2015\/06\/image101.png?resize=525%2C300 1.5x"},"classes":[]},{"id":7144,"url":"https:\/\/monodes.com\/predaelli\/2020\/04\/30\/little-notes\/","url_meta":{"origin":8844,"position":5},"title":"Little notes","author":"Paolo Redaelli","date":"2020-04-30","format":false,"excerpt":"Just a little note to myself, to start salome on a severly hacked and messed up workstation like mine: paolo@rigel:~\/salome_meca\/appli_V2019.0.3_universal$ \/home\/paolo\/salome_meca\/appli_V2019.0.3_universal\/salome runSalome running on rigel Searching for a free port for naming service: 2815 - OK Searching Naming Service + found in 0.1 seconds Searching \/Kernel\/Session in Naming Service ++++libGL\u2026","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/8844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/comments?post=8844"}],"version-history":[{"count":0,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/8844\/revisions"}],"wp:attachment":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/media?parent=8844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/categories?post=8844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/tags?post=8844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}