{"id":391,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:16","modified_gmt":"2020-08-22T20:26:16","slug":"this-is-the-page-title-toplevel-224","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=391","title":{"rendered":"Managing Scripts"},"content":{"rendered":"\n<title>Managing Scripts<\/title>\n<p>\nA very common use of shell scripts that you write is to automate work. If you need to run\nthe command by hand each time, it often defeats the intent of the automation. Therefore,\nit is also very common that commands are started from <tutorial id=78>cron<\/tutorial>.\n<\/p>\n<p>\nAs Murphy&#8217;s Law would have it, sometimes something will prevent the script from\nending. However, each time <glossary>cron<\/glossary> starts, a new process is\nstarted, so you end up with dozens, if not hundreds of processes. Depending on\nthe script, this could have a dramatic effect on the performance of your system.\nThe solution is to make sure that the process can only start once, or if it is\nalready running, you want to stop any previous instances.\n<\/p>\n<p>\nSo, the first question is how to figure out what processes are running, which is\nsomething we go into details about in <tutorial id=174>another section<\/tutorial>. In\nshort, you can use the <command>ps<\/command> command to see what processes are running:\n<\/p>\n<p>\n<commandexample command=\"grep\">ps aux | grep your_process_name | grep -v grep<\/commandexample>\n<\/p>\n<p>\nNote that when you run this command, it will also appear in the process table.\nSince your process name is an argument to the <command>grep<\/command> command, grep ends up finding\nitself. The <keyinput>grep -v grep<keyinput> says to skip entries that containing the word\n&#8220;grep&#8221; which means you do not find the command you just issued. Assuming that\nthe script is only started from <command>cron<\/command>, the only entries found will be those\nstarted by cron. If the return code of the command is 1, you know the process\nis running (or at least <command>grep<\/command> found a match.)\n<\/p>\n<p>\nIn your script, you check for the return code and if it is 1, the script exits,\notherwise it does the intended work. Alternatively, you can make the assumption\nthat if it is still running, there is a problem and you want to kill the\nprocess. You could use <command>ps<\/command>, <command>grep<\/command>, and <command>awk<\/command> to get the <glossary>PID<\/glossary> of\nthat processes (or even multiple processes). However, it is a lot easier using\nthe <command>pidof<\/command> command. You end up with something like this:\n<p>\n<commandexample command=\"kill\"> kill `pidof your_process_name`<\/commandexample>\n<\/p>\n<p>\nThe problem with that is the danger of killing a process that you hadn&#8217;t\nintended. Therefore, you need to be sure that you kill the correct process.\nThis is done by storing the <glossary>PID<\/glossary> of the process in a file\nand then checking for the existence of that file each time your scripts starts.\nIf the file does not exist, it is assumed the process is not running, so the\nvery next thing the script does is create the <glossary>PID<\/glossary> file.\nThis can could be done using the special variable $$ which is the process ID of the\ncurrect process, something like this:\n<\/p>\n<p>\n<commandexample command=\"echo\">echo $$ > PID_file<\/commandexample>\n<\/p>\n<p>\nThis is already done by many system processes and typically these\nfiles are stored in <directory>\/var\/run<\/directory> and have the ending .pid. Therefore, the file\ncontaining the <glossary>PID<\/glossary> of your HTTP server is <file type=\"\">\/var\/run\/httpd.pid<\/file>. You can then be sure you get the right process with a command like this:\n<\/p>\n<p>\n<commandexample command=\"kill\">kill `cat PID_FILE`<\/commandexample>\n<\/p>\n<p>\nWhere &#8220;PID_FILE&#8221; is the path to the file contain the PID.\n<\/p>\n<p>\nNote that in your script, you should first check for the existence of the\n<glossary>PID<\/glossary> file before you try to kill the process. If the process\ndoes not exist, but the <glossary>PID<\/glossary> file does, maybe the process died. Depending on how\nlong ago the process died, it is possible that the <glossary>PID<\/glossary> has been re-used and now\nbelongs to a completely different process. So as an added safety measure you could verify\nthat the <glossary>PID<\/glossary> belongs to the correct process.\n<\/p>\n<p>\nTo get some ideas on how existing scripts manage processes take a look at the\n<tutorial id=67>init scripts<\/tutorial> in <directory>\/etc\/rc.d<\/directory>.\n<\/p>\n<p>\n<!--DETAILS-->\nDetails on if-then constructs in scripts can be found <tutorial id=33>here<\/tutorial>.<br \/>\nDetails on using back-quotes can be found <tutorial id=20>here<\/tutorial>.<br \/>\nDetails on file redirection can be found <tutorial id=21>here<\/tutorial>.<br \/>\n<!--DETAILS-->\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Managing Scripts A very common use of shell scripts that you write is to automate work. If you need to run the command by hand each time, it often defeats the intent of the automation. Therefore, it is also very &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=391\">Continue reading <span class=\"meta-nav\">&rarr;<\/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":""},"class_list":["post-391","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=391"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/391\/revisions"}],"predecessor-version":[{"id":616,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/391\/revisions\/616"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}