{"id":33802,"date":"2021-05-20T12:50:32","date_gmt":"2021-05-20T12:50:32","guid":{"rendered":"https:\/\/ubuntuhandbook.org\/?p=33802"},"modified":"2021-05-20T12:50:32","modified_gmt":"2021-05-20T12:50:32","slug":"create-schedule-tasks-ubuntu-daily-weekly-monthly-job","status":"publish","type":"post","link":"https:\/\/ubuntuhandbook.org\/index.php\/2021\/05\/create-schedule-tasks-ubuntu-daily-weekly-monthly-job\/","title":{"rendered":"How to Create Schedule Tasks in Ubuntu for Daily \/ Weekly \/ Monthly Job"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-4464\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2015\/01\/go-for-it-todolist-icon.png\" alt=\"Go For It To-Do List App\" width=\"245\" height=\"245\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2015\/01\/go-for-it-todolist-icon.png 245w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2015\/01\/go-for-it-todolist-icon-150x150.png 150w\" sizes=\"auto, (max-width: 245px) 100vw, 245px\" \/><\/p>\n<p>Want to run a command or a script daily, weekly, monthly, or on other given schedule? It&#8217;s easy to do this in Ubuntu via cron job.<\/p>\n<p>Cron is a time-based job scheduler to run command or script periodically at fixed times, dates, or intervals. It&#8217;s typically used for system maintenance or administration, though it can be useful for general purpose, e.g., downloading file from internet at regular intervals.<\/p>\n<h3>1. Edit crontab files:<\/h3>\n<p>Users can set up a cron job easily by configuring crontab file by crontab command. It&#8217;s pre-installed in Ubuntu based systems. And each user has its own crontab config file.<\/p>\n<p><b>b.) Schedule task for current user:<\/b><\/p>\n<p>To run command or script by current user, simply open <i>terminal<\/i> from system app launcher and run command:<\/p>\n<pre style=\"border: none;\">crontab -e<\/pre>\n<p>For the first time, it will prompt to select an editor to edit the config file. Choose one you prefer or press Enter to use the default nano text editor.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-firstrun.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-33804\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-firstrun-600x206.jpg\" alt=\"\" width=\"600\" height=\"206\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-firstrun-600x206.jpg 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-firstrun-300x103.jpg 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-firstrun-768x263.jpg 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-firstrun.jpg 785w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p><b>b.) If need root or sudo privilege:<\/b><\/p>\n<p>For command or script need sudo or root user privilege, you may run following command instead:<\/p>\n<pre style=\"border: none;\">sudo crontab -e<\/pre>\n<p>It will create (if not exit) or open the configuration file for root user.<\/p>\n<p><b>c.) Specify a user to run the schedule task:<\/b><\/p>\n<p>You can add <code>-u &lt;user_name&gt;<\/code> flag to specify the user, <i>ji<\/i> for instance.<\/p>\n<pre style=\"border: none;\">sudo crontab -u ji -e<\/pre>\n<p>User can be root, so it will do the same to <code>sudo crontab -e<\/code>:<\/p>\n<pre style=\"border: none;\">sudo crontab -u root -e<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-root.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-33805\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-root-600x208.jpg\" alt=\"\" width=\"600\" height=\"208\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-root-600x208.jpg 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-root-300x104.jpg 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-root-768x267.jpg 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-root.jpg 778w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<h3>2. Set time interval, command or script to run periodically:<\/h3>\n<p>After running a command in step 1, it opens the configuration file in the terminal window (or command console).<\/p>\n<p>Now scroll down and add a new line:<\/p>\n<pre style=\"border:none;\">* * * * * &lt;command or script&gt;<\/pre>\n<p>The first 5 asterisks &#8220;*&#8221; specify the time and date, change them accordingly.<\/p>\n<h4>Examples:<\/h4>\n<p><b>a.)<\/b> For example, to run a python3 script under my Documents folder at midnight (00:00) every Sunday, use:<\/p>\n<pre style=\"border:none;\">0 0 * * 0 python3 \/home\/ji\/Documents\/script.py<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-weekly.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-weekly-600x376.jpg\" alt=\"\" width=\"600\" height=\"376\" class=\"aligncenter size-large wp-image-33806\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-weekly-600x376.jpg 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-weekly-300x188.jpg 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-weekly-768x481.jpg 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-weekly.jpg 1047w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>Here:<\/p>\n<ul>\n<li>the first <b>0<\/b> specifies the minute, use <b>*<\/b> for every minute.<\/li>\n<li>the second <b>0<\/b> specifies the hour, use <b>*<\/b> for every hour.<\/li>\n<li>the third flag <b>*<\/b> specifies the day of month, every day if week day not specified.<\/li>\n<li>the forth flag <b>*<\/b> says every month.<\/li>\n<li>the fifth flag (third <b>0<\/b>) specifies the week day. From 0 to 6 mean Sunday to Saturday.<\/li>\n<\/ul>\n<p><b>b.)<\/b> Run <code>echo \"hello world!\"<\/code> command everyday at 16:30, add this line:<\/p>\n<pre style=\"border:none;\">30 16 * * * echo \"hello world!\"<\/pre>\n<p><b>c.)<\/b> You can use <code>*\/n<\/code> to run for every n-th interval of time. And use multiple specific time intervals with commas.<\/p>\n<p>For instance, run the command every Friday at first, second, an third hour every 5th minute (01:00, 01:05, 01:10, &#8230;, 02:00, 02:05, 02:10, &#8230;, 03:55).<\/p>\n<pre style=\"border:none;\">*\/5 1,2,3 * * 5 echo \"hello world!\"<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-friday.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-friday-600x367.jpg\" alt=\"\" width=\"600\" height=\"367\" class=\"aligncenter size-large wp-image-33807\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-friday-600x367.jpg 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-friday-300x184.jpg 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-friday-768x470.jpg 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/05\/crontab-friday.jpg 925w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>Finally, save the configuration file. If edited via nano, press <b>Ctrl+X<\/b> on keyboard, type <b>y<\/b>, and hit Enter to save it.<\/p>","protected":false},"excerpt":{"rendered":"<p>Want to run a command or a script daily, weekly, monthly, or on other given schedule? It&#8217;s easy to do this in Ubuntu via cron job. Cron is a time-based job scheduler to run command or script periodically at fixed times, dates, or intervals. It&#8217;s typically used for system maintenance or administration, though it can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":33803,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[2033],"class_list":["post-33802","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","tag-schedule-task"],"_links":{"self":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/33802","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/comments?post=33802"}],"version-history":[{"count":0,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/33802\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media\/33803"}],"wp:attachment":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media?parent=33802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/categories?post=33802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/tags?post=33802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}