Guide to add a Cron Job via SSH

Updated on December 3, 2021

A server can be set up to conduct operations on its own, such as running commands or scripts to update entire applications like WordPress or certain aspects of Magento and other popular Content Management Systems.

To transform the manual work to automation, Linux provides the cron service. This time-based scheduler executes the task and records when these actions should be performed periodically at fixed times, dates, or intervals.

To set up a cron job via SSH, let’s go through the table of contents and see what topics we will cover in this article.

1) Cron Job Structure

On Unix-like operating systems, the cron command-line utility, sometimes known as a cron job, is a task scheduler. Cron is used by users who set up and maintain software systems to plan jobs (commands or shell scripts) to run on a regular basis at specific times, dates, or intervals.

Cron Job Structure

2) Cron Job Syntax

  • Minute — The minute of the hour the command will run, ranging 0 – 59.
  • Hour — on what time the command will be executed, ranging 0 – 23.
  • Day of the month — on what day of the month you want to run the command, ranging 1 – 31.
  • Month — on what month the command runs on, ranging 1 – 12.
  • Day of the week — on what day of the week you want to run the command, ranging 0 – 7.

Furthermore, it would be best if you utilized the correct characters in each crontab file.

  • Asterisk (*) — to provide all scheduling variables.
  • Comma (,) — to keep two or more execution periods of the same command.
  • Hyphen (-) — to calculate the time range when specifying several execution periods for a single command.
  • Slash (/) — for generating preset time intervals within a given range.
  • Last (L) — for the particular purpose of determining the last weekday of a certain month. For example, 3L denotes the final Wednesday of the month.
  • Weekday (W) — to get the nearest weekday of a particular time. For example, 1W indicates that if the 1st falls on a Saturday, the order will be executed on Monday (the 3rd).
  • Hash (#) — followed by a number ranging from 1 to 5 to determine the day of the week. For example, 1#2 denotes the second Monday of the month.
  • Question mark (?) — to leave empty.

3) Special Strings

You can also use the special string in the cron jobs.

Special StringDescriptionFormatExample
@yearly
Runs once a year at midnight on January 1st.
0 0 1 1 *
@yearly cat /dev/null > test.log
@monthly
Runs on the first day of every month at midnight
0 0 1 * *
@monthly cat /dev/null > test.log
@weekly
Run once a week at midnight on Sunday morning
0 0 * * 0
@weekly cat /dev/null > test.log
@daily
Run once a day at midnight
0 0 * * *
@daily cat /dev/null > test.log
@hourly
Run once an hour at the beginning of the hour
0 * * * *
@hourly cat /dev/null > test.log
@reboot
Run at startup (of the cron daemon)
@reboot
@reboot cat /dev/null > test.log

4) Cron Job Basic Commands

  • $ crontab e — to create and modify a crontab file.
  • $ crontab -u username -e — to modify another user’s crontab file with superuser privileges.
  • $ crontab -l — to modify another user’s crontab file with superuser privileges.
  • $ crontab -r — to delete the crontab file(s).
  • $ crontab -a filename — to install the filename as a crontab file (–a is optional on some platforms).

5) Cron Permissions

In order to run cron, the system administrator or superuser must provide user-specific permissions. Access to the crontab command can be restricted using two files in the /etc/cron.d directory: cron.deny and cron.allow. Only defined users are allowed to conduct crontab command activities such as creating, changing, displaying, or removing their own crontab files.

When it comes to cron jobs, two files are critical.

  • If allow exists, it must include the user’s name for the user to use a cron job.
  • If the cron.allow file exists but the deny file does, the user must not be mentioned in the cron.deny file to use cron jobs.

6) Cron Job Examples

To create a cronjob, you will need to connect your web application via SSH.

Once you connect the SSH, run the below command inside the public_html folder to edit the crontab file.

crontab –e

Once you are inside the crontab section using the -e flag. Then you can write the cron at the bottom of the file. Once you are done editing, save and exit the file.

crontab SSH

If you want to list out all the crons, use the following command:

crontab –l
list out all the crons

To remove the cron, run the following command:

crontab -r

Now that you know how to write cron syntax correctly, we’ll provide you some more examples to assist you to grasp the guidelines indicated above.

Before proceeding, keep in mind that the command’s output will be automatically forwarded to your local email account. So, to avoid getting these emails, add >/dev/null 2>&1 to the syntax, as seen below:

0 5 * * * /public_html/backup.sh >/dev/null 2>&1

If you wish to direct the email output to a specific account, use MAILTO followed by the email address.

MAILTO="[email protected]"
0 5 * * * /public_html/backup.sh >/dev/null 2>&1

Below are some more syntax examples:

0 0 * * * /bin/sh backup.sh
To backup the database at midnight and run it once a day.
0 6,18 * * * /bin/sh backup.sh
To backup the database twice a day, at 6 a.m. and 6 p.m.
*/30 * * * * /applications/application_folder_name/public_html/script/script.sh
Every 30 minutes, run a cron job for the script file in the home directory.
10-59/5 5 * * * /applications/application_folder_name/public_html/script/script.sh
To execute a command every 5 minutes at 5 a.m., beginning at 5:10 a.m.
* * * * * /scripts/script.sh; /scripts/scrit2.sh
To schedule several jobs using a single cron job.

Here is another example to clear the access log file using the command and script:

Command: 30 12 * * * cat /dev/null > /var/log/access.log

Script: 30 12 * * * /applications/application_folder_name/public_html/script/scripts/clear_log.sh

If you want to verify if the cron is executed or not, kindly contact our Live Chat support, and our engineer will share all the details you need related to cronjobs.

Conclusion:

Setting up automatically scheduled jobs is a convenient way to avoid forgetting crucial activities and automating them instead for a smooth flow of operations.

Cron jobs are a great approach for system administrators and web developers to automate repetitive operations. All that remains is to enter the appropriate commands and select the appropriate execution time.

Remember to provide the cron file adequate permissions so that only authorized people may access it.

Need More Help!

Type in your question at the knowledge base website or comment below

Leave a Comment

Your email address will not be published. Required fields are marked *