{"id":2198,"date":"2021-06-25T10:57:10","date_gmt":"2021-06-25T07:57:10","guid":{"rendered":"https:\/\/www.scalahosting.com\/kb\/?p=2198"},"modified":"2022-06-08T17:29:04","modified_gmt":"2022-06-08T14:29:04","slug":"guide-to-cron-jobs","status":"publish","type":"post","link":"https:\/\/www.scalahosting.com\/kb\/guide-to-cron-jobs\/","title":{"rendered":"Guide to Cron Jobs"},"content":{"rendered":"\n<p><b>The job of managing a website could be overwhelming<\/b><span style=\"font-weight: 400;\">. Automating repetitive tasks and processes enables you to work more efficiently, save time and get more job done. In addition, it allows users to work more productively and reduces overhead costs.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">A report shows that about<\/span> <a href=\"https:\/\/resources.industrydive.com\/state-of-marketing-automation-2019-survey-report\" rel=\"nofollow\"><span style=\"font-weight: 400;\">75 percent<\/span><\/a><span style=\"font-weight: 400;\"> of companies use at least one automation tool, indicating wide acceptability. As a webmaster, you can automate your<\/span><b> processes using cron jobs.<\/b><span style=\"font-weight: 400;\">&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">This article guides you on everything you need to know about cron jobs, including their <\/span><i><span style=\"font-weight: 400;\">applications, syntax, examples, permissions<\/span><\/i><span style=\"font-weight: 400;\">, and of course, how to set it up on <\/span><a href=\"https:\/\/www.scalahosting.com\/spanel.html\"><b>SPanel<\/b><\/a><span style=\"font-weight: 400;\">.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">So, let&#8217;s get to it.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Understanding Cron and Cron Jobs?<\/b><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">The name \u201cCron\u201d comes from the ancient Greek god of time\u2014<\/span><b>Chronos<\/b><span style=\"font-weight: 400;\">. A cron is a time-based scheduler that enables web admins to automate repetitive tasks.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">For example, they could <\/span><b>schedule system maintenance, file backup, or send emails at a specific time, day and month <\/b><span style=\"font-weight: 400;\">without handling them repeatedly. As a daemon, cron works in the background to execute jobs.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">A daemon is a program that works uninterruptedly in the background and independent of users\u2019 <\/span><b>direct control<\/b><span style=\"font-weight: 400;\">. It sits idly waiting for command requests or specific events or occurrences to perform a cron job.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Cron jobs are the tasks a cron performs.&nbsp; It gives commands to <\/span><b>schedule tasks to run periodically at a fixed time<\/b><span style=\"font-weight: 400;\">, helping webmasters execute recurring tasks automatically. A cron job is perfect for computers that run 24\/7.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">In a nutshell, cron is the utility program that schedules and executes cron jobs.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Cron Job Use Cases<\/b><\/h2>\n\n\n\n<p><b>Using cron comes with a lot of benefits; some of its use cases include:<\/b><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Automating system maintenance<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Scheduling <\/span><a href=\"https:\/\/www.scalahosting.com\/kb\/how-to-restore-a-database\/\"><b>database backups<\/b><\/a><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Monitoring <\/span><b>disk spaces<\/b><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Cleaning up <\/span><b>databases, log files and comments<\/b><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Updating a website\u2019s <\/span><b>RSS feeds<\/b><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Automating newsletter distribution<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Sending <\/span><b>welcome messages to new email subscribers<\/b><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Scheduling <\/span><b>content on blogs<\/b><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><b>Moving subscribers<\/b><span style=\"font-weight: 400;\"> between email lists<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><b>Generating invoices <\/b><span style=\"font-weight: 400;\">and a lot more<\/span><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Cron Job Structure and Syntax<\/b><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">Cron uses the crontab to<\/span><b> organize and execute jobs<\/b><span style=\"font-weight: 400;\">. The crontab, short for cron table, is a special configuration file that contains all the active cron jobs that need execution. It\u2019s where the <\/span><b>list of jobs and commands to the cron daemon resides<\/b><span style=\"font-weight: 400;\">, with each line representing a cron job or declaring an environment variable.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">The cron ignores empty lines or lines beginning with spaces, tabs or #. Instead, it considers lines starting with # as the user\u2019s comments.<\/span><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"383\" height=\"76\" src=\"https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2021\/06\/image4-8.png\" alt=\"Guide to Cron Jobs, Cron Job Structure and Syntax\" class=\"wp-image-2199\" srcset=\"https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2021\/06\/image4-8.png 383w, https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2021\/06\/image4-8-300x60.png 300w\" sizes=\"(max-width: 361px) 660px, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 910px, 1140px\" \/><\/figure>\n<\/div>\n\n\n<p><span style=\"font-weight: 400;\">You can access the crontab file by running the command <\/span><span style=\"font-weight: 400;\">[server]$ <\/span><span style=\"font-weight: 400;\">crontab -e<\/span><span style=\"font-weight: 400;\">&nbsp; at the command line.<\/span><\/p>\n\n\n\n<p><i><span style=\"font-weight: 400;\">The crontab follows a basic syntax consisting of two parts arranged in six columns like a table: the time scheduler and the command, like the example below.<\/span><\/i><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">* * * * * command to execute<\/span><\/p>\n\n\n\n<p><i><span style=\"font-weight: 400;\">The first five columns represented by asterisks in the above example indicate the time scheduler and defines when the command will run, while the last part contains the command or script to execute.<\/span><\/i><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Let\u2019s simplify this with graphics.<\/span><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"822\" height=\"171\" src=\"https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2021\/06\/image1-7.png\" alt=\"Guide to Cron Jobs, Cron Job Structure and Syntax 2\" class=\"wp-image-2203\" srcset=\"https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2021\/06\/image1-7.png 822w, https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2021\/06\/image1-7-300x62.png 300w, https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2021\/06\/image1-7-768x160.png 768w\" sizes=\"(max-width: 361px) 660px, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 910px, 1140px\" \/><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li style=\"font-weight: 400;\" aria-level=\"1\"><b>min<\/b><span style=\"font-weight: 400;\"> defines the minute of the hour the command will execute. It ranges from 0 to 59.<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><b>hour<\/b><span style=\"font-weight: 400;\"> shows the hour the command will run, ranging from 0 to 23.<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><b>day of the month<\/b><span style=\"font-weight: 400;\"> is the day you wish the command run, and it ranges between 1 to 31.<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><b>month<\/b><span style=\"font-weight: 400;\"> specifies the month the command will run and ranges from 1 to 12.<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><b>day of the week<\/b><span style=\"font-weight: 400;\"> defines the day, Sunday to Saturday you wish the command to run, specified as 0 to 7.<\/span><\/li><\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">You could use the wildcard \u201c*\u201d to instruct the cron to execute the job at any time.&nbsp; For instance, the cron job <\/span><b>0 0 1 1 * execute command<\/b><span style=\"font-weight: 400;\"> instructs the cron to run the command once a year at January 1st midnight, without caring whether it\u2019s Monday, Tuesday, or any of the week.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">The wildcard means there&#8217;s no specified day of the week for the command to run. It can run on any day of the week, provided it\u2019s January 1st at midnight. You could also <\/span><b>use special operators to add multiple values to a column.&nbsp;<\/b><\/p>\n\n\n\n<p><b>Let\u2019s take a quick look at some of them:<\/b><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li style=\"font-weight: 400;\" aria-level=\"1\"><b>Asterisk (*):<\/b><span style=\"font-weight: 400;\"> the operator works as a wildcard, as explained above.<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><b>Comma (,):<\/b><span style=\"font-weight: 400;\"> this operator helps you add two or more execution times, for example, <\/span><b>0 0 1 1,3,7 * execute command.<\/b><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><b>Dash (-):<\/b><span style=\"font-weight: 400;\"> use this operator to specify a range of value, for example, <\/span><b>0 0 1 1-3 * execute command <\/b><span style=\"font-weight: 400;\">instead of <\/span><b>0 0 1 1,2,3 * execute command<\/b><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><b>Slash (\/):<\/b><span style=\"font-weight: 400;\"> the \/ operator helps you create a predetermined interval in a given range; for instance, the cron job <\/span><b>0 0 1 *\/3 * execute command<\/b><span style=\"font-weight: 400;\"> runs the specified task every three months.&nbsp;<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><b>Weekday (W):<\/b><span style=\"font-weight: 400;\"> you could use this operator to instruct the cron to run the command at the closest weekday of a given time; for instance, if the specified runtime is Saturday, the operator makes cron execute the command Monday.<\/span><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Cron Job Examples<\/b><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">Taking your time to <\/span><b>understand the cron structure and syntax <\/b><span style=\"font-weight: 400;\">will enable you to write cron commands correctly, but let\u2019s look at five real examples that make it more straightforward.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">0 0 * * * \/bin\/sh backup.sh&nbsp;<\/span><\/p>\n\n\n\n<p><i><span style=\"font-weight: 400;\">The cron job runs once daily, executing database backup at midnight:<\/span><\/i><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">0 4,18 * * * \/bin\/sh backup.sh&nbsp;<\/span><\/p>\n\n\n\n<p><i><span style=\"font-weight: 400;\">The above cron job runs twice daily, executing database backup at 4 am and 6 pm:<\/span><\/i><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">0 *\/3 * * * \/scripts\/monitor.sh&nbsp;<\/span><\/p>\n\n\n\n<p><i><span style=\"font-weight: 400;\">It performs monitoring every three hours:<\/span><\/i><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">*\/30 * * * *\/usr\/bin\/wget -P \/home\/username\/cronfolder\/ https:\/\/example.com\/index.html<\/span><\/p>\n\n\n\n<p><i><span style=\"font-weight: 400;\">The cron job uses wget to download a file to a directory named cronfolder every 30 minutes:<\/span><\/i><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">0 6 10 *\/3 * \/home\/user\/script.sh&nbsp;<\/span><\/p>\n\n\n\n<p><i><span style=\"font-weight: 400;\">The crontab command runs quarterly on the tenth day at 6 am, executing the specified order:<\/span><\/i><\/p>\n\n\n\n<p><i><span style=\"font-weight: 400;\">You can specify the email account to receive the cron job output using the <\/span><\/i><b><i>MAILTO <\/i><\/b><i><span style=\"font-weight: 400;\">variable, like in the example below:<\/span><\/i><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">MAILTO=\u201dmyemail@domain.com\u201d<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">0 6 10 *\/3 * \/home\/user\/script.sh&nbsp;<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Cron Job Permission<\/b><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">You can <\/span><b>control access to the crontab command <\/b><span style=\"font-weight: 400;\">and specify who runs commands using two files\u2014the <\/span><span style=\"font-weight: 400;\">cron.deny<\/span><span style=\"font-weight: 400;\"> and <\/span><span style=\"font-weight: 400;\">cron.allow<\/span><span style=\"font-weight: 400;\"> files. You can find the files in the <\/span><span style=\"font-weight: 400;\">\/etc\/cron.d<\/span><span style=\"font-weight: 400;\"> directory.<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">If it exists, the<\/span><span style=\"font-weight: 400;\"> cron.allow<\/span><span style=\"font-weight: 400;\"> file defines the users that can <\/span><b>create, edit, display, or create crontab files<\/b><span style=\"font-weight: 400;\">. Only the users in the file can use the cron job.<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">If the <\/span><span style=\"font-weight: 400;\">cron.allow<\/span><span style=\"font-weight: 400;\">&nbsp; file doesn&#8217;t exist, then all users can use the<\/span><b> cron job except those listed<\/b><span style=\"font-weight: 400;\"> in the <\/span><span style=\"font-weight: 400;\">cron.deny<\/span><span style=\"font-weight: 400;\"> file.<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">But if neither files exist, only superusers can run crontab commands, and only they can create or edit the two files.<\/span><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Setting Up Cron Job On SPanel<\/b><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">Of course, you can create a cron job via the command line using <\/span><a href=\"https:\/\/www.scalahosting.com\/kb\/ssh-access\/\"><span style=\"font-weight: 400;\">SSH access<\/span><\/a><span style=\"font-weight: 400;\">, but you could make it a lot easier using<\/span><b> SPanel\u2014Scala Hosting\u2019s all-in-one control panel.<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">You could take these steps to get it done in a few minutes.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><b>Step 1: Log into SPanel&#8217;s User Interface.<br><\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">The default login URL is <strong><em>yourdomain.com\/spanel <\/em><\/strong>(make sure you substitute <em>yourdomain.com<\/em> with your actual domain).<\/span><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"453\" height=\"456\" src=\"https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2022\/01\/40.png\" alt=\"Guide to Cron Jobs, Step 1: Log into SPanel&#8217;s User Interface.\" class=\"wp-image-2898\" srcset=\"https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2022\/01\/40.png 453w, https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2022\/01\/40-298x300.png 298w, https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2022\/01\/40-150x150.png 150w\" sizes=\"(max-width: 361px) 660px, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 910px, 1140px\" \/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><b>Step 2: Access SPanel&#8217;s Cron Job management tool.<br><\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">The <strong>Cron jobs<\/strong> icon is available under the <strong>Tools<\/strong> section on SPanel&#8217;s homepage.<\/span><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"681\" height=\"176\" src=\"https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2021\/06\/image5-5.png\" alt=\"Guide to Cron Jobs, Step 2: Access SPanel&#8217;s Cron Job management tool.\" class=\"wp-image-2200\" srcset=\"https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2021\/06\/image5-5.png 681w, https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2021\/06\/image5-5-300x78.png 300w\" sizes=\"(max-width: 361px) 660px, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 910px, 1140px\" \/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><b>Step 3: Create your Cron Job<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Under <\/span><b>NEW CRON JOB<\/b><span style=\"font-weight: 400;\">, set up a custom schedule by selecting from the drop-down, or you could use the preset schedule option for a quick setup.<\/span><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"724\" height=\"466\" src=\"https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2021\/06\/image7-4.png\" alt=\"Guide to Cron Jobs, Step 3: Create your Cron Job\" class=\"wp-image-2202\" srcset=\"https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2021\/06\/image7-4.png 724w, https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2021\/06\/image7-4-300x193.png 300w\" sizes=\"(max-width: 361px) 660px, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 910px, 1140px\" \/><\/figure>\n<\/div>\n\n\n<p><span style=\"font-weight: 400;\">Paste your command into the <\/span><b>Cron command<\/b><span style=\"font-weight: 400;\"> text box and click the <\/span><b>Create cron Job<\/b><span style=\"font-weight: 400;\"> button to finish the setup.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Wrapping It Up<\/b><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">You can automate the<\/span><b> execution of commands or scripts using a cron job<\/b><span style=\"font-weight: 400;\">. If you have questions regarding setting up cron jobs via SPanel or the command line, reach out to <\/span><a href=\"https:\/\/www.scalahosting.com\/contact-us.html\"><span style=\"font-weight: 400;\">our support<\/span><\/a><span style=\"font-weight: 400;\">, and we\u2019ll be happy to help.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The job of managing a website could be overwhelming. Automating repetitive tasks and processes enables you to work more efficiently, save time and get more job done. In addition, it allows users to work more productively and reduces overhead costs. A report shows that about 75 percent of companies use at least one automation tool, [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_seopress_robots_primary_cat":"","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":""},"categories":[34],"tags":[],"class_list":["post-2198","post","type-post","status-publish","format-standard","hentry","category-web-hosting"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts\/2198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/comments?post=2198"}],"version-history":[{"count":4,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts\/2198\/revisions"}],"predecessor-version":[{"id":4845,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts\/2198\/revisions\/4845"}],"wp:attachment":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/media?parent=2198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/categories?post=2198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/tags?post=2198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}