{"id":7346,"date":"2018-11-26T19:49:36","date_gmt":"2018-11-26T16:49:36","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-service-command\/"},"modified":"2018-11-26T19:49:36","modified_gmt":"2018-11-26T16:49:36","slug":"linux-service-command-tutorial-for-beginners-with-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-service-command-tutorial-for-beginners-with-examples\/","title":{"rendered":"Linux service Command Tutorial for Beginners (with Examples)"},"content":{"rendered":"<article readability=\"45.733360555329\">\n<p>The service command lets you run a System V init script. In this tutorial, we will briefly discuss this tool using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.<\/p>\n<h2 id=\"linux-service-command\">Linux service command<\/h2>\n<p>Here are the available syntax for the service command in Linux:<\/p>\n<pre class=\"command\" readability=\"4\"><code spellcheck=\"false\" readability=\"2\">service SCRIPT COMMAND [OPTIONS]<p>service --status-all<\/p><p>service --help | -h | --version<\/p><\/code><\/pre>\n<p>And here&#8217;s how the man page explains it:<\/p>\n<pre readability=\"16\"><code spellcheck=\"false\" readability=\"26\">service\u00a0 runs\u00a0 a System V init script or systemd unit in as predictable an environment as possible,<br\/>removing most environment variables and with the current working directory set to \/.<p>The SCRIPT parameter specifies a System V init script, located in \/etc\/init.d\/SCRIPT, or the name <br\/>of a systemd unit. The\u00a0 existence of\u00a0 a\u00a0 systemd unit of the same name as a script in \/etc\/init.d <br\/>will cause the unit to take precedence over the init.d script. The supported values of COMMAND <br\/>depend on the invoked script. service passes COMMAND\u00a0 and OPTIONS to the init\u00a0 script\u00a0 unmodified.<br\/>For systemd units, start, stop, status, and reload are passed through to their systemctl\/initctl <br\/>equivalents.<\/p><p>All\u00a0 scripts\u00a0 should\u00a0 support at least the start and stop commands.\u00a0 As a special case, if COMMAND <br\/>is --full-restart, the script is run twice, first with the stop command, then with the start <br\/>command.<\/p><p>service --status-all runs all init scripts, in alphabetical order, with the status command. The <br\/>status is [ + ]\u00a0 for\u00a0 running\u00a0 services,\u00a0 [\u00a0 -\u00a0 ]\u00a0 for\u00a0 stopped services and [ ? ] for services <br\/>without a status command.\u00a0 This option only calls status for sysvinit jobs.<\/p><\/code><\/pre>\n<h2 id=\"how-to-use-service-command\">How to use service command?<\/h2>\n<p>The usage of the &#8216;service&#8217; command line tool isn&#8217;t complicated at all. Let&#8217;s talk about how you can use this utility when it comes to handling scripts like httpd and sshd.<\/p>\n<p>Here&#8217;s how you run a script using service:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">service httpd start<\/code><\/pre>\n<p>Here&#8217;s how you stop an already running script using service:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">service sshd stop<\/code><\/pre>\n<p>Here&#8217;s how you fetch status for a script:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">service httpd status<\/code><\/pre>\n<p>Here&#8217;s how you restart and already-running service:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">service sshd restart<\/code><\/pre>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Depending on your\u00a0<span>privileges<\/span> as Linux user, you may want to run all the above examples using the &#8216;sudo&#8217; command. To learn more about the &#8216;service&#8217; command, head to its <a href=\"https:\/\/linux.die.net\/man\/8\/service\" target=\"_blank\" rel=\"noopener\">man page<\/a>.<\/p>\n<\/article>\n<aside class=\"authorbox\" readability=\"10\">\n<img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-service-command-tutorial-for-beginners-with-examples.jpg\" alt=\"Himanshu Arora\" title=\"\"> <\/p>\n<p><strong>About Himanshu Arora<\/strong><\/p>\n<p>Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu&#8217;s work has also been featured in some of world&#8217;s other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.<\/p>\n<\/aside>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Flinux-service-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-service-command-tutorial-for-beginners-with-examples.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-service-command%2F&amp;text=Linux+service+Command+Tutorial+for+Beginners+%28with+Examples%29&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-service-command-tutorial-for-beginners-with-examples-1.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-service-command-tutorial-for-beginners-with-examples-2.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-service-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-service-command-tutorial-for-beginners-with-examples-3.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The service command lets you run a System V init script. In this tutorial, we will briefly discuss this tool using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine. Linux service command Here are the available syntax [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-7346","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7346","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=7346"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7346\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=7346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=7346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=7346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}