{"id":22081,"date":"2023-10-01T16:11:33","date_gmt":"2023-10-01T16:11:33","guid":{"rendered":"https:\/\/linuxways.net\/?p=22081"},"modified":"2023-10-01T16:11:33","modified_gmt":"2023-10-01T16:11:33","slug":"how-to-use-cron-jobs-in-ubuntu-22-04","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-use-cron-jobs-in-ubuntu-22-04\/","title":{"rendered":"How to Use Cron Jobs in Ubuntu 22.04?"},"content":{"rendered":"<p><strong>Cron Jobs<\/strong> are used to perform scheduling actions such as report generation. With cron, we create a Job on a repeating schedule that runs in the background. It is used on most Unix-like systems where one Cron Object corresponds to one line of a crontab also known as Cron Table.<\/p>\n<p>This article explains the use of a Cron Job in Ubuntu.<\/p>\n<h2><strong>How Cron Jobs Work?<\/strong><\/h2>\n<p>The cron jobs are written in a cron format to run a Job periodically in the Future. The Syntax of a cron job is:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"sy0\">&lt;<\/span>minute<span class=\"sy0\">&gt;&lt;<\/span>hour<span class=\"sy0\">&gt;&lt;<\/span>day-of-month<span class=\"sy0\">&gt;&lt;<\/span>month-of-year<span class=\"sy0\">&gt;&lt;<\/span>day-of-the-week<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">&lt;<\/span><span class=\"kw3\">command<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>The Cron Jobs are scheduled according to a five-star pattern. Each asterisk or star represents the time i.e. the first asterisk is for a minute, the second for an hour, the third for a day, the third is for a month and the fourth is for the day of the week. The asterisk (*) means it will run every time. To run a cron Job at 1 p.m. every Tuesday, our cron job syntax will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"nu0\">0<\/span> <span class=\"nu0\">11<\/span> <span class=\"sy0\">*<\/span> <span class=\"sy0\">*<\/span> <span class=\"nu0\">2<\/span> <span class=\"sy0\">&lt;<\/span><span class=\"kw3\">command<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>Here, the \u201c0\u201d means the Job will run only once, the \u201c11\u201d means it will run at 1 p.m. (in 24-hour format), the \u201c<strong>*<\/strong> <strong>*<\/strong>\u201d means the Job will run every day for every month, and the \u201c2\u201d means it will run on Tuesday. Thus this Job will run every Tuesday at 1 pm for every month. Similarly, to run a cron Job every Hour on the Third Day of Every Month, we will make our Job as:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"nu0\">0<\/span> <span class=\"sy0\">*<\/span> <span class=\"nu0\">3<\/span> <span class=\"sy0\">*<\/span> <span class=\"sy0\">*<\/span> <span class=\"sy0\">&lt;<\/span><span class=\"kw3\">command<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<h2><strong>How to Use Cron Jobs in Ubuntu 22.04?<\/strong><\/h2>\n<p>By default, Cron is mostly installed in the Ubuntu but in case Cron does not exist, we need to install it. Follow the steps beneath to install and use Cron in Ubuntu.<\/p>\n<h3><strong>Step 1: Installing Cron<\/strong><\/h3>\n<p>Before installing, update the system packages. To update Ubuntu System Packages, use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt update<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"729\" height=\"442\" class=\"wp-image-22090\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-1.png 729w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-1-300x182.png 300w\" sizes=\"auto, (max-width: 729px) 100vw, 729px\" \/><\/p>\n<p>Once the packages are updated, upgrade the packages:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt upgrade<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"730\" height=\"404\" class=\"wp-image-22096\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-2.png 730w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-2-300x166.png 300w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><\/p>\n<p>To install Cron, use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt <span class=\"kw2\">install<\/span> cron<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"722\" height=\"389\" class=\"wp-image-22103\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-3.png 722w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-3-300x162.png 300w\" sizes=\"auto, (max-width: 722px) 100vw, 722px\" \/><\/p>\n<p>Add the \u201cenable\u201d command with \u201ccron\u201d to ensure cron works in the background:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> systemctl <span class=\"kw3\">enable<\/span> cron<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"729\" height=\"97\" class=\"wp-image-22110\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-4.png 729w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-4-300x40.png 300w\" sizes=\"auto, (max-width: 729px) 100vw, 729px\" \/><\/p>\n<h3><strong>Step 2: Setting Up Cron Job<\/strong><\/h3>\n<p>A Cron Table or a \u201c<strong>crontab<\/strong>\u201d contains all the Cron Jobs as they are specified in it. To create a crontab, use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">crontab <span class=\"re5\">-e<\/span><\/div><\/div>\n<p>The \u201c-e\u201d flag is used for editing:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"716\" height=\"166\" class=\"wp-image-22114\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-5.png 716w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-5-300x70.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/p>\n<p>It will ask for the default text editor if you are using Cron for the first time. Select the editor and press \u201cEnter\u201d. A new file will be created where you can add cron jobs:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"724\" height=\"437\" class=\"wp-image-22121\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-6.png 724w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-6-300x181.png 300w\" sizes=\"auto, (max-width: 724px) 100vw, 724px\" \/><\/p>\n<h3><strong>Step 3: Adding a Cron Job<\/strong><\/h3>\n<p>This file also contains details that inform how you can use cron commands to execute cron jobs. Now add a new cron job at the bottom of the file:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"sy0\">*<\/span> <span class=\"sy0\">*<\/span> <span class=\"sy0\">*<\/span> <span class=\"sy0\">*<\/span> <span class=\"sy0\">*<\/span> <span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;This Cron Job is executed every minute&quot;<\/span> <span class=\"sy0\">&gt;&gt;<\/span> <span class=\"re1\">$HOME<\/span><span class=\"sy0\">\/<\/span>cronjob.txt<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"394\" class=\"wp-image-22130\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-7.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-7.png 726w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-7-300x163.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/p>\n<p>With \u201c <strong>* * * * * <\/strong>\u201c the command will run every minute of every hour of every day of every month.<\/p>\n<p>With this command, the string inside <strong>\u201c \u201d <\/strong>will be executed each minute. That string is then appended to the \u201ccronjob.txt\u201d file:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"732\" height=\"441\" class=\"wp-image-22135\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-8.png 732w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-8-300x181.png 300w\" sizes=\"auto, (max-width: 732px) 100vw, 732px\" \/><\/p>\n<p>Save the file and Exit. This will create a new file \u201ccronjob.txt\u201d on the Homepage. Navigate to your Home Page and you will see the file:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1211\" height=\"390\" class=\"wp-image-22139\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-9.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-9.png 1211w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-9-300x97.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-9-1024x330.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-9-768x247.png 768w\" sizes=\"auto, (max-width: 1211px) 100vw, 1211px\" \/><\/p>\n<p>Open the file and you will see the commands executed:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"896\" height=\"111\" class=\"wp-image-22145\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-10.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-10.png 896w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-10-300x37.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-10-768x95.png 768w\" sizes=\"auto, (max-width: 896px) 100vw, 896px\" \/><\/p>\n<p>As each minute the cron job will be executed, when you open the file after some time, more cron jobs will have been executed:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"905\" height=\"218\" class=\"wp-image-22146\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-11.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-11.png 905w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-11-300x72.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-11-768x185.png 768w\" sizes=\"auto, (max-width: 905px) 100vw, 905px\" \/><\/p>\n<p>Similarly, the following Job executes every 6th day of the month at 9 am:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"nu0\">0<\/span> <span class=\"nu0\">9<\/span> <span class=\"nu0\">6<\/span> <span class=\"sy0\">*<\/span> <span class=\"sy0\">*<\/span> <span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;This Job executes every 9am at the 6th day of the month every month&quot;<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1112\" height=\"459\" class=\"wp-image-22151\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-12.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-12.png 1112w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-12-300x124.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-12-1024x423.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-12-768x317.png 768w\" sizes=\"auto, (max-width: 1112px) 100vw, 1112px\" \/><\/p>\n<p>To check the table of cron jobs i.e. crontab, execute the command below to access the file and see the cron jobs in the table that are currently being executed:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">crontab <span class=\"re5\">-l<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"725\" height=\"492\" class=\"wp-image-22152\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-13.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-13.png 725w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-13-300x204.png 300w\" sizes=\"auto, (max-width: 725px) 100vw, 725px\" \/><\/p>\n<h3><strong>Step 4: Deleting a Cron Job<\/strong><\/h3>\n<p>Once you are done with the tasks or you want to stop the cron job from further executing jobs in the background, use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">crontab <span class=\"re5\">-r<\/span><\/div><\/div>\n<p>This will remove your Cron Job:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"713\" height=\"43\" class=\"wp-image-22156\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-14.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-14.png 713w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-14-300x18.png 300w\" sizes=\"auto, (max-width: 713px) 100vw, 713px\" \/><\/p>\n<p>To verify, open the file you created earlier for the cron job. You can see that no further commands are executed, i.e. the Cron Job stops:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"903\" height=\"256\" class=\"wp-image-22159\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-15.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-15.png 903w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-15-300x85.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22081-15-768x218.png 768w\" sizes=\"auto, (max-width: 903px) 100vw, 903px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>Cron Jobs are powerful tasks that are mostly performed by system administrators to automate tedious tasks. It reduces the burden of tasks for system administrators. This article explained the installation and the use of Cron Jobs in Ubuntu.<\/p>","protected":false},"excerpt":{"rendered":"<p>Cron Jobs are powerful tasks performed by system administrators for task automation. With cron jobs, the scheduling of tasks is easier to interpret.<\/p>","protected":false},"author":110,"featured_media":22193,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-22081","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/22081","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\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=22081"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/22081\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/22193"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=22081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=22081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=22081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}