{"id":9654,"date":"2014-09-17T05:03:52","date_gmt":"2014-09-16T19:03:52","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=9654"},"modified":"2015-05-23T14:34:40","modified_gmt":"2015-05-23T04:34:40","slug":"linux-sudo-nohup-watchdog-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/linux-sudo-nohup-watchdog-primer-tutorial\/","title":{"rendered":"Linux sudo nohup Watchdog Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Linux\/index.html\"><img decoding=\"async\" id='xlii' style=\"float:left; border: 15px solid pink;\" alt=\"Linux sudo nohup Watchdog Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Linux\/Linux_watchdog-52of.jpg\" title=\"Linux sudo nohup Watchdog Primer Tutorial\" onmouseover=\"  this.src = this.src.replace('-0of.jpg', '-50of.JPG').replace('-50of.jpg', '-51of.JPG').replace('-51of.jpg', '-52of.JPG').replace('-52of.jpg', '-99of.JPG').replace('-99of.jpg', '-0of.JPG').replace('JPG', 'jpg');    \" \/><\/a><p class=\"wp-caption-text\">Linux sudo nohup Watchdog Primer Tutorial<\/p><\/div>\n<p>Even though it is not good to generalize too much about the workings of a <a target=_blank title='Watchdog process information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Watchdog_timer'>watchdog<\/a> process (because of environmental differences), even so there are some features that can be pointed out for some Linux ideas, which we&#8217;ll discuss regarding Korn Shell scripting:<\/p>\n<ul>\n<li>we model the slightly generic approach on what we&#8217;d do to imagine the resurrection of <a target=_blank href='http:\/\/linux.about.com\/od\/commands\/l\/blcmdl5_crontab.htm' title='crontab information'><i>cron<\/i>tab<\/a> should it fall over &#8230; in researching this, found that it is very hard to knock <i>cron<\/i>tab over for any length of time, because it already protects itself &#8230; this brings up the point though, that if you are writing a watchdog for <i>cron<\/i>tab it should not be run via <i>cron<\/i>tab &#8230; doh &#8230; so that is why there is code in our watchdog to use the Linux command <a target=_blank title='at information' href='http:\/\/linux.about.com\/od\/nwb_guide\/a\/gdenwb01t47s01.htm'><i>at<\/i><\/a> as an alternative (method) &#8230; but for our generic thinking watchdog we lean towards\n<p>  <i>sudo nohup [watchdog] > [output] 2> [errfile] &#038;<\/i> <\/p>\n<p> &#8230; this also involves allowing our process that runs all the time to:<\/p>\n<ol>\n<li>be on the Linux path with good <a target=_blank title='chmod information' href='http:\/\/linux.about.com\/od\/commands\/l\/blcmdl1_chmod.htm'><i>chmod<\/i><\/a> permissions<\/li>\n<li>be written to have functionality to match that of <i>cron<\/i>tab&#8217;s argument values &#8230; &#8220;start&#8221;, &#8220;restart&#8221;, &#8220;stop&#8221;<\/li>\n<\/ol>\n<\/li>\n<li>it would be good not to need to use <a target=_blank title='kill information' href='http:\/\/linux.about.com\/library\/cmd\/blcmdl_kill.htm'><i>kill -9 [pidlist]<\/i><\/a> but the watchdog is difficult to arrange without this usage &#8230; ie. great care is needed with watchdogs &#8230; doh<\/li>\n<li>in conjunction with <i>kill -9 [pidlist]<\/i> the use of Korn Shell&#8217;s <i>$$<\/i> to represent the current process&#8217;s <a target=_blank title='pid information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Process_identifier'>pid<\/a> becomes important<\/i>\n<li>use of <a target=_blank href='http:\/\/linux.about.com\/od\/commands\/l\/blcmdl8_sudo.htm' title='Linux sudo information'>sudo<\/a> to be best able to control Linux processes<\/li>\n<li>use of <a target=_blank href='http:\/\/linux.about.com\/library\/cmd\/blcmdl1_nohup.htm' title='Linux nohup information'>nohup<\/a> to avoid the necessity of being logged on &#8230; even so, we are not recommending the code here for occasions when a user is not logged in<\/li>\n<li>use of &#038; to commands to run them in the background<\/li>\n<li><a target=_blank title='ps information' href='http:\/\/linux.about.com\/library\/cmd\/blcmdl_kill.htm'>ps -ef<\/a> &#8212; for process list in Linux<\/li>\n<li>the watchdog restarts itself after one cycle of looking, with <i>sudo nohup [watchdog] > [output] 2> [errfile] &#038;<\/i> rather than using <i>cron<\/i>tab usage for logged in interactive mode of use, primarily &#8230; though the code is close to being okay to add two commands to \/etc\/profile &#8230; as discussed in previous <a target=_blank href='#lbslpt' title='Linux Bash Shell Login Primer Tutorial'>Linux Bash Shell Login Primer Tutorial<\/a> as shown below (the date and time on the prompt can be useful)<\/li>\n<li>most of watchdog work is in testing scenarios, and trying to imagine the worst, unless you have evidence to the contrary &#8230; especially regarding:\n<ol>\n<li>program modes of access<\/li>\n<li>user priviledges<\/li>\n<li>file priviledges<\/li>\n<\/ol>\n<\/li>\n<li>not done (in the code) here, but please leave comments, as required, regarding how to use the watchdog, and why, and when, and where, and who<\/li>\n<\/ul>\n<p>Here is Linux Korn shell script idea for a process that runs all the time (designed for our Mac OS X Terminal application Bash environment) &#8230; we knock it over and get the watchdog to bring it back up &#8230; you could call it <a target=_blank title='thing_that_runs_and_runs' href='http:\/\/www.rjmprogramming.com.au\/Linux\/thing_that_runs_and_runs.GETME'>thing_that_runs_and_runs<\/a><\/p>\n<p>Here is Linux Korn shell script idea for the proposed watchdog (just tailor its use for ideas of your own) &#8230; we knock over the process above and get this watchdog to bring it back up &#8230; you could call it <a target=_blank title='cron_check.ksh' href='http:\/\/www.rjmprogramming.com.au\/Linux\/cron_check.ksh-GETME'>cron_check.ksh<\/a><\/p>\n<p>The way it went from a first watchdog concept to resurrect <i>cron<\/i>tab to being a more generic watchdog for system-wide processes that run forever a bit like <i>cron<\/i>tab see the differences link <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/Linux\/cron_check.ksh-GETME' title='cron_check.ksh differences from an earlier incarnation just for crontab'>here<\/a> &#8230; and for a good related research topic in Linux in this area look up <a target=_blank title='Daemon process information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Daemon_%28computing%29'>daemon<\/a> processes ( in some systems (eg. CentOS)  they live in \/etc\/init.d\/ )<\/p>\n<p>A preliminary discussion about watchdog processes at this blog occurred at <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=4495' title='Linux Watchdog Primer Tutorial'>Linux Watchdog Primer Tutorial<\/a>.<\/p>\n<p>The book <i>MySQL &amp; mSQL<\/i> by Randy Jay Yarger, George Reese &amp; Tim King has a good section on pages 74 and 75 regarding a watchdog idea for MySQL process protection.<\/p>\n<p>Please enjoy today&#8217;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Linux\/index.html\" title=\"Click picture\">tutorial<\/a> ideas.<\/p>\n<hr \/>\n<p id=\"lbslpt\">Previous relevant <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=9645' title='Linux Bash Shell Login Primer Tutorial'>Linux Bash Shell Login Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Linux\/Linux_prompt-4of.jpg\"><img decoding=\"async\" id='lii' style=\"float:left; border: 15px solid pink;\" alt=\"Linux Bash Shell Login Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Linux\/Linux_prompt-4of.jpg\" title=\"Linux Bash Shell Login Primer Tutorial\" onmouseover=\"  this.src = this.src.replace('.jpg', '.GIF').replace('.gif', '.jpg').replace('.GIF', '.gif');    \" \/><\/a><p class=\"wp-caption-text\">Linux Bash Shell Login Primer Tutorial<\/p><\/div>\n<p>When you login to Linux the operating system looks to a file called &#8230;<\/p>\n<p>\/etc\/profile<\/p>\n<p>&#8230; to perform its commands.<\/p>\n<p>Within that file, if you are in a default Bash environment shell, as we are with Mac OS X application Terminal you may see a line of code, as highlighted in <font color='red'>red<\/font> like &#8230;<\/p>\n<p><code><br \/>\n# System-wide .profile for sh(1)<\/p>\n<p>if [ -x \/usr\/libexec\/path_helper ]; then<br \/>\n\teval `\/usr\/libexec\/path_helper -s`<br \/>\nfi<\/p>\n<p>if [ \"${BASH-no}\" != \"no\" ]; then<br \/>\n\t<font color='red'>[ -r \/etc\/bashrc ] && . \/etc\/bashrc<\/font><br \/>\nfi<br \/>\n<\/code><\/p>\n<p>&#8230; which then proceeds to take commands from a file called &#8230;<\/p>\n<p>\/etc\/bashrc<\/p>\n<p>&#8230; which may contain code like &#8230;<\/p>\n<p><code><br \/>\n# System-wide .bashrc file for interactive bash(1) shells.<br \/>\nif [ -z \"$PS1\" ]; then<br \/>\n   return<br \/>\nfi<\/p>\n<p>PS1='h:W u$ '<br \/>\n# Make bash check its window size after a process completes<br \/>\nshopt -s checkwinsize<br \/>\n# Tell the terminal about the working directory at each prompt.<br \/>\nif [ \"$TERM_PROGRAM\" == \"Apple_Terminal\" ] && [ -z \"$INSIDE_EMACS\" ]; then<br \/>\n    update_terminal_cwd() {<br \/>\n        # Identify the directory using a \"file:\" scheme URL,<br \/>\n        # including the host name to disambiguate local vs.<br \/>\n        # remote connections. Percent-escape spaces.<br \/>\n\tlocal SEARCH=' '<br \/>\n\tlocal REPLACE='%20'<br \/>\n\tlocal PWD_URL=\"file:\/\/$HOSTNAME${PWD\/\/$SEARCH\/$REPLACE}\"<br \/>\n\tprintf 'e]7;%sa' \"$PWD_URL\"<br \/>\n    }<br \/>\n    PROMPT_COMMAND=\"update_terminal_cwd; $PROMPT_COMMAND\"<br \/>\nfi<\/p>\n<p><\/code><\/p>\n<p><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Linux\/Linux_prompt-4of.jpg\"title=\"Click picture\">Today<\/a>, we change this \/etc\/bashrc file so that a current date and time is added to the Linux prompt (and for advice we thank <a target=_blank title='Bash prompt ideas' href='http:\/\/www.thegeekstuff.com\/2008\/09\/bash-shell-ps1-10-examples-to-make-your-linux-prompt-like-angelina-jolie\/'>this excellent link<\/a>) by the amendment in <font color='orange'>orange<\/font> as shown below for \/etc\/bashrc &#8230;<\/p>\n<p><code><br \/>\n# System-wide .bashrc file for interactive bash(1) shells.<br \/>\nif [ -z \"$PS1\" ]; then<br \/>\n   return<br \/>\nfi<\/p>\n<p>PS1='h:W u <font color='orange'>d [t]<\/font>$ '<br \/>\n# Make bash check its window size after a process completes<br \/>\nshopt -s checkwinsize<br \/>\n# Tell the terminal about the working directory at each prompt.<br \/>\nif [ \"$TERM_PROGRAM\" == \"Apple_Terminal\" ] && [ -z \"$INSIDE_EMACS\" ]; then<br \/>\n    update_terminal_cwd() {<br \/>\n        # Identify the directory using a \"file:\" scheme URL,<br \/>\n        # including the host name to disambiguate local vs.<br \/>\n        # remote connections. Percent-escape spaces.<br \/>\n\tlocal SEARCH=' '<br \/>\n\tlocal REPLACE='%20'<br \/>\n\tlocal PWD_URL=\"file:\/\/$HOSTNAME${PWD\/\/$SEARCH\/$REPLACE}\"<br \/>\n\tprintf 'e]7;%sa' \"$PWD_URL\"<br \/>\n    }<br \/>\n    PROMPT_COMMAND=\"update_terminal_cwd; $PROMPT_COMMAND\"<br \/>\nfi<\/p>\n<p><\/code><\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d9645' onclick='var dv=document.getElementById(\"d9645\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=Linux\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d9645' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr \/>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d9654' onclick='var dv=document.getElementById(\"d9654\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=watchdog\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d9654' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Even though it is not good to generalize too much about the workings of a watchdog process (because of environmental differences), even so there are some features that can be pointed out for some Linux ideas, which we&#8217;ll discuss regarding &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/linux-sudo-nohup-watchdog-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,29,37],"tags":[132,234,288,677,707,1565,725,841,997,1219,1319,1397],"class_list":["post-9654","post","type-post","status-publish","format-standard","hentry","category-elearning","category-operating-system","category-tutorials","tag-bash","tag-command-line","tag-daemon","tag-korn-shell","tag-linux","tag-login","tag-mac-os-x","tag-nohup","tag-programming","tag-sudo","tag-tutorial","tag-watchdog"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/9654"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=9654"}],"version-history":[{"count":1,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/9654\/revisions"}],"predecessor-version":[{"id":14980,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/9654\/revisions\/14980"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=9654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=9654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=9654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}