{"id":558,"date":"2013-03-23T17:49:30","date_gmt":"2013-03-23T17:49:30","guid":{"rendered":"http:\/\/blog.stratus.org.uk\/?p=558"},"modified":"2013-03-23T17:51:18","modified_gmt":"2013-03-23T17:51:18","slug":"simple-shell-script-file-if-then-else","status":"publish","type":"post","link":"http:\/\/blog.stratus.org.uk\/?p=558","title":{"rendered":"Simple Shell Script &#8211; File if-then-else"},"content":{"rendered":"<h2>Problem<\/h2>\n<p>An annoyance with one of my Raspbery Pi computers is that the two USB weather stations it is connected to sometimes lose USB connectivty and appear on a different file handle. The USB port (either <code>\/dev\/ttyUSB0<\/code> or <code>\/dev\/ttyUSB1<\/code> then re-enuermates on a different file handle in the <code>\/dev <\/code>folder.<\/p>\n<p>Aside from fixing the issue in the firmware or kernel on the Pi (being investigated), a temporary fix is to restart the Pi when <code>\/dev\/ttyUSB2<\/code> appears.<\/p>\n<h2>Solution<\/h2>\n<p>The soultion used is to have a script that checks if a new USB file handle appears and then reboot if that happens. We also need to check every five minutes if this happens because the weather station sends reports every 10 minutes and we need to ensure that it continues to do that in time.<\/p>\n<p>So, that means a cron job and a shell script and here they are \u2026<\/p>\n<h2>Shell Script (check-usb.sh)<\/h2>\n<p>Here\u2019s the script in full:<\/p>\n<pre> #!\/bin\/sh\r\n LOGFILE=\/home\/pi\/cgi-bin\/log\/check-usb.log\r\n USB0=\/dev\/ttyUSB0\r\n USB1=\/dev\/ttyUSB1\r\n USB2=\/dev\/ttyUSB2\r\n USB3=\/dev\/ttyUSB3\r\n TIMENOW=$(date)\r\n\r\n if [ -e \"$USB0\" ] && [ -e \"$USB1\" ]\r\n then\r\n  echo \"$TIMENOW: $USB0 and $USB1 found\"\r\n  echo \"$TIMENOW: $USB0 and $USB1 found\" >> $LOGFILE\r\n  echo \"$TIMENOW: Exit normally\"\r\n  echo \"$TIMENOW: Exit normally\" >> $LOGFILE\r\n  exit 0\r\n fi\r\n# If get to here check for others\r\n if [ -e \"$USB2\" ] || [ -e \"$USB3 ]\r\n then\r\n  echo \"$TIMENOW: $USB2 or $USB3 found\"\r\n  echo \"$TIMENOW: $USB2 or $USB3 found\" >> $LOGFILE\r\n  echo \"$TIMENOW: USB re-numerated - reboot required\"\r\n  echo \"$TIMENOW: USB re-numerated - reboot required\" >> $LOGFILE\r\n  \/sbin\/reboot\r\n fi<\/pre>\n<p>A log file is also included as that\u2019s a useful diagnostic tool \u2013 don\u2019t forget to use logrotate to chop it from time to time. The key bit is to do a shell <code>if<\/code> using <code>-e<\/code> to see if the file handle to the USB device is still there. As there are two USB devices to worry about (two file handles), use a logical <code>or<\/code> to ensure they are both there. Otherwise, something is broken so then do a reboot having logged that fact to the log file for later review.<\/p>\n<p>Right, that takes care of the checking, now the cron job.<\/p>\n<h2>Cron Job<\/h2>\n<p>I set this to run as a root cron job (<code>crontab -e<\/code> to put this in your cron table for <code>root<\/code>). This ensures no problems with permissions and also means that root is rebooting and not anyone else.<\/p>\n<pre>#check every 5 mins if USB 0 or USB 1 get re-enumerated\r\n#if USB0 or 1 disappears reboot to resync\r\n*\/5 * * * * \/home\/pi\/cgi-bin\/check-usb.sh<\/pre>\n<p>That should get around the problem until a patch is available for the loss of USB enumeration.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem An annoyance with one of my Raspbery Pi computers is that the two USB weather stations it is connected to sometimes lose USB connectivty and appear on a different file handle. The USB port (either \/dev\/ttyUSB0 or \/dev\/ttyUSB1 then &hellip; <a href=\"http:\/\/blog.stratus.org.uk\/?p=558\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-558","post","type-post","status-publish","format-standard","hentry","category-misc"],"_links":{"self":[{"href":"http:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/posts\/558","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.stratus.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=558"}],"version-history":[{"count":8,"href":"http:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/posts\/558\/revisions"}],"predecessor-version":[{"id":566,"href":"http:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/posts\/558\/revisions\/566"}],"wp:attachment":[{"href":"http:\/\/blog.stratus.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.stratus.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=558"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.stratus.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}