What is a Cron Job? How to use it via CloudPanel?

What is a Cron Job? How to use it via CloudPanel?

Cron allows you to schedule jobs at specific intervals or dates. It automates repetitive tasks for better system maintenance.

For example, you can create cron jobs for daily or weekly backups. It is up to you which tasks you’d like to automate and schedule.

This article explores what are cron jobs and how to use them via CloudPanel.

What Is a Cron Job?

What is a cron job -Overview of cron

Cron is a Linux command used for scheduling tasks. It works on Unix or Unix-like computer operating systems.

The tasks scheduled in the cron are called cron jobs. Cron is a Daemon -a computer program that runs a background process.

Cron Job consists of three components:

  • The script - which is to be called or executed.
  • Command - executes the script regularly.
  • Action or output - depends on what the script does.

How does Cron Work?

How does Cron Work -What is a cron job

Cron jobs are executed based on the crontab (cron table) file.

A crontab file is a simple text file with commands that run periodically on a schedule.

The default system cron table or crontab configuration file is /etc/crontab, located within the crontab directory /etc/cron.*/.

The crontab files are stored where the jobs to the cron daemon are kept.

You can have separate crontab files for different jobs. Only system admins can edit the system crontab file.

Unix-like operating systems support many admin users. Each admin can create a crontab file and write commands to perform jobs.

The cron daemon checks the crontab files and their run times. It then executes the tasks to run in the system background.

What is Crontab Syntax?

What is Crontab Syntax -Overview of a cron job

To create a cron job, you should first know the cron syntax & formatting. Every line in the crontab file represents a job.

The crontab syntax has five fields that show the time to execute the command. It comes with the following possible values:

Field Description
Minute The specific minute the command will run on.
It ranges from minute 0-59.
Hour The specific hour the command will run at.
Ranges from 0-23 in the 24-hour rotation. 0 is equal to midnight.
Day of the month The day of the month the command runs on.
It ranges from 1-31 days.
Month The specific month the command is to run in.
It ranges from 1-12 that represents January-December.
Day of the week The day of the week for a command to run on. It ranges from 0-6 and represents Sunday-Saturday.

Usually, 0 = Sunday, but the value 7 represents Sunday in some systems.

The five time-and-date fields cannot contain spaces.

To set the time correctly, you should also know about cron job operators:

Cron job operators Description
(*) asterisk operator Signifies all possible values in the field.

For example: If you want your cron job to run every hour, write an asterisk in the hour field.
(,) comma operator The comma allows you to list multiple values.

For example: Adding 1,5 in the day of the week field will schedule the task for every Monday and Friday.
(-) hyphen operator Hyphen allows you to specify a range of values.

For example: Adding hyphen between February to June. Write 2-6 in the Month field.
(/) slash operator Slash divides the value such as */12 in the hour field. It makes the script run every 12 hours.
(#) hash operator Specifies the day of the week and a number ranging from 1-5.

For example, 2#3 means the third Tuesday of the month.
(?) question mark It is used to add “no specific value” for the day of the month and day of the week fields.

It is used to add “no specific value” for the day of the month and day of the week fields.

Cron job special strings:

Special strings are used to schedule time intervals more easily. Instead of numbers, you can write @, followed by a phrase:

  • @hourly -The cron job is scheduled to run once an hour.
  • @daily or @midnight -Run the task every day at midnight.
  • @weekly -The script runs the command once a week at midnight on Sunday.
  • @monthly -Run a command once on the first day of every month.
  • @yearly -The special string runs a task once a year at midnight on January 1st.
  • @reboot -The job will run only once at startup.

Basic Cron Operations

Basic Cron Operations -Overview of a cron job

Before moving to cron operations, let's understand the crontab configuration file:

  • System crontab -Use it to schedule system-wide cron jobs. You require root privileges to make changes.

  • User crontab -The user crontab file lets users create and edit cron jobs. It only applies at the user level.

Cron operations:

1. crontab-e

crontab-e

Used to create or edit a crontab file. You can also add, edit and delete cron jobs.

2. crontab-1

crontab -l

You can use this in the command line to see a list of active scheduled tasks in your system.

For multiple users, you can view their crontab file lists by using this command as a superuser:

crontab -u username -l

3. crontab -r

crontab -r

The command deletes all scheduled tasks in the crontab file.

You can also use the following command to remove the crontab. It will provide a yes/no option before deleting the crontab.

crontab -i

The root user can also add cron jobs to the etc/cron.d directory. However, the user adding cron jobs to this directory must have root access.

Cron Variables

The commonly used crontab variables are listed below:

Cron variables Description
SHELL The path of the current user’s shell, such as bash.
PATH The list of directories in the search path for cron.
HOME The home directory of the logged user.
MAIL The mail storage location of the current user.
MAILTO Sets who get the email with the output of each command
EDITOR The default file editor.
HOME The home directory of the logged user.
LOGNAME The current user's name.

Cron Syntax Examples

Cron Syntax Examples

  • 0 0 * * 0 /root/backup.sh

    Performs a backup every Sunday at midnight

  • 0 6,18 * * * /root/backup.sh
    Backup data twice a day at 6am and 6pm.

  • */15 * * * * /root/backup.sh

    Perform a backup every 15 minutes.

  • 0 * * * 1 /root/clearcache.sh

    Clear the cache every hour on Mondays.

  • 0 22 * * 1-5 /root/clearcache.sh

    Clear the cache every weekday (Monday to Friday) at 10pm.

  • @hourly /scripts/monitor.sh

    Perform monitoring every hour.

  • 15 9 1,20 * * /scripts/monitor.sh

    Perform monitoring at 9:15 pm on the 1st and 20th of every month.

Cron Permissions

Cron Permissions and access

You can allow or restrict users from using the system’s cron file. Use the following commands to set user permissions:

  • /etc/cron.allow – cron.allow should have a user’s name so they can use cron jobs.

  • /etc/cron.deny – cron.deny users are not allowed to execute any file. The user assigned for cron jobs should not be listed in the file.

Limitations of Cron Jobs-

Limitations of Cron Jobs

  • The shortest interval for jobs is 60 seconds - If a task needs to run every 30 seconds, you can't perform it with cron.

  • Centralized to one computer - If the computer running cron crashes, the scheduled tasks won’t be executed.

  • Cannot re-execute a task - If a task fails, it won’t run again until the next scheduled time.

  • Logging: Crons are not logged unless you set it up. The cron output logs are usually not configured.

Adding a Cron Job via CloudPanel

You can directly add cron jobs with a click of a button on CloudPanel.

  1. To add a Cron job, click on Add Cron Job on the top right.

  2. Select a Template for the fields Minute, Hour, Day, Month, and Weekday.

  3. Enter the Command below.

  4. Select the user from whom the cron job will be executed.

Adding a Cron Job via CloudPanel

You can also delete a specific cron job by clicking on Delete.

In the Settings section, you can enter an email address. It allows you to receive the output every time the cron job runs.

That way you get notified of executed cron jobs.

End Note

Cron jobs are a practical way to manage your server and automate repetitive tasks. The scheduled jobs are executed at the specific time intervals.

You have to enter the commands and pick the execution time. Use the correct syntax mentioned in the article for cron jobs.

It is also faster to create and delete cron jobs with CloudPanel.

To learn more about managing your server, check out the CloudPanel blog.

Nikita S.
Nikita S.
Technical Writer

As a professional content writer, Nikita S. is experienced in crafting well-researched articles that simplify complex information and promote technical communication. She is enthusiastic about cloud computing and holds a specialization in digital marketing.


Deploy CloudPanel For Free! Get Started For Free!