Guide to Cron Jobs

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, indicating wide acceptability. As a webmaster, you can automate your processes using cron jobs. 

This article guides you on everything you need to know about cron jobs, including their applications, syntax, examples, permissions, and of course, how to set it up on SPanel.

So, let’s get to it.

Understanding Cron and Cron Jobs?

The name “Cron” comes from the ancient Greek god of time—Chronos. A cron is a time-based scheduler that enables web admins to automate repetitive tasks. 

For example, they could schedule system maintenance, file backup, or send emails at a specific time, day and month without handling them repeatedly. As a daemon, cron works in the background to execute jobs.

A daemon is a program that works uninterruptedly in the background and independent of users’ direct control. It sits idly waiting for command requests or specific events or occurrences to perform a cron job.

Cron jobs are the tasks a cron performs.  It gives commands to schedule tasks to run periodically at a fixed time, helping webmasters execute recurring tasks automatically. A cron job is perfect for computers that run 24/7.

In a nutshell, cron is the utility program that schedules and executes cron jobs.

Cron Job Use Cases

Using cron comes with a lot of benefits; some of its use cases include:

  • Automating system maintenance
  • Scheduling database backups
  • Monitoring disk spaces
  • Cleaning up databases, log files and comments
  • Updating a website’s RSS feeds
  • Automating newsletter distribution
  • Sending welcome messages to new email subscribers
  • Scheduling content on blogs
  • Moving subscribers between email lists
  • Generating invoices and a lot more

Cron Job Structure and Syntax

Cron uses the crontab to organize and execute jobs. The crontab, short for cron table, is a special configuration file that contains all the active cron jobs that need execution. It’s where the list of jobs and commands to the cron daemon resides, with each line representing a cron job or declaring an environment variable. 

The cron ignores empty lines or lines beginning with spaces, tabs or #. Instead, it considers lines starting with # as the user’s comments.

Guide to Cron Jobs, Cron Job Structure and Syntax

You can access the crontab file by running the command [server]$ crontab -e  at the command line.

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.

* * * * * command to execute

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.

Let’s simplify this with graphics.

Guide to Cron Jobs, Cron Job Structure and Syntax 2
  • min defines the minute of the hour the command will execute. It ranges from 0 to 59.
  • hour shows the hour the command will run, ranging from 0 to 23.
  • day of the month is the day you wish the command run, and it ranges between 1 to 31.
  • month specifies the month the command will run and ranges from 1 to 12.
  • day of the week defines the day, Sunday to Saturday you wish the command to run, specified as 0 to 7.

You could use the wildcard “*” to instruct the cron to execute the job at any time.  For instance, the cron job 0 0 1 1 * execute command instructs the cron to run the command once a year at January 1st midnight, without caring whether it’s Monday, Tuesday, or any of the week.

The wildcard means there’s no specified day of the week for the command to run. It can run on any day of the week, provided it’s January 1st at midnight. You could also use special operators to add multiple values to a column. 

Let’s take a quick look at some of them:

  • Asterisk (*): the operator works as a wildcard, as explained above.
  • Comma (,): this operator helps you add two or more execution times, for example, 0 0 1 1,3,7 * execute command.
  • Dash (-): use this operator to specify a range of value, for example, 0 0 1 1-3 * execute command instead of 0 0 1 1,2,3 * execute command
  • Slash (/): the / operator helps you create a predetermined interval in a given range; for instance, the cron job 0 0 1 */3 * execute command runs the specified task every three months. 
  • Weekday (W): 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.

Cron Job Examples

Taking your time to understand the cron structure and syntax will enable you to write cron commands correctly, but let’s look at five real examples that make it more straightforward.

0 0 * * * /bin/sh backup.sh 

The cron job runs once daily, executing database backup at midnight:

0 4,18 * * * /bin/sh backup.sh 

The above cron job runs twice daily, executing database backup at 4 am and 6 pm:

0 */3 * * * /scripts/monitor.sh 

It performs monitoring every three hours:

*/30 * * * */usr/bin/wget -P /home/username/cronfolder/ https://example.com/index.html

The cron job uses wget to download a file to a directory named cronfolder every 30 minutes:

0 6 10 */3 * /home/user/script.sh 

The crontab command runs quarterly on the tenth day at 6 am, executing the specified order:

You can specify the email account to receive the cron job output using the MAILTO variable, like in the example below:

MAILTO=”myemail@domain.com”

0 6 10 */3 * /home/user/script.sh 

Cron Job Permission

You can control access to the crontab command and specify who runs commands using two files—the cron.deny and cron.allow files. You can find the files in the /etc/cron.d directory.

  • If it exists, the cron.allow file defines the users that can create, edit, display, or create crontab files. Only the users in the file can use the cron job.
  • If the cron.allow  file doesn’t exist, then all users can use the cron job except those listed in the cron.deny file.
  • But if neither files exist, only superusers can run crontab commands, and only they can create or edit the two files.

Setting Up Cron Job On SPanel

Of course, you can create a cron job via the command line using SSH access, but you could make it a lot easier using SPanel—Scala Hosting’s all-in-one control panel.

You could take these steps to get it done in a few minutes.

Step 1: Log into SPanel’s User Interface.

The default login URL is yourdomain.com/spanel (make sure you substitute yourdomain.com with your actual domain).

Guide to Cron Jobs, Step 1: Log into SPanel’s User Interface.

Step 2: Access SPanel’s Cron Job management tool.

The Cron jobs icon is available under the Tools section on SPanel’s homepage.

Guide to Cron Jobs, Step 2: Access SPanel’s Cron Job management tool.

Step 3: Create your Cron Job

Under NEW CRON JOB, set up a custom schedule by selecting from the drop-down, or you could use the preset schedule option for a quick setup.

Guide to Cron Jobs, Step 3: Create your Cron Job

Paste your command into the Cron command text box and click the Create cron Job button to finish the setup.

Wrapping It Up

You can automate the execution of commands or scripts using a cron job. If you have questions regarding setting up cron jobs via SPanel or the command line, reach out to our support, and we’ll be happy to help.

Was this helpful?

What’s your goal today?

1. Find the right Web hosting solution

If you’re looking for industry-leading speed, ease of use and reliability Try ScalaHosting with an unconditional money-back guarantee.

2. Make your website lighting fast

We guarantee to make your WordPress site load in less than 2 seconds on a managed VPS with ScalaHosting or give your money back. Fill out the form, and we’ll be in touch.

Please enter a valid name
Please enter a valid website
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

3. Streamline your clients’ hosting experience

If you’re a web studio or development agency hosting more than 30 websites, schedule a call with Vlad, our co-founder and CTO, and see how we can deliver unmatched value to both your business and your clients.

Photo

Need a custom cluster or professional advice?

Book a meeting and get a free 30-minute consultation with Vlad, co-founder & CTO of Scala Hosting, who will help you select, design and build the right solution - from a single data center cluster to a multi-region & multi-datacenter high availability cluster with hundreds of servers.

Book a free consultation

4. Learn how to grow your website in 2024

An all-star team of SEO and web influencers are sharing their secret knowledge for the first time in years. Learn about the future of SEO, Web Design best practices and the secrets to getting the foundation for your website to thrive. Watch the exclusive webinar.

An Exclusive Insiders Look Behind The SEO and Web Development Curtain

Rado
Author

Working in the web hosting industry for over 13 years, Rado has inevitably got some insight into the industry. A digital marketer by education, Rado is always putting himself in the client's shoes, trying to see what's best for THEM first. A man of the fine detail, you can often find him spending 10+ minutes wondering over a missing comma or slightly skewed design.