{"id":83,"date":"2022-04-23T14:01:33","date_gmt":"2022-04-23T14:01:33","guid":{"rendered":"https:\/\/hb9iiu.com\/?p=83"},"modified":"2022-04-23T14:01:33","modified_gmt":"2022-04-23T14:01:33","slug":"how-to-create-a-systemd-service-in-linux","status":"publish","type":"post","link":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/2022\/04\/23\/how-to-create-a-systemd-service-in-linux\/","title":{"rendered":"How to create a Systemd service in Linux"},"content":{"rendered":"\n<p>At times you create a script and then you want to have the scripts controlled by systemd or in some cases you wish to have the scripts getting restarted by itself when it is killed due to some reason. <\/p>\n\n\n\n<p>In such cases <strong>systemd<\/strong> in Linux helps to configure services which can be managed.<\/p>\n\n\n\n<p> To do so follow the following steps.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>cd \/etc\/systemd\/system<\/li><li>Create a file named <strong>your-service.service<\/strong> and include the follow<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">[Unit]\nDescription=&lt;description about this service&gt;\n\n[Service]\nUser=&lt;user e.g. root&gt;\nWorkingDirectory=&lt;directory_of_script e.g. \/root&gt;\nExecStart=&lt;script which needs to be executed&gt;\nRestart=always\n\n[Install]\nWantedBy=multi-user.target<\/pre>\n\n\n\n<p>For Python specific projects which include virtual environment:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[Unit]\nDescription=&lt;project description&gt;\n\n[Service]\nUser=&lt;user e.g. root&gt;\nWorkingDirectory=&lt;path to your project directory containing your python script&gt;\nExecStart=\/home\/user\/.virtualenv\/bin\/python main.py\nRestart=always\n# replace \/home\/user\/.virtualenv\/bin\/python with your virtualenv and main.py with your script\n\n[Install]\nWantedBy=multi-user.target<\/pre>\n\n\n\n<p>or<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[Unit]\nDescription=&lt;project description&gt;\n\n[Service]\nUser=&lt;user e.g. root&gt;\nWorkingDirectory=&lt;path to your project directory&gt;\nExecStart=\/bin\/bash -c 'cd \/home\/ubuntu\/project\/ &amp;&amp; source venv\/bin\/activate &amp;&amp; python test.py'\n\n[Install]\nWantedBy=multi-user.target\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Reload the service file to include the new one<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo systemctl daemon-reload<\/code>\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">To check the status of the service<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo systemctl status example.service<\/code>\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">To enable the service on every reboot<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo systemctl&nbsp;enable example.service<\/code>\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">To&nbsp;disable&nbsp;the service on every reboot<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo systemctl&nbsp;disable example.service<\/code>\n<\/pre>\n\n\n","protected":false},"excerpt":{"rendered":"<p>At times you create a script and then you want to have the scripts controlled by systemd or in some cases you wish to have the scripts getting restarted by itself when it is killed due to some reason. In&#8230; <a href=\"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/2022\/04\/23\/how-to-create-a-systemd-service-in-linux\/\" class=\"readmore\">Read more<span class=\"screen-reader-text\">How to create a Systemd service in Linux<\/span><span class=\"fa fa-angle-double-right\" aria-hidden=\"true\"><\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-83","post","type-post","status-publish","format-standard","hentry","category-linux","content-layout-excerpt-thumb"],"_links":{"self":[{"href":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/83","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=83"}],"version-history":[{"count":0,"href":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/83\/revisions"}],"wp:attachment":[{"href":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=83"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=83"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}