{"id":531,"date":"2011-03-09T09:47:26","date_gmt":"2011-03-09T09:47:26","guid":{"rendered":"https:\/\/fir3netwp.gmsrrpobkbd.com\/2011\/03\/09\/conditional-execution-within-crontab\/"},"modified":"2021-07-24T18:33:39","modified_gmt":"2021-07-24T18:33:39","slug":"conditional-execution-within-crontab","status":"publish","type":"post","link":"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html","title":{"rendered":"Conditional Execution within Crontab"},"content":{"rendered":"

Conditional execution is a term used to describe the execution of processes or commands based on specfic conditions. This can also be thought of as conditional logic. 
Like with most scripting or programming languages conditional logic is built upon the use of return codes. A return code (exit status)  is returned upon process completion to determine success or failure. A return code of 0 is set if the process has been successfully executed, and a non-zero is set on failure. <\/p>\n

You can test this by running a command such as pgrep crond<\/span>on a system that you already know is running crond.<\/p>\n

[root@server ~]# pgrep crond
2518
[root@server ~]# echo $?
0
[root@server ~]# pgrep crond123
[root@server ~]# echo $?
1<\/pre>\n

The $?<\/span> allows you to echo the return code. In this case crond<\/span> was successful but crond123<\/span> was not as there is no process running with this name.
Using this logic we can create a single crontab entry to allow us to run a script only if it is not already running. <\/p>\n

* * * * *  \/usr\/sbin\/pgrep example.sh ; [ $? != 0 ] && \/path\/example.sh<\/pre>\n

The logic of this works by [ $? != 0 ]<\/span> evaluating the return code of \/usr\/sbin\/pgrep example.sh<\/span>. If this evaluation is successful (i.e a return code of 0 is not found from the pgrep command) then a return code of 0 is produced from this test statement. The test statement return code is then passed to &&<\/span>. &&<\/span> works by only executing any further commands if a return code of 0 is received. This then allows us to only run the \/path\/example.sh script based on a return code of 0 from [ $? != 0 ] <\/span>(test statement).<\/p>\n","protected":false},"excerpt":{"rendered":"

Conditional execution is a term used to describe the execution of processes or commands based on specfic conditions. This can also be thought of as conditional logic.  Like with most scripting or programming languages conditional logic is built upon the use of return codes. A return code (exit status)  is returned upon process completion to … Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52],"tags":[],"yoast_head":"\nConditional Execution within Crontab - Fir3net<\/title>\n<meta name=\"description\" content=\"Conditional execution is a term used to describe the execution of processes or commands based on specfic conditions. This can also be thought of as\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Conditional Execution within Crontab - Fir3net\" \/>\n<meta property=\"og:description\" content=\"Conditional execution is a term used to describe the execution of processes or commands based on specfic conditions. This can also be thought of as\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html\" \/>\n<meta property=\"og:site_name\" content=\"Fir3net\" \/>\n<meta property=\"article:published_time\" content=\"2011-03-09T09:47:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-24T18:33:39+00:00\" \/>\n<meta name=\"author\" content=\"Rick Donato\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rick Donato\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html\"},\"author\":{\"name\":\"Rick Donato\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037\"},\"headline\":\"Conditional Execution within Crontab\",\"datePublished\":\"2011-03-09T09:47:26+00:00\",\"dateModified\":\"2021-07-24T18:33:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html\"},\"wordCount\":251,\"publisher\":{\"@id\":\"https:\/\/www.fir3net.com\/#organization\"},\"articleSection\":[\"Linux\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html\",\"url\":\"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html\",\"name\":\"Conditional Execution within Crontab - Fir3net\",\"isPartOf\":{\"@id\":\"https:\/\/www.fir3net.com\/#website\"},\"datePublished\":\"2011-03-09T09:47:26+00:00\",\"dateModified\":\"2021-07-24T18:33:39+00:00\",\"description\":\"Conditional execution is a term used to describe the execution of processes or commands based on specfic conditions. This can also be thought of as\",\"breadcrumb\":{\"@id\":\"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.fir3net.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Operating Systems\",\"item\":\"https:\/\/www.fir3net.com\/os\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Linux\",\"item\":\"https:\/\/www.fir3net.com\/os\/linux\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Conditional Execution within Crontab\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.fir3net.com\/#website\",\"url\":\"https:\/\/www.fir3net.com\/\",\"name\":\"Fir3net\",\"description\":\"Keeping you in the know\",\"publisher\":{\"@id\":\"https:\/\/www.fir3net.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.fir3net.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.fir3net.com\/#organization\",\"name\":\"Fir3net\",\"url\":\"https:\/\/www.fir3net.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png\",\"contentUrl\":\"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png\",\"width\":390,\"height\":88,\"caption\":\"Fir3net\"},\"image\":{\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037\",\"name\":\"Rick Donato\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g\",\"caption\":\"Rick Donato\"},\"description\":\"Rick Donato is a Network Automation Architect\/Evangelist and the founder of Packet Coders.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Conditional Execution within Crontab - Fir3net","description":"Conditional execution is a term used to describe the execution of processes or commands based on specfic conditions. This can also be thought of as","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html","og_locale":"en_US","og_type":"article","og_title":"Conditional Execution within Crontab - Fir3net","og_description":"Conditional execution is a term used to describe the execution of processes or commands based on specfic conditions. This can also be thought of as","og_url":"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html","og_site_name":"Fir3net","article_published_time":"2011-03-09T09:47:26+00:00","article_modified_time":"2021-07-24T18:33:39+00:00","author":"Rick Donato","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rick Donato","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html#article","isPartOf":{"@id":"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html"},"author":{"name":"Rick Donato","@id":"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037"},"headline":"Conditional Execution within Crontab","datePublished":"2011-03-09T09:47:26+00:00","dateModified":"2021-07-24T18:33:39+00:00","mainEntityOfPage":{"@id":"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html"},"wordCount":251,"publisher":{"@id":"https:\/\/www.fir3net.com\/#organization"},"articleSection":["Linux"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html","url":"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html","name":"Conditional Execution within Crontab - Fir3net","isPartOf":{"@id":"https:\/\/www.fir3net.com\/#website"},"datePublished":"2011-03-09T09:47:26+00:00","dateModified":"2021-07-24T18:33:39+00:00","description":"Conditional execution is a term used to describe the execution of processes or commands based on specfic conditions. This can also be thought of as","breadcrumb":{"@id":"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.fir3net.com\/UNIX\/Linux\/conditional-execution-within-crontab.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.fir3net.com\/"},{"@type":"ListItem","position":2,"name":"Operating Systems","item":"https:\/\/www.fir3net.com\/os"},{"@type":"ListItem","position":3,"name":"Linux","item":"https:\/\/www.fir3net.com\/os\/linux"},{"@type":"ListItem","position":4,"name":"Conditional Execution within Crontab"}]},{"@type":"WebSite","@id":"https:\/\/www.fir3net.com\/#website","url":"https:\/\/www.fir3net.com\/","name":"Fir3net","description":"Keeping you in the know","publisher":{"@id":"https:\/\/www.fir3net.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.fir3net.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.fir3net.com\/#organization","name":"Fir3net","url":"https:\/\/www.fir3net.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png","contentUrl":"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png","width":390,"height":88,"caption":"Fir3net"},"image":{"@id":"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037","name":"Rick Donato","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.fir3net.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g","caption":"Rick Donato"},"description":"Rick Donato is a Network Automation Architect\/Evangelist and the founder of Packet Coders."}]}},"_links":{"self":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts\/531"}],"collection":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/comments?post=531"}],"version-history":[{"count":0,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts\/531\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/media?parent=531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/categories?post=531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/tags?post=531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}