{"id":12745,"date":"2022-10-15T07:22:36","date_gmt":"2022-10-15T11:22:36","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=12745"},"modified":"2022-10-15T07:22:40","modified_gmt":"2022-10-15T11:22:40","slug":"how-to-execute-multiple-commands-in-a-single-cron-job","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/","title":{"rendered":"How to Execute Multiple Commands in a Single Cron Job"},"content":{"rendered":"\n

Our task today is to show you how to run multiple commands in one cron job. In this guide we showed you how to run a Python script using cron job<\/a>. The question is, how would you run several scripts without creating seperate cron jobs?<\/p>\n\n\n\n

Crontab Utility<\/h2>\n\n\n\n

Crontab is a utility on Unix, Linux and Mac systems used for running scheduled tasks at regular intervals on those systems. With crontab you can schedule multiple cron jobs to run at once. This can be done on any shell command or script that you would normally execute on the terminal.<\/p>\n\n\n\n

Scheduled jobs in crontab are created on a new line, however, this does not always have to be the can. If we have several scheduled tasks that are supposed to run at the same time and interval, we can also define all those commands or scripts in a single cron job.<\/p>\n\n\n\n

A Little Background<\/h2>\n\n\n\n

Before we see how to execute multiple commands in a single cron job command, we need to understand how those commands or scripts are run by the shell terminal.<\/p>\n\n\n\n

In any Linux-Unix you can run one or more commands in a single line. When running two or more commands you will generally separate them by using either semicolons (;), logical AND (&&), or logical OR (||) operators. Each of these separators are used based on the specific requirements as explained below:<\/p>\n\n\n\n

1. Semicolon (;) Separator: is used to separate multiple commands. This is guaranteed to execute all the commands without checking the exit status of previous commands.<\/p>\n\n\n\n

command_1;  command_2; ... command_n<\/code><\/pre>\n\n\n\n

2. Logical AND (&&) operator: is used to separate commands where we want to execute the next command only if the previous command was successfully executed with exit status 0.<\/p>\n\n\n\n

command_1 &&  command_2 && ... command_n<\/code><\/pre>\n\n\n\n

3. Logical OR (||) operator: is used to separate commands where we want to execute the next command only if the previous command failed with a non-0 exit status.<\/p>\n\n\n\n

command_1 ||  command_2 || ... command_n<\/code><\/pre>\n\n\n\n

Execute Multiple Commands in a Single Cron Job<\/h2>\n\n\n\n

Executed as the user from which you want to run a cron job, open the crontab editor by running the following command.<\/p>\n\n\n\n

$ crontab -e<\/code><\/pre>\n\n\n\n

For more details about crontab, we have included a guide to understand how to set up the cron jobs. In our examples below we will use a cron job scheduled to run every 15 minutes.<\/p>\n\n\n\n

The Semicolon (;) Option<\/h2>\n\n\n\n

In this situation it is ideal for running commands that are not related or do not depend on one another. In this example we will run a random script and delete a log file every 15 minutes.<\/p>\n\n\n\n

*\/15 * * * * python \/path\/to\/app\/cronscript.py; rm \/path\/to\/logs\/logfile<\/code><\/pre>\n\n\n\n

The Logical AND (&&) Option<\/h2>\n\n\n\n

In this situation we want to run the next command only if the previous is returns successfully with exit status 0. In this example, you want to extract a file only after successfully downloading the latest copy and delete the downloaded file only after successful extraction.<\/p>\n\n\n\n

*\/15 * * * * wget https:\/\/example.com\/downloads\/latest.tar.gz && tar -xzvf latest.tar.gz && rm latest.tar.gz<\/code><\/pre>\n\n\n\n

The Logical OR (||) Option<\/h2>\n\n\n\n

With the logical OR (||) operator, you want to run the next command only if the previous is failed with exit status non-0. In this example, we want to send an email if the log file not found.<\/p>\n\n\n\n

*\/15 * * * * [ -f \/important\/log\/logfile-`date +%F`.log ] || echo "Warning important message" | mailx -s 'Warning subject' server-admin@mydomain.com<\/code><\/pre>\n\n\n\n

Conclusion<\/h2>\n\n\n\n

In this article, you have learned how to execute multiple commands in a single cron job command. We also looked at the various options to separate commands and shown example scenarios of how and why you would choose to use either one of them.<\/p>\n","protected":false},"excerpt":{"rendered":"

Our task today is to show you how to run multiple commands in one cron job. In this guide we showed you how to run a Python script using cron…<\/p>\n","protected":false},"author":1,"featured_media":12746,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,9,16,18],"tags":[121,183,193,354,359,433,449,573,591,598,604],"yoast_head":"\nHow to Execute Multiple Commands in a Single Cron Job<\/title>\n<meta name=\"description\" content=\"Using examples this is how you can execute multiple commands in a single cron job command in Linux, Unix, and Mac systems\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Execute Multiple Commands in a Single Cron Job\" \/>\n<meta property=\"og:description\" content=\"Using examples this is how you can execute multiple commands in a single cron job command in Linux, Unix, and Mac systems\" \/>\n<meta property=\"og:url\" content=\"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/\" \/>\n<meta property=\"og:site_name\" content=\"Brightwhiz.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/brightwhiz\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-15T11:22:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-10-15T11:22:40+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2022\/10\/execute-multiple-commands-in-a-single-cron-job.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"680\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Michael Bright\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:site\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michael Bright\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/\"},\"author\":{\"name\":\"Michael Bright\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\"},\"headline\":\"How to Execute Multiple Commands in a Single Cron Job\",\"datePublished\":\"2022-10-15T11:22:36+00:00\",\"dateModified\":\"2022-10-15T11:22:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/\"},\"wordCount\":562,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2022\/10\/execute-multiple-commands-in-a-single-cron-job.jpg\",\"keywords\":[\"CentOS\",\"Debian\",\"DevOps\",\"Linux\",\"MacOS\",\"Optimization\",\"Performance\",\"Systems Administration\",\"Tools\",\"Ubuntu\",\"Unix\"],\"articleSection\":[\"Articles\",\"Guides\",\"How To\",\"Technology\",\"Tools\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/\",\"url\":\"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/\",\"name\":\"How to Execute Multiple Commands in a Single Cron Job\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/#primaryimage\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2022\/10\/execute-multiple-commands-in-a-single-cron-job.jpg\",\"datePublished\":\"2022-10-15T11:22:36+00:00\",\"dateModified\":\"2022-10-15T11:22:40+00:00\",\"description\":\"Using examples this is how you can execute multiple commands in a single cron job command in Linux, Unix, and Mac systems\",\"breadcrumb\":{\"@id\":\"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/#primaryimage\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2022\/10\/execute-multiple-commands-in-a-single-cron-job.jpg\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2022\/10\/execute-multiple-commands-in-a-single-cron-job.jpg\",\"width\":1280,\"height\":680,\"caption\":\"Execute Multiple Commands in a Single Cron Job\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/local.brightwhiz\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Execute Multiple Commands in a Single Cron Job\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/local.brightwhiz\/#website\",\"url\":\"http:\/\/local.brightwhiz\/\",\"name\":\"Brightwhiz.com\",\"description\":\"Best Tech guides, Tutorials, and News\",\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/local.brightwhiz\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/local.brightwhiz\/#organization\",\"name\":\"Brightwhiz\",\"url\":\"http:\/\/local.brightwhiz\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"width\":706,\"height\":135,\"caption\":\"Brightwhiz\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/brightwhiz\/\",\"https:\/\/twitter.com\/brightwhizmag\",\"https:\/\/instagram.com\/bright_whiz\/\",\"https:\/\/www.pinterest.com\/sobbayi\/\",\"https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\",\"name\":\"Michael Bright\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"caption\":\"Michael Bright\"},\"sameAs\":[\"https:\/\/sobbayi.com\"],\"url\":\"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Execute Multiple Commands in a Single Cron Job","description":"Using examples this is how you can execute multiple commands in a single cron job command in Linux, Unix, and Mac systems","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":"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/","og_locale":"en_US","og_type":"article","og_title":"How to Execute Multiple Commands in a Single Cron Job","og_description":"Using examples this is how you can execute multiple commands in a single cron job command in Linux, Unix, and Mac systems","og_url":"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/","og_site_name":"Brightwhiz.com","article_publisher":"https:\/\/www.facebook.com\/brightwhiz\/","article_published_time":"2022-10-15T11:22:36+00:00","article_modified_time":"2022-10-15T11:22:40+00:00","og_image":[{"width":1280,"height":680,"url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2022\/10\/execute-multiple-commands-in-a-single-cron-job.jpg","type":"image\/jpeg"}],"author":"Michael Bright","twitter_card":"summary_large_image","twitter_creator":"@brightwhizmag","twitter_site":"@brightwhizmag","twitter_misc":{"Written by":"Michael Bright","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/#article","isPartOf":{"@id":"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/"},"author":{"name":"Michael Bright","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32"},"headline":"How to Execute Multiple Commands in a Single Cron Job","datePublished":"2022-10-15T11:22:36+00:00","dateModified":"2022-10-15T11:22:40+00:00","mainEntityOfPage":{"@id":"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/"},"wordCount":562,"commentCount":0,"publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"image":{"@id":"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2022\/10\/execute-multiple-commands-in-a-single-cron-job.jpg","keywords":["CentOS","Debian","DevOps","Linux","MacOS","Optimization","Performance","Systems Administration","Tools","Ubuntu","Unix"],"articleSection":["Articles","Guides","How To","Technology","Tools"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/","url":"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/","name":"How to Execute Multiple Commands in a Single Cron Job","isPartOf":{"@id":"http:\/\/local.brightwhiz\/#website"},"primaryImageOfPage":{"@id":"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/#primaryimage"},"image":{"@id":"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2022\/10\/execute-multiple-commands-in-a-single-cron-job.jpg","datePublished":"2022-10-15T11:22:36+00:00","dateModified":"2022-10-15T11:22:40+00:00","description":"Using examples this is how you can execute multiple commands in a single cron job command in Linux, Unix, and Mac systems","breadcrumb":{"@id":"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/#primaryimage","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2022\/10\/execute-multiple-commands-in-a-single-cron-job.jpg","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2022\/10\/execute-multiple-commands-in-a-single-cron-job.jpg","width":1280,"height":680,"caption":"Execute Multiple Commands in a Single Cron Job"},{"@type":"BreadcrumbList","@id":"http:\/\/local.brightwhiz\/how-to-execute-multiple-commands-in-a-single-cron-job\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/local.brightwhiz\/"},{"@type":"ListItem","position":2,"name":"How to Execute Multiple Commands in a Single Cron Job"}]},{"@type":"WebSite","@id":"http:\/\/local.brightwhiz\/#website","url":"http:\/\/local.brightwhiz\/","name":"Brightwhiz.com","description":"Best Tech guides, Tutorials, and News","publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/local.brightwhiz\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/local.brightwhiz\/#organization","name":"Brightwhiz","url":"http:\/\/local.brightwhiz\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","width":706,"height":135,"caption":"Brightwhiz"},"image":{"@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/brightwhiz\/","https:\/\/twitter.com\/brightwhizmag","https:\/\/instagram.com\/bright_whiz\/","https:\/\/www.pinterest.com\/sobbayi\/","https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ"]},{"@type":"Person","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32","name":"Michael Bright","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","caption":"Michael Bright"},"sameAs":["https:\/\/sobbayi.com"],"url":"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/"}]}},"_links":{"self":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/12745"}],"collection":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/comments?post=12745"}],"version-history":[{"count":0,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/12745\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media\/12746"}],"wp:attachment":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media?parent=12745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/categories?post=12745"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/tags?post=12745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}