{"id":8509,"date":"2024-11-12T01:01:00","date_gmt":"2024-11-12T06:01:00","guid":{"rendered":"https:\/\/www.both.org\/?p=8509"},"modified":"2024-11-09T16:10:47","modified_gmt":"2024-11-09T21:10:47","slug":"how-i-delay-code-execution-in-bash-until-a-remote-host-is-responding","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=8509","title":{"rendered":"How I delay code execution in Bash until a remote host is responding"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"8509\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>I have recently been working on some code that needs to run on a remote host using SSH. I describe this in my article, <a href=\"https:\/\/www.both.org\/?p=4650\" data-type=\"link\" data-id=\"https:\/\/www.both.org\/?p=4650\" target=\"_blank\" rel=\"noreferrer noopener\">Using tar and ssh for backups<\/a>. <\/p>\n\n\n\n<p>But now I need to reboot the remote computer and wait until it has completely rebooted and the network is up and running. Only after that occurs can I continue sending commands to that remote system.<\/p>\n\n\n\n<p>This little bit of code accomplishes that for me.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Host=\"f41vm\"\nX=1\n\nuntil &#91; $X -eq 0 ] ; do\n      sleep 10s \n      ping -c1 $Host \n      X=$?\ndone\n\n# The rest of the code is executed starting here, \n# when the ping is successful and \n# returns 0 instead of 1.<\/code><\/pre>\n\n\n\n<p>The <strong>X=$?<\/strong> statement takes the return code of the ping command ( $?_ ) and sets the $X variable to that value. Then $X can be used in the <strong>until<\/strong> statement comparison expression.<\/p>\n\n\n\n<p>I add the sleep command so I&#8217;m not sending a Bazzillion pings to the remote host, and 10 seconds seems to work well enough for most of my purposes. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have recently been working on some code that needs to run on a remote host using SSH.<\/p>\n","protected":false},"author":2,"featured_media":2725,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[149,98,5],"tags":[437],"class_list":["post-8509","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash","category-code","category-linux","tag-bash-programming"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8509"}],"version-history":[{"count":3,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8509\/revisions"}],"predecessor-version":[{"id":8512,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8509\/revisions\/8512"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/2725"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}