{"id":2557,"date":"2019-02-24T08:47:04","date_gmt":"2019-02-24T08:47:04","guid":{"rendered":"https:\/\/davidpapkin.net\/?p=2557"},"modified":"2019-02-24T08:47:04","modified_gmt":"2019-02-24T08:47:04","slug":"adding-startup-service-with-sysvinit-david-papkin","status":"publish","type":"post","link":"https:\/\/davidpapkin.com\/?p=2557","title":{"rendered":"Adding Startup Service with SysVinit by David Papkin"},"content":{"rendered":"<p>In this post by David Papkin\u00a0, will show how to create a dummy service named fake_service.<\/p>\n<p>In this and the following exercise, we will create a simple startup service.<\/p>\n<p><strong>First we will do it for a SysVinit system.<\/strong><br \/>\nNote that if you are using a systemd-based system everything should still work because of the backwards compatibility\u00a0layer that all distributions utilize. However, in the next exercise we will do natively for systemd.<br \/>\nIf you are on a Debian-based system like Ubuntu, make sure you have installed the sysvinit-utils and chkconfig\u00a0packages. However, recent versions of Ubuntu no longer package chkconfig; you\u2019ll have to use the update-rc.d utility\u00a0instead.<br \/>\nFirst we have to create the service-specific script; you can create one of your own for fun, or to get the procedure down\u00a0just (as root) create a file named \/etc\/init.d\/fake_service containing the following content:<br \/>\n#!\/bin\/bash<br \/>\n# fake_service<br \/>\n# Starts up, writes to a dummy file, and exits<br \/>\n#<br \/>\n# chkconfig: 35 69 31<br \/>\n# description: This service doesn\u2019t do anything.<br \/>\n# Source function library<br \/>\n. \/etc\/sysconfig\/fake_service<br \/>\ncase &#8220;$1&#8221; in<br \/>\nstart) echo &#8220;Running fake_service in start mode&#8230;&#8221;<br \/>\ntouch \/var\/lock\/subsys\/fake_service<br \/>\necho &#8220;$0 start at $(date)&#8221; &gt;&gt; \/var\/log\/fake_service.log<br \/>\nif [ ${VAR1} = &#8220;true&#8221; ]<br \/>\nthen<br \/>\necho &#8220;VAR1 set to true&#8221; &gt;&gt; \/var\/log\/fake_service.log<br \/>\nfi<br \/>\necho<br \/>\n;;<br \/>\nstop)<br \/>\necho &#8220;Running the fake_service script in stop mode&#8230;&#8221;<br \/>\necho &#8220;$0 stop at $(date)&#8221; &gt;&gt; \/var\/log\/fake_service.log<br \/>\nif [ ${VAR2} = &#8220;true&#8221; ]<br \/>\nthen<br \/>\necho &#8220;VAR2 = true&#8221; &gt;&gt; \/var\/log\/fake_service.log<br \/>\nfi<br \/>\nrm -f \/var\/lock\/subsys\/fake_service<br \/>\necho<br \/>\n;;<br \/>\n*)<br \/>\necho &#8220;Usage: fake_service {start | stop}&#8221;<br \/>\nexit 1<br \/>\nesac<br \/>\nexit 0<br \/>\nMake the file above executable and give other proper permissions:<br \/>\n$ sudo chmod 755 \/etc\/init.d\/fake_service<br \/>\nLFS201: V 1.0 c Copyright the Linux Foundation 2015. All rights reserved.<br \/>\n2 CHAPTER 4. INIT: SYSTEMV, UPSTART, SYSTEMD<br \/>\nYou\u2019ll notice the script includes the file \/etc\/sysconfig\/fake service. (On non-RHEL systems you should change<br \/>\nthis to \/etc\/default\/fake_service.) Create it and give it the following contents:<br \/>\nVAR1=&#8221;true&#8221;<br \/>\nVAR2=&#8221;true&#8221;<br \/>\nTest to see if the script works properly by running the following commands:<br \/>\n$ sudo service fake_service<br \/>\n$ sudo service fake_service start<br \/>\n$ sudo service fake_service stop<br \/>\nLook at the file named \/var\/log\/fake service.log. What does it contain?<br \/>\nFor fun you can add additional modes like restart to the script file; look at other scripts in the directory to get examples\u00a0of what to do.<br \/>\nNext we will want to have the ability to start fake service whenever the system starts, and stop it when it shuts down.<br \/>\nIf you do:<br \/>\n$ sudo chkconfig &#8211;list fake_service<br \/>\nyou will get an error as it hasn\u2019t been set up yet for this. You can easily do this with:<br \/>\n$ sudo chkconfig &#8211;add fake_service<br \/>\nand you can turn it on or off at boot time with<br \/>\n$ sudo chkconfig fake_service on<br \/>\n$ sudo chkconfig fake_service off<br \/>\nTo test this completely you\u2019ll have to reboot the system to see if it comes on automatically. You can also try varying the\u00a0runlevels in which the service is running.<\/p>\n<p>&nbsp;<\/p>\n<p>Next we will do it for\u00a0<strong>systemd<\/strong><\/p>\n<p><span class=\"fontstyle0\">The analogous procedure is to create (as root) a file directly under <\/span><span class=\"fontstyle2\">\/etc\/systemd\/system <\/span><span class=\"fontstyle0\">or somewhere else in that directory tree; distributions have some varying tastes on this. For example a very minimal file named <\/span><span class=\"fontstyle2\">\/etc\/systemd\/system\/fake2.service<\/span><span class=\"fontstyle0\">:<br \/>\n<\/span><span class=\"fontstyle3\">[Unit]<br \/>\nDescription=fake2<br \/>\nAfter=network.target<br \/>\n[Service]<br \/>\nExecStart=\/bin\/sh -c \u2019\/bin\/echo I am starting the fake2 service ; \/bin\/sleep 30\u2019<br \/>\nExecStop=\/bin\/echo I am stopping the fake2 service<br \/>\n[Install]<br \/>\nWantedBy=multi-user.target<br \/>\n<\/span><span class=\"fontstyle0\">Now there are many things that can go in this <\/span><span class=\"fontstyle4\">unit <\/span><span class=\"fontstyle0\">file. The <\/span><span class=\"fontstyle2\">After=network.target <\/span><span class=\"fontstyle0\">means the service should start only after the network does, while the <\/span><span class=\"fontstyle2\">WantedBy=multi-user.target <\/span><span class=\"fontstyle0\">means it should start when we reach multiple-user mode. This is equivalent to runlevels 2 and 3 in <\/span><span class=\"fontstyle4\">SysVinit<\/span><span class=\"fontstyle0\">. Note <\/span><span class=\"fontstyle2\">graphical.target <\/span><span class=\"fontstyle0\">would correlate with runlevel 5.<br \/>\nNow all we have to do to start, stop and check the service status are to issue the commands:<br \/>\n<\/span><span class=\"fontstyle3\">$ sudo systemctl start fake2.service<br \/>\n$ sudo systemctl status fake2.service<br \/>\n$ sudo systemctl stop fake2.service<br \/>\n<\/span><span class=\"fontstyle0\">If you are fiddling with the unit file while doing this you\u2019ll need to reload things with:<br \/>\n<\/span><span class=\"fontstyle3\">$ sudo systemctl daemon-reload <\/span><span class=\"fontstyle0\">as the system will warn you.<br \/>\nTo keep an eye directly on the output you can do:<br \/>\n<\/span><span class=\"fontstyle3\">$ sudo tail -f \/var\/log\/messages<br \/>\n<\/span><span class=\"fontstyle0\">(use <\/span><span class=\"fontstyle2\">\/var\/log\/syslog <\/span><span class=\"fontstyle0\">on <\/span><span class=\"fontstyle4\">Ubuntu<\/span><span class=\"fontstyle0\">) either in background or in another windows while the service is running.<br \/>\nTo set things up so the service turns on or off on system boot:<br \/>\n<\/span><span class=\"fontstyle3\">$ sudo systemctl enable fake2.service<br \/>\n$ sudo systemctl disable fake2.service<\/span><\/p>\n<p>The original source from\u00a0<a href=\"https:\/\/www.linuxfoundation.org\/\">https:\/\/www.linuxfoundation.org\/<\/a>\u00a0is<\/p>\n<p><a href=\"https:\/\/lms.360training.com\/scorm\/linuxfoundation\/LFS201\/Lab%204.1.pdf\">https:\/\/lms.360training.com\/scorm\/linuxfoundation\/LFS201\/Lab%204.1.pdf<\/a>. It is posted here for ease of access for students doing scripting.<\/p>\n<p>Please support <a href=\"https:\/\/davidpapkin.net\/david-papkin-links\/linux-links\/\">Linux<\/a> by visiting\u00a0\u00a0<a href=\"https:\/\/www.linuxfoundation.org\/\">https:\/\/www.linuxfoundation.org\/<\/a><\/p>\n<p>This concludes this post by David Papkin on how to create a simple startup service<\/p>\n<p><a href=\"http:\/\/davidpapkin.org\/\">http:\/\/davidpapkin.org\/<\/a><\/p>\n<p><strong>David Papkin favorite movies<\/strong><\/p>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/GoodFellas\">Robert Deniro in GoodFellas<\/a><\/p>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Singapore_(1947_film)\">Ava Gardner in Singapore (Flim Noir)<\/a><\/p>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/China_Seas_(film)\">Clarke Gable in China Seas<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post by David Papkin\u00a0, will show how to create a dummy service named fake_service. In this and the following exercise, we will create a simple startup service. First we will do it for a SysVinit system. Note that&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,7,15],"tags":[52,56,67,70,99,146],"class_list":["post-2557","post","type-post","status-publish","format-standard","hentry","category-david-papkin","category-linux","category-singapore","tag-bash","tag-clarkegable","tag-david-papkin","tag-davidpapkin","tag-linux","tag-singapore"],"_links":{"self":[{"href":"https:\/\/davidpapkin.com\/index.php?rest_route=\/wp\/v2\/posts\/2557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davidpapkin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davidpapkin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davidpapkin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davidpapkin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2557"}],"version-history":[{"count":0,"href":"https:\/\/davidpapkin.com\/index.php?rest_route=\/wp\/v2\/posts\/2557\/revisions"}],"wp:attachment":[{"href":"https:\/\/davidpapkin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davidpapkin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davidpapkin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}