One of the most valuable benefits of having computers take charge of many aspects of our lives is the way they can complete tasks over and over again, following a defined schedule without complaint. How do they do this? The answer is something called “cron jobs”.

In this post, you’ll learn exactly what a cron job is, why people use them, and how you can use cron jobs yourself for automating mundane tasks on your server.

But even if you’re not a Linux expert then don’t worry, we will also share a novel method for novice Linux users to manage their system much more effectively using cron jobs.

Let’s get started!

What is a Cron Job?

A cron job is a command or a script that is executed by the cron daemon, a background service that runs on Linux systems and which checks for scheduled tasks. A cron job consists of two parts: a schedule and a command.

  • The schedule defines when and how often the command should run.
  • The command defines what should be executed.

Let’s take a look at each of them individually.

Understanding Cron Schedules

The schedule is specified using a special syntax that consists of five fields:

  1. minute
  2. hour
  3. day of month
  4. month
  5. day of week.

Each field can have a single value, a range of values, a list of values, or a special character. The fields are separated by spaces or tabs.

Here is an example of a schedule:

30 10 * * 1-5

The above schedule means that the specified command should run at 10:30 AM on every weekday (Monday to Friday). The asterisk (*) means any value, and the dash (-) means a range of values. You can also use a comma (,) to separate multiple values, a slash (/) to specify increments, and predefined keywords, such as @reboot, @daily, @weekly

You can use these predefined keywords to make your scripts more readable. For example, if you want to run something once a week then instead of writing 0 0 * * 0, you can simply write @weekly

To learn more about creating cron schedules, we recommend you take a look at these useful articles:

Understanding Cron Task

The second part of the cron syntax defines a task that needs to be completed. It can be either any valid Linux command, or a path to a script that can be executed. The command can have arguments, redirections, pipes, etc. The output of the command is usually sent to the user’s email, unless it is redirected to a file or another command.

Here is an example of a command:

/usr/bin/backup.sh

This command runs a script that performs a backup operation. The script must have the executable permission, and the correct shebang line.

Why Use Cron Jobs?

Cron jobs are extremely valuable for automating repetitive or periodic tasks, such as backups, updates, notifications, etc. You can use these schedules for a variety of tasks such as:

  • Importing data from another system on a recurring basis
  • Making changes to properties in your CRM based on time and date (such as annually, monthly, or daily)
  • Running system management commands, such as clearing the cache, updating packages, checking disk space, etc.
  • Running scripts or programs that perform specific tasks, such as generating reports, sending emails, scraping websites, etc.
  • Scheduling backups of your files, databases, or websites
  • Many popular web platforms such as Magento and WordPress rely on cron jobs to perform various tasks related to their functionality and performance. Some of these tasks are: indexing databases, sending newsletters, processing orders, etc.

Suggested read: Understanding WordPress Cron Jobs

How to Create a Cron Job

To create a cron job on Linux, you need to edit the crontab file. This is a text file that stores the cron jobs for a particular user account on the computer. You can use the crontab -e command to edit the crontab file using your default editor. If you are running the command for the first time, you may be asked to choose an editor, such as nano, vim, emacs, etc.

Suggested read: How to create WordPress cron jobs with wp-cli tool

Select the number corresponding to your desired editor to use that one, or just press ‘Enter’ to use the default editor.

When working on the crontab file, you should keep following things in mind:

  1. Lines starting with the hash (#) sign are comments, and are completely ignored by the computer
  2. Blank lines are also ignored 
  3. All other lines represents an individual cron job

As noted previously, cron jobs follow the schedule command format. If we want to use the schedule and command described above, we will add the following line to the crontab file. 

Note that at the end of the command, we have added a helpful comment that will make it easy to identify this cron task if we ever wish to make changes to it in the future.

30 10 * * 1-5 /usr/bin/backup.sh # Run backup.sh at 10:30 AM on weekdays

You can add, modify, or delete lines in the crontab file to create or update your cron jobs. After you save and exit the editor, the cron daemon will reload the crontab file and apply the changes. You can also use the crontab -l command to list the current crontab file, or the crontab -r command to remove the crontab file.

For more details on the cron schedule syntax, you can check the crontab man page.

How to Manage a Cron Job

Cron jobs are usually set and forget – there isn’t much that you usually need to do once it has been enabled. However, if you suspect that your jobs are not running properly, then you can use the following tips to manage and troubleshoot your cron jobs:

  • To disable a cron job temporarily, you can comment out the line in the crontab file by adding a hash sign # at the beginning of the line.
  • To enable a cron job, you can uncomment the line in the crontab file by removing the hash sign # at the beginning of the line.
  • To view the status of the cron daemon, you can use the systemctl command or the service command, depending on your Linux distribution. For example, systemctl status cron or service cron status.
cron status linux
  • To restart the cron daemon, you can use the systemctl command or the service command, depending on your Linux distribution. For example, systemctl restart cron or service cron restart.

Creating Cron Jobs the Easy Way with RunCloud

Managing Linux can be hard and difficult, especially for beginners or non-technical users. Linux commands and scripts can be complex and error-prone, and require a lot of knowledge and experience to use them effectively. This is why we built RunCloud!

RunCloud is a cloud server management panel that simplifies and automates the process of creating and managing cron jobs on Linux servers.

We provide a feature-rich GUI that helps you create and edit cron jobs right from the dashboard, without the need to use the terminal or edit the crontab file manually.

To create a new job on your server, simply open your RunCloud dashboard and click on the “Cronjob” tab in the left menu.

On the next screen, just click on “Add New Job” to get started.

When creating a new job, simply fill in the basic information, such as the name of the job and the command that you want to execute – everything else will be pre-filled for you.

cron jobs runcloud

The above screenshot shows an example of a cron job created using RunCloud. This cron job will run the WordPress cron events for the app-pat2 web application every minute, using the runcloud user and the wp-cli tool.

Let’s take a look at each of the settings that you can modify.

  • Job Label: This is a name or description for the cron job, which can help you identify and organize your cron jobs.
  • User: This is the system user that will execute the cron job. You can choose any user that has the permission to run the command or script.
  • Vendor Binary: This is the binary or interpreter that will run the command or script. You can choose from a list of vendor binaries provided by RunCloud, such as /bin/bash, /usr/bin/php, etc. or you can write your own binary inside the command field.
  • Command: This field contains the actual command or script that will be executed by the cron job. You can write any valid Linux command or a path to a script that can be executed. In this case, the command is using the wp-cli tool to run the WordPress cron events for the app-pat2 web application.
  • Run In: This is the schedule or frequency of the cron job. You can choose from a list of predefined settings, such as every minute, every hour, every day, etc., or you can use the custom settings to specify your own schedule using the cron syntax.

Why Use RunCloud? 

RunCloud’s cron job feature is more powerful than the traditional way of creating and managing cron jobs on Linux systems, because it offers the following advantages:

  • It has a user-friendly and intuitive GUI that makes it easy to create and edit cron jobs, without the need to use the terminal or edit the crontab file manually.
  • It provides predefined settings and custom settings for scheduling your cron jobs, which can save you time and avoid errors in writing the cron syntax.
  • It provides a list of vendor binaries that you can use to run your commands or scripts, which can simplify your commands and ensure compatibility with your server environment.
  • It allows you to choose any system user that has the permission to run the command or script, which can enhance the security and flexibility of your cron jobs.
  • It allows you to quickly Activate or Disable individual cron jobs without ever needing to log in to your server.
  • It enables you to run cron jobs on demand right from your RunCloud dashboard.

Conclusion

In this article, we have shown you how to create and manage cron jobs on Linux using the crontab command and the cron daemon. Cron jobs are a powerful and convenient way to automate tasks on Linux systems. We hope you have found this article useful and informative. If you have any questions or feedback, please feel free to leave a comment below. 

If you are looking for a simple and powerful way to create and manage cron jobs on Linux servers, you should try RunCloud.

RunCloud is a cloud server management panel that simplifies and automates the process of managing Linux servers. 

RunCloud is more than just a cron job manager. It is a complete solution for managing your web applications, databases, backups, security, and performance on Linux servers. You can use RunCloud to deploy, configure, and optimize your web applications using any PHP version and web server stack.

RunCloud is compatible with any cloud provider, such as AWS, Google Cloud, DigitalOcean, Linode, Vultr, etc. If you want to experience the power and simplicity of RunCloud, you can sign up for a RunCloud account today!