{"id":245,"date":"2024-07-12T09:14:03","date_gmt":"2024-07-12T06:14:03","guid":{"rendered":"https:\/\/pac-man.ocitec.us\/?page_id=245"},"modified":"2024-08-13T10:19:03","modified_gmt":"2024-08-13T07:19:03","slug":"0004_services","status":"publish","type":"page","link":"https:\/\/itgen.itbumper.com\/?page_id=245","title":{"rendered":"0004_services"},"content":{"rendered":"<p style=\"text-align: justify;\" data-tadv-p=\"keep\">Creating and managing services in Ubuntu Server 18 is essential for system administrators. In this article, we will explore how to create custom services using <code>systemd<\/code>. We will explain each option in detail and provide examples.<\/p>\n<p style=\"text-align: center;\"><strong>Essential Steps to Create a Service<\/strong><\/p>\n<p>Let&#8217;s start by creating a simple script. Suppose we have a script that logs the current time to a file.<\/p>\n<ol>\n<li>&nbsp;Create a file (<strong><em>sudo nano \/usr\/local\/bin\/time_logger<\/em><\/strong>) and put the code below and save it;<\/li>\n<li>&nbsp;Make a file executable (<strong><em>sudo <span class=\"hljs-built_in\">chmod<\/span> +x \/usr\/local\/bin\/time_logge<\/em>r<\/strong>).<\/li>\n<\/ol>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n#!\/bin\/bash\nwhile true\ndo\n    echo &quot;Current time: $(date)&quot; &gt;&gt; \/var\/log\/time.log\n    sleep 60\ndone\n<\/pre><\/div>\n\n<p><\/p>\n<p style=\"text-align: center;\"><strong>Create the Service File<\/strong><\/p>\n<p>Create a file. (<strong><em>sudo nano \/etc\/systemd\/system\/time_logger.service<\/em><\/strong>) and add the code below.<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n&#x5B;Unit]\nDescription=Time Logger Service\nAfter=network.target\n\n&#x5B;Service]\nExecStart=\/usr\/local\/bin\/time_logger\nRestart=always\nUser=pac-man\nGroup=adm\n\n&#x5B;Install]\nWantedBy=multi-user.target\n\n<\/pre><\/div>\n\n<p><\/p>\n<p>Let&#8217;s look at the main options in this file:<\/p>\n<p><strong>[Unit]<\/strong><\/p>\n<p><strong>Description:<\/strong> A description of the service.<br><strong>After:<\/strong> Specifies that the service should start after the specified target. Here, it is network.target.<\/p>\n<p><strong>[Service]<\/strong><\/p>\n<p><strong>ExecStart:<\/strong> The command that will be executed to start the service. In our case, it is the path to the script.<br><strong>Restart:<\/strong> The policy for restarting the service. The always option means the service will be resumed on any exit.<br><strong>User and Group:<\/strong> The user and group under which the service will run.<\/p>\n<p><strong>[Install]<\/strong><\/p>\n<p><strong>WantedBy:<\/strong> Specifies the target to which the service should be attached. multi-user.target means the service will run in multi-user mode (standard mode for servers).<\/p>\n<p style=\"text-align: center;\"><strong>Starting and Managing the Service<\/strong><\/p>\n<p>After creating the service file, reload the service files and start the new service:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo systemctl daemon-reload\nsudo systemctl start time_logger.service\n\n<\/pre><\/div>\n\n<p>To check the status of the service:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo systemctl status time_logger.service\n<\/pre><\/div>\n\n<p style=\"text-align: left;\">To enable the service to start on boot:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo systemctl enable time_logger.service\n<\/pre><\/div>\n\n<p>To stop and disable the service:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo systemctl stop time_logger.service\nsudo systemctl disable time_logger.service\n<\/pre><\/div>\n\n<p>Create a file, change the permissions, and change the owner<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo touch \/var\/log\/time.log\nsudo chmod 660 \/var\/log\/time.log\nsudo chown pac-man:adm \/var\/log\/time.log\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# Example output\n\npac-man@lab-vm:\/var\/log$ sudo touch \/var\/log\/time.log\npac-man@lab-vm:\/var\/log$ sudo chmod 660 \/var\/log\/time.log\npac-man@lab-vm:\/var\/log$ sudo chown pac-man:adm \/var\/log\/time.log\npac-man@lab-vm:\/var\/log$ sudo service time_logger start\npac-man@lab-vm:\/var\/log$ ll |grep time.log\n-rw-rw----   1 pac-man   adm                138 Jul 12 07:04 time.log\npac-man@lab-vm:\/var\/log$\npac-man@lab-vm:\/var\/log$ cat time.log\nCurrent time: Fri 12 Jul 2024 07:02:23 AM UTC\nCurrent time: Fri 12 Jul 2024 07:03:23 AM UTC\nCurrent time: Fri 12 Jul 2024 07:04:23 AM UTC\nCurrent time: Fri 12 Jul 2024 07:05:23 AM UTC\npac-man@lab-vm:\/var\/log$\n\n\n<\/pre><\/div>\n\n<p><\/p>\n<p style=\"text-align: center;\"><strong>Troubleshooting<\/strong><\/p>\n<p style=\"text-align: left;\">If there are issues with the service, you can use the journalctl command to view logs:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo journalctl -u time_logger.service\n<\/pre><\/div>\n\n<p><\/p>\n<p style=\"text-align: center;\"><strong>Example of a Full-Service<\/strong><\/p>\n<p style=\"text-align: justify;\">As an example, let&#8217;s take the nginx web server and create a custom service for it (even though it already has a built-in service):<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n# Install NGINX\nsudo apt-get update\nsudo apt-get install nginx\n<\/pre><\/div>\n\n<p><\/p>\n<p style=\"text-align: center;\"><strong>Create the Service File<\/strong><\/p>\n<p>Create the file&nbsp; \/etc\/systemd\/system\/custom_nginx.service and put the code below<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;Unit]\nDescription=Custom Nginx Service\nAfter=network.target\n\n&#x5B;Service]\nExecStart=\/usr\/sbin\/nginx -g 'daemon off;'\nExecReload=\/bin\/kill -s HUP $MAINPID\nExecStop=\/bin\/kill -s QUIT $MAINPID\nPIDFile=\/run\/nginx.pid\nRestart=on-failure\n\n&#x5B;Install]\nWantedBy=multi-user.target\n\n<\/pre><\/div>\n\n<p style=\"text-align: center;\"><strong>Start and Manage<\/strong><\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo systemctl daemon-reload\nsudo systemctl start custom_nginx.service\nsudo systemctl enable custom_nginx.service\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Creating and managing services in Ubuntu Server 18 is essential for system administrators. In this article, we will explore how to create custom services using systemd. We will explain each option in detail and provide examples. Essential Steps to Create a Service Let&#8217;s start by creating a simple script. Suppose we have a script that &hellip; <a href=\"https:\/\/itgen.itbumper.com\/?page_id=245\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;0004_services&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=\/wp\/v2\/pages\/245"}],"collection":[{"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=245"}],"version-history":[{"count":9,"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=\/wp\/v2\/pages\/245\/revisions"}],"predecessor-version":[{"id":260,"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=\/wp\/v2\/pages\/245\/revisions\/260"}],"wp:attachment":[{"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}