{"id":3553,"date":"2021-01-18T11:43:38","date_gmt":"2021-01-18T11:43:38","guid":{"rendered":"https:\/\/linuxways.net\/?p=3553"},"modified":"2021-01-18T12:12:51","modified_gmt":"2021-01-18T12:12:51","slug":"how-to-schedule-a-job-in-cron-to-run-every-hour-in-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-schedule-a-job-in-cron-to-run-every-hour-in-ubuntu-20-04\/","title":{"rendered":"How to Schedule a Job in Cron to Run Every Hour in Ubuntu 20.04"},"content":{"rendered":"<p>Cron is a utility that is used to schedule jobs according to a specific week, month, day,\u00a0 time, or time intervals. It is a time-based job scheduler that is pre-installed in Unix-like operating systems: Mac and Linux.<\/p>\n<p>In this article, we will use Cron to schedule a job to run every hour on Ubuntu 20.04 LTS (Focal Fossa).<\/p>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>Ubuntu 20.04 LTS<\/li>\n<li>Sudo access<\/li>\n<\/ul>\n<p><strong>Note:<\/strong> The commands discussed in this article have been tested on <strong>Ubuntu 20.04 LTS<\/strong> <strong>(Focal Fossa).<\/strong><\/p>\n<h2>Script to be scheduled<\/h2>\n<p>We need a small script that can be run as a job by Cron. For this purpose, we have used the <em>date<\/em> command in our script. The output of a command is date and time which is routed to <em>file.txt<\/em> using <em>&gt;&gt;<\/em> directive.<\/p>\n<pre>date &gt;&gt; file.txt<\/pre>\n<p>Open a file (we name it <em>demo.sh<\/em> here). Write this snippet in the file, save, and exit. The file will be saved in our current directory which is \/home\/usman\/<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1640\" height=\"940\" class=\"wp-image-3554\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-.png\" alt=\"C:\\Users\\MUHAMM~1\\AppData\\Local\\Temp\\Rar$DRa11832.47528\\img\\cron1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-.png 1640w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832--300x172.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832--1024x587.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832--768x440.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832--1536x880.png 1536w\" sizes=\"auto, (max-width: 1640px) 100vw, 1640px\" \/><\/p>\n<h2>Enlist already scheduled jobs<\/h2>\n<p>The jobs that are already scheduled in the <em>crontab<\/em> can be enlisted using the following command:<\/p>\n<pre>$ crontab \u2013l<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1640\" height=\"940\" class=\"wp-image-3555\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-1.png\" alt=\"C:\\Users\\MUHAMM~1\\AppData\\Local\\Temp\\Rar$DRa11832.21467\\img\\cronList.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-1.png 1640w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-1-300x172.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-1-1024x587.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-1-768x440.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-1-1536x880.png 1536w\" sizes=\"auto, (max-width: 1640px) 100vw, 1640px\" \/><\/p>\n<p>Since we are enlisting the jobs here before initiating any, it reasonably prompts &#8220;no crontab for user&#8221;.<\/p>\n<p>Let us add our first cron job now.<\/p>\n<h2>Add a new cron job<\/h2>\n<p>The parameter <em>\u2013e<\/em> is used to add a new job to cron.<\/p>\n<pre>$ crontab -e<\/pre>\n<p>The following file will open in our selected text editor.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1640\" height=\"940\" class=\"wp-image-3556\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-2.png\" alt=\"C:\\Users\\MUHAMM~1\\AppData\\Local\\Temp\\Rar$DRa11832.4742\\img\\cron3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-2.png 1640w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-2-300x172.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-2-1024x587.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-2-768x440.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-2-1536x880.png 1536w\" sizes=\"auto, (max-width: 1640px) 100vw, 1640px\" \/><\/p>\n<p>Scroll down to the bottom of the file using the keyboard.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1640\" height=\"940\" class=\"wp-image-3557\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-3.png\" alt=\"C:\\Users\\MUHAMM~1\\AppData\\Local\\Temp\\Rar$DRa11832.10567\\img\\cron4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-3.png 1640w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-3-300x172.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-3-1024x587.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-3-768x440.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-3-1536x880.png 1536w\" sizes=\"auto, (max-width: 1640px) 100vw, 1640px\" \/><\/p>\n<p>Here we will add our command.<\/p>\n<pre>0 *\/1 * * * \/bin\/sh \/home\/usman\/demo.sh<\/pre>\n<p>The first entity represents that the job should execute at the zero<sup>th<\/sup> minute. The second entity represents that it should run after an interval of an hour. Shell in which script is coded and the script itself is mentioned in the next sections of the job.<\/p>\n<p>The following image represents the rest of the entities of the above command.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1378\" height=\"612\" class=\"wp-image-3558\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-4.png\" alt=\"C:\\Users\\MUHAMM~1\\AppData\\Local\\Temp\\Rar$DRa11832.33813\\cron.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-4.png 1378w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-4-300x133.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-4-1024x455.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-4-768x341.png 768w\" sizes=\"auto, (max-width: 1378px) 100vw, 1378px\" \/><\/p>\n<p>Let us write it in our file.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1640\" height=\"940\" class=\"wp-image-3559\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-5.png\" alt=\"C:\\Users\\MUHAMM~1\\AppData\\Local\\Temp\\Rar$DRa11832.38023\\img\\cron9.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-5.png 1640w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-5-300x172.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-5-1024x587.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-5-768x440.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-5-1536x880.png 1536w\" sizes=\"auto, (max-width: 1640px) 100vw, 1640px\" \/><\/p>\n<p>Save and close the file.<\/p>\n<h2>Observe the results<\/h2>\n<p>We will use <em>cat<\/em> utility to observe the results. The utility dumps the content of the file on the command line.<\/p>\n<pre>$ cat file.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1640\" height=\"940\" class=\"wp-image-3560\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-6.png\" alt=\"C:\\Users\\MUHAMM~1\\AppData\\Local\\Temp\\Rar$DRa11832.35892\\img\\cron10.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-6.png 1640w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-6-300x172.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-6-1024x587.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-6-768x440.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/c-users-muhamm1-appdata-local-temp-rardollardra11832-1-6-1536x880.png 1536w\" sizes=\"auto, (max-width: 1640px) 100vw, 1640px\" \/><\/p>\n<p>Here, it can be observed that the utility runs as the hour begins and writes time with a date at the end of the file.<\/p>\n<pre>\u00a0<\/pre>\n<h2>Conclusion<\/h2>\n<p>In this article, we share with you how a cron job can be scheduled to run every hour, and observe their results. Again, for any feedback use the comment section.<\/p>\n\n\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>Cron is a utility that is used to schedule jobs according to a specific week, month, day,\u00a0 time, or time intervals. It is a time-based job scheduler that&hellip;<\/p>","protected":false},"author":1,"featured_media":3594,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[145],"class_list":["post-3553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-cron"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/3553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=3553"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/3553\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/3594"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=3553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=3553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=3553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}