{"id":42765,"date":"2019-07-02T10:17:36","date_gmt":"2019-07-02T08:17:36","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=42765"},"modified":"2019-07-02T10:17:45","modified_gmt":"2019-07-02T08:17:45","slug":"bash-trick-repeat-last-command-until-success","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/","title":{"rendered":"Bash trick: Repeat last command until success"},"content":{"rendered":"<!-- google_ad_section_start -->\n\n<p class=\"wp-block-paragraph\">More and more often I come across a scenario where I need to repeat the shell command until it succeeds.  Here are a couple of examples:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Reboot a server.  Try to remotely login to it via ssh.  This fails until the server actually boots up.  Keep trying until connected.<\/li><li>Start an application that writes to the log file.  Run &#8220;tail -f some.log&#8221; to watch the log messages.  This fails if the log file does not exist yet.  Keep trying until the application creates the log file and writes something into it.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Sure, I can always press the up arrow key and Enter, to repeat the last command from the history.  But it is a tiny bit annoying.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Today I came across <a href=\"https:\/\/medium.com\/@marko.luksa\/bash-trick-repeat-last-command-until-success-750a61c43c8a\">this little trick<\/a>, that solves the problem.  Add the following function to your <em>.bashrc<\/em>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nrpt() {\n  CMD=$(fc -ln | tail -n 2 | head -n 1)\n  echo &quot;repeating until success: $CMD&quot;\n  until $CMD\n  do\n    sleep 1\n  done\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Now you can run &#8220;rpt&#8221; to repeat the latest command until it succeeds.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Handy!<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>More and more often I come across a scenario where I need to repeat the shell command until it succeeds. Here are a couple of examples: Reboot a server. Try to remotely login to it via ssh. This fails until the server actually boots up. Keep trying until connected. Start an application that writes to &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Bash trick: Repeat last command until success<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Bash trick: Repeat last command until success #linux #sysadmin #devops #CLI #bash #shell #productivity","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false,"_links_to":"","_links_to_target":""},"categories":[1,6,133,62],"tags":[3098,1960,1079],"keyring_services":[],"class_list":["post-42765","post","type-post","status-publish","format-standard","hentry","category-general","category-linux","category-sysadmin","category-technology","tag-bash","tag-command-line","tag-productivity"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.10 - aioseo.com -->\n\t<meta name=\"description\" content=\"More and more often I come across a scenario where I need to repeat the shell command until it succeeds. Here are a couple of examples: Reboot a server. Try to remotely login to it via ssh. This fails until the server actually boots up. Keep trying until connected.Start an application that writes to the\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Leonid Mamchenkov\"\/>\n\t<meta name=\"google-site-verification\" content=\"VHvdD0_usx1_4DzKy_QCVcICVgX2EgA2ybELT-wl7kQ\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.10\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Leonid Mamchenkov - Life, universe, and everything else\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Bash trick: Repeat last command until success - Leonid Mamchenkov\" \/>\n\t\t<meta property=\"og:description\" content=\"More and more often I come across a scenario where I need to repeat the shell command until it succeeds. Here are a couple of examples: Reboot a server. Try to remotely login to it via ssh. This fails until the server actually boots up. Keep trying until connected.Start an application that writes to the\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2019-07-02T08:17:36+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2019-07-02T08:17:45+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/MamchenkovBlog\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@mamchenkov\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Bash trick: Repeat last command until success - Leonid Mamchenkov\" \/>\n\t\t<meta name=\"twitter:description\" content=\"More and more often I come across a scenario where I need to repeat the shell command until it succeeds. Here are a couple of examples: Reboot a server. Try to remotely login to it via ssh. This fails until the server actually boots up. Keep trying until connected.Start an application that writes to the\" \/>\n\t\t<meta name=\"twitter:creator\" content=\"@mamchenkov\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2019\\\/07\\\/02\\\/bash-trick-repeat-last-command-until-success\\\/#blogposting\",\"name\":\"Bash trick: Repeat last command until success - Leonid Mamchenkov\",\"headline\":\"Bash trick: Repeat last command until success\",\"author\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2019\\\/07\\\/02\\\/bash-trick-repeat-last-command-until-success\\\/#articleImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"Leonid Mamchenkov\"},\"datePublished\":\"2019-07-02T10:17:36+02:00\",\"dateModified\":\"2019-07-02T10:17:45+02:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2019\\\/07\\\/02\\\/bash-trick-repeat-last-command-until-success\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2019\\\/07\\\/02\\\/bash-trick-repeat-last-command-until-success\\\/#webpage\"},\"articleSection\":\"All, Linux, Sysadmin, Technology, bash, command line, productivity\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2019\\\/07\\\/02\\\/bash-trick-repeat-last-command-until-success\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/#listItem\",\"position\":2,\"name\":\"Technology\",\"item\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/linux\\\/#listItem\",\"name\":\"Linux\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/linux\\\/#listItem\",\"position\":3,\"name\":\"Linux\",\"item\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/linux\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2019\\\/07\\\/02\\\/bash-trick-repeat-last-command-until-success\\\/#listItem\",\"name\":\"Bash trick: Repeat last command until success\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2019\\\/07\\\/02\\\/bash-trick-repeat-last-command-until-success\\\/#listItem\",\"position\":4,\"name\":\"Bash trick: Repeat last command until success\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/linux\\\/#listItem\",\"name\":\"Linux\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#person\",\"name\":\"Leonid Mamchenkov\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2019\\\/07\\\/02\\\/bash-trick-repeat-last-command-until-success\\\/#personImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"Leonid Mamchenkov\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/#author\",\"url\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/\",\"name\":\"Leonid Mamchenkov\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2019\\\/07\\\/02\\\/bash-trick-repeat-last-command-until-success\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"Leonid Mamchenkov\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2019\\\/07\\\/02\\\/bash-trick-repeat-last-command-until-success\\\/#webpage\",\"url\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2019\\\/07\\\/02\\\/bash-trick-repeat-last-command-until-success\\\/\",\"name\":\"Bash trick: Repeat last command until success - Leonid Mamchenkov\",\"description\":\"More and more often I come across a scenario where I need to repeat the shell command until it succeeds. Here are a couple of examples: Reboot a server. Try to remotely login to it via ssh. This fails until the server actually boots up. Keep trying until connected.Start an application that writes to the\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2019\\\/07\\\/02\\\/bash-trick-repeat-last-command-until-success\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/#author\"},\"datePublished\":\"2019-07-02T10:17:36+02:00\",\"dateModified\":\"2019-07-02T10:17:45+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#website\",\"url\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/\",\"name\":\"Blog of Leonid Mamchenkov\",\"description\":\"Life, universe, and everything else\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Bash trick: Repeat last command until success - Leonid Mamchenkov","description":"More and more often I come across a scenario where I need to repeat the shell command until it succeeds. Here are a couple of examples: Reboot a server. Try to remotely login to it via ssh. This fails until the server actually boots up. Keep trying until connected.Start an application that writes to the","canonical_url":"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"google-site-verification":"VHvdD0_usx1_4DzKy_QCVcICVgX2EgA2ybELT-wl7kQ","miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/#blogposting","name":"Bash trick: Repeat last command until success - Leonid Mamchenkov","headline":"Bash trick: Repeat last command until success","author":{"@id":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/#author"},"publisher":{"@id":"https:\/\/mamchenkov.net\/wordpress\/#person"},"image":{"@type":"ImageObject","@id":"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/#articleImage","url":"https:\/\/secure.gravatar.com\/avatar\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g","width":96,"height":96,"caption":"Leonid Mamchenkov"},"datePublished":"2019-07-02T10:17:36+02:00","dateModified":"2019-07-02T10:17:45+02:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/#webpage"},"isPartOf":{"@id":"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/#webpage"},"articleSection":"All, Linux, Sysadmin, Technology, bash, command line, productivity"},{"@type":"BreadcrumbList","@id":"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress#listItem","position":1,"name":"Home","item":"https:\/\/mamchenkov.net\/wordpress","nextItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/#listItem","position":2,"name":"Technology","item":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/linux\/#listItem","name":"Linux"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/linux\/#listItem","position":3,"name":"Linux","item":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/linux\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/#listItem","name":"Bash trick: Repeat last command until success"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/#listItem","position":4,"name":"Bash trick: Repeat last command until success","previousItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/linux\/#listItem","name":"Linux"}}]},{"@type":"Person","@id":"https:\/\/mamchenkov.net\/wordpress\/#person","name":"Leonid Mamchenkov","image":{"@type":"ImageObject","@id":"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/#personImage","url":"https:\/\/secure.gravatar.com\/avatar\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g","width":96,"height":96,"caption":"Leonid Mamchenkov"}},{"@type":"Person","@id":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/#author","url":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/","name":"Leonid Mamchenkov","image":{"@type":"ImageObject","@id":"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g","width":96,"height":96,"caption":"Leonid Mamchenkov"}},{"@type":"WebPage","@id":"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/#webpage","url":"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/","name":"Bash trick: Repeat last command until success - Leonid Mamchenkov","description":"More and more often I come across a scenario where I need to repeat the shell command until it succeeds. Here are a couple of examples: Reboot a server. Try to remotely login to it via ssh. This fails until the server actually boots up. Keep trying until connected.Start an application that writes to the","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/mamchenkov.net\/wordpress\/#website"},"breadcrumb":{"@id":"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/#breadcrumblist"},"author":{"@id":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/#author"},"creator":{"@id":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/#author"},"datePublished":"2019-07-02T10:17:36+02:00","dateModified":"2019-07-02T10:17:45+02:00"},{"@type":"WebSite","@id":"https:\/\/mamchenkov.net\/wordpress\/#website","url":"https:\/\/mamchenkov.net\/wordpress\/","name":"Blog of Leonid Mamchenkov","description":"Life, universe, and everything else","inLanguage":"en-US","publisher":{"@id":"https:\/\/mamchenkov.net\/wordpress\/#person"}}]},"og:locale":"en_US","og:site_name":"Leonid Mamchenkov - Life, universe, and everything else","og:type":"article","og:title":"Bash trick: Repeat last command until success - Leonid Mamchenkov","og:description":"More and more often I come across a scenario where I need to repeat the shell command until it succeeds. Here are a couple of examples: Reboot a server. Try to remotely login to it via ssh. This fails until the server actually boots up. Keep trying until connected.Start an application that writes to the","og:url":"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/","og:image":"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg","og:image:secure_url":"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg","og:image:width":1024,"og:image:height":1024,"article:published_time":"2019-07-02T08:17:36+00:00","article:modified_time":"2019-07-02T08:17:45+00:00","article:publisher":"https:\/\/www.facebook.com\/MamchenkovBlog","twitter:card":"summary_large_image","twitter:site":"@mamchenkov","twitter:title":"Bash trick: Repeat last command until success - Leonid Mamchenkov","twitter:description":"More and more often I come across a scenario where I need to repeat the shell command until it succeeds. Here are a couple of examples: Reboot a server. Try to remotely login to it via ssh. This fails until the server actually boots up. Keep trying until connected.Start an application that writes to the","twitter:creator":"@mamchenkov","twitter:image":"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg"},"aioseo_meta_data":{"post_id":"42765","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2023-07-20 00:08:32","updated":"2026-01-15 14:14:45","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/mamchenkov.net\/wordpress\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/\" title=\"Technology\">Technology<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/linux\/\" title=\"Linux\">Linux<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tBash trick: Repeat last command until success\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/mamchenkov.net\/wordpress"},{"label":"Technology","link":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/"},{"label":"Linux","link":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/linux\/"},{"label":"Bash trick: Repeat last command until success","link":"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/"}],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":7367,"url":"https:\/\/mamchenkov.net\/wordpress\/2004\/03\/23\/repeat-of-the-last-command-in-bash\/","url_meta":{"origin":42765,"position":0},"title":"Repeat of the last command in bash","author":"Leonid Mamchenkov","date":"March 23, 2004","format":false,"excerpt":"Ok, we all know that to repeat the last command in bash (as well as many other shells) you just need to press the Up key. Sometimes though it is handy to test a command before using it as a subset of a bigger command. Example: if I want to\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26685,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/09\/12\/git-history-of-a-source-code-line\/","url_meta":{"origin":42765,"position":1},"title":"git: history of a source code line","author":"Leonid Mamchenkov","date":"September 12, 2016","format":false,"excerpt":"git is one of those tools that no matter how much you know about it, there is an infinite supply of new things to learn. \u00a0Here's a handy bit I've discovered recently, thanks to this StackOverflow comment: Since Git 1.8.4, git log has -L to view the evolution of a\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":12214,"url":"https:\/\/mamchenkov.net\/wordpress\/2010\/03\/02\/monitoring-php-errors-warnings-and-notices\/","url_meta":{"origin":42765,"position":2},"title":"Monitoring PHP errors, warnings, and notices","author":"Leonid Mamchenkov","date":"March 2, 2010","format":false,"excerpt":"There are a number of ways to monitor PHP errors, warnings, and notices. \u00a0 You can have your application code trigger some error handling, you can use PHP built-in methods, you can have some scripts running in the background\u00a0analyzing logs, etc. \u00a0While you already probably do some of it, here\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9014,"url":"https:\/\/mamchenkov.net\/wordpress\/2005\/05\/27\/handling-arguments-with-spaces-in-bash\/","url_meta":{"origin":42765,"position":3},"title":"Handling arguments with spaces in bash","author":"Leonid Mamchenkov","date":"May 27, 2005","format":false,"excerpt":"Way to often I get it wrong, so I decided to right this down... When processing the list of arguments in your bash script, remember that often arguments such as file names contain spaces. The wrong way to go about this is: #!\/bin\/bash for FILE in $* do echo \"$FILE\"\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":25030,"url":"https:\/\/mamchenkov.net\/wordpress\/2015\/12\/10\/weird-php-error-output-bug\/","url_meta":{"origin":42765,"position":4},"title":"Weird PHP error output bug","author":"Leonid Mamchenkov","date":"December 10, 2015","format":false,"excerpt":"We came across this PHP bug at work today. \u00a0But before you go and read it, let me show you a use case. \u00a0See, if you can spot the problem. We had a cron job script which looked something like this (shortened for clarity): Crystal clear, no? Output a time\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":16781,"url":"https:\/\/mamchenkov.net\/wordpress\/2012\/10\/02\/bash-directory-bookmarks\/","url_meta":{"origin":42765,"position":5},"title":"Bash directory bookmarks","author":"Leonid Mamchenkov","date":"October 2, 2012","format":false,"excerpt":"While reading through the comments to this Habrahabr article (in Russian), I came across an excellent tip for the directory bookmarks in bash shell. \u00a0Here's how to set it up. Firstly, add the following lines to your .bashrc or .bash_profile file: Secondly, if you are already using ~\/.bookmarks file for\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/42765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=42765"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/42765\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=42765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=42765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=42765"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=42765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}