{"id":4678,"date":"2018-06-04T19:55:16","date_gmt":"2018-06-04T15:55:16","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-pidof-command\/"},"modified":"2018-06-04T19:55:16","modified_gmt":"2018-06-04T15:55:16","slug":"linux-pidof-command-tutorial-for-beginners-5-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-pidof-command-tutorial-for-beginners-5-examples\/","title":{"rendered":"Linux pidof Command Tutorial for Beginners (5 Examples)"},"content":{"rendered":"<p>Linux command line offers a lot of utilities that work with processes. Once such tool is <strong>pidof<\/strong>, which &#8211; as the name suggests &#8211; gives you the process ID of an already executing process. In this tutorial, we will discuss the basics of pidof using some easy to understand examples.<\/p>\n<p>But before we do that, it&#8217;s worth mentioning that all examples here have been tested on an Ubuntu 16.04 LTS machine.<\/p>\n<h2 id=\"linux-pidof-command\">Linux pidof command<\/h2>\n<p>As already mentioned above, the pidof command lets you find the process ID of a running program. Following is its syntax:<\/p>\n<p class=\"command\">pidof [options] command<\/p>\n<p>And here&#8217;s what the man page says about the tool:<\/p>\n<pre> Pidof finds the process id's (pids) of the named\u00a0 programs.\u00a0 It\u00a0 prints<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 those id's on the standard output. This program is on some systems used<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 in run-level change scripts, especially when the system has a\u00a0 System-V<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 like\u00a0\u00a0 rc\u00a0 structure.\u00a0 In\u00a0 that\u00a0 case\u00a0 these\u00a0 scripts\u00a0 are\u00a0 located\u00a0 in<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/etc\/rc?.d, where ? is the runlevel. If the system\u00a0 has\u00a0 a\u00a0 start-stop-<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 daemon (8) program that should be used instead.<\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a good idea on how the tool works.<\/p>\n<h2 id=\"q-how-to-use-pidof-command\">Q1. How to use pidof command?<\/h2>\n<p>Basic usage is fairly simple &#8211; just\u00a0 pass the name of the program as input to the command. For example:<\/p>\n<p class=\"command\">pidof gedit<\/p>\n<p><a class=\"fancybox\" id=\"img-pidof-basic-usage\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/pidof-basic-usage.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-pidof-command-tutorial-for-beginners-5-examples.png\" alt=\"How to use pidof command\" width=\"256\" height=\"35\" title=\"\"><\/a><\/p>\n<p>So you can see the command produced gedit&#8217;s process ID in the output.<\/p>\n<h2 id=\"q-how-to-make-pidof-return-only-single-pid\">Q2. How to make pidof return only single pid?<\/h2>\n<p>Sometimes, you&#8217;ll see the pidof command returns multiple pids. For example, try running the pidof command with a web browser program as input.<\/p>\n<p class=\"command\">pidof firefox<\/p>\n<p>Following is the output produced on my system:<\/p>\n<p><a class=\"fancybox\" id=\"img-pidof-firefox\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/pidof-firefox.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-pidof-command-tutorial-for-beginners-5-examples-1.png\" alt=\"pidof command example\" width=\"272\" height=\"34\" title=\"\"><\/a><\/p>\n<p>However, in case you want, you can force the tool to only produce a single PID in output. This can be done using the -s command line option.<\/p>\n<p class=\"command\">pidof -s firefox<\/p>\n<p><a class=\"fancybox\" id=\"img-pidof-s-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/pidof-s-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-pidof-command-tutorial-for-beginners-5-examples-2.png\" alt=\"pidof command single pid\" width=\"306\" height=\"70\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-limit-output-based-on-root-directory\">Q3. How to limit output based on root directory?<\/h2>\n<p>If you want pidof to only return process ids that are running with the same root directory, use the -c command line option.<\/p>\n<p class=\"command\">pidof -c program_name<\/p>\n<p>Please note that this option is ignored for non-root users, as they will be unable to check the current root directory of processes<br \/>they do not own.<\/p>\n<h2 id=\"q-how-to-use-pidof-in-case-of-binaries-kept-on-network-file-systems\">Q4. How to use pidof in case of binaries kept on network file systems?<\/h2>\n<p>While dealing with binaries located on network-based file systems like NFS, you can use the -n command line option to make pidof command avoid using the stat system function call.<\/p>\n<p class=\"command\">pidof -n program_name<\/p>\n<p>Please note that the man page suggests &#8220;instead of using this option the the variable PIDOF_NETFS may be set and exported.&#8221;<\/p>\n<h2 id=\"q-how-pidof-works-internally\">Q5. How pidof works internally?<\/h2>\n<p>Official docs say pidof is actually the same program as killall. The program behaves according to the name under which it is called. Here&#8217;s what the man page exactly says:<\/p>\n<pre> When pidof is invoked with a full pathname to the program it should<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 find the pid of, it is reasonably safe. Otherwise it is\u00a0 possible\u00a0 that<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 it\u00a0 returns\u00a0 pids of running programs that happen to have the same name<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 as the program you're after but are actually other programs. Note\u00a0 that<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 that\u00a0 the executable name of running processes is calculated with read?<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 link(2), so symbolic links to executables will also match.<\/pre>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>The pidof command may come in handy in many situations. For example, if you want to kill a process whose PID you don&#8217;t know, you can quickly use this tool to find the process id and the process can be killed within no time. The pidof command doesn&#8217;t offer many options, and we&#8217;ve already discussed majority of them here. For more info, head to the tool&#8217;s <a href=\"https:\/\/linux.die.net\/man\/8\/pidof\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a>.<\/p>\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-pidof-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-pidof-command-tutorial-for-beginners-5-examples-3.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-pidof-command%2F&amp;text=Linux+pidof+Command+Tutorial+for+Beginners+%285+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\/06\/linux-pidof-command-tutorial-for-beginners-5-examples-4.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\/06\/linux-pidof-command-tutorial-for-beginners-5-examples-5.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-pidof-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-pidof-command-tutorial-for-beginners-5-examples-6.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Linux command line offers a lot of utilities that work with processes. Once such tool is pidof, which &#8211; as the name suggests &#8211; gives you the process ID of an already executing process. In this tutorial, we will discuss the basics of pidof using some easy to understand examples. But before we do that, &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-4678","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/4678","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=4678"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/4678\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=4678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=4678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=4678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}