Table of contents of the article:
In today's digital age, characterized by uninterrupted data flows and 24/24 operations, system administrators are constantly confronted with the need to manage tasks and processes that need to run at specific times. In this context, cron stands out for its utility and functionality, a task scheduling utility present in Unix-like operating systems, including the famous Linux.
Cron proves to be a tool of incredible power and flexibility, capable of automating the execution of commands or scripts according to a defined time schedule. The range of tasks it can handle is wide and varied, including periodically deleting temporary files, creating backups of critical data, monitoring and checking system status, as well as a multitude of other operations which, if handled manually can be costly and error-prone.
One of cron's most appealing aspects lies in its unique combination of versatility and simplicity. While having a complex structure that allows you to define very precise execution schedules, its management is incredibly simple. With a few lines of code, it is possible to program a series of recurring operations, obtaining a considerable saving of time and minimizing errors due to human intervention. This represents a considerable advantage in a context where operational efficiency and system reliability are of vital importance.
How is a cron structured on Linux?
A cron job in Linux is defined by a single line of text within the crontab, the "cron table". This line of text is divided into six distinct parts, each of which represents a different element of the cron job.
These parts, in order, are:
- Minute: This field can assume values from 0 to 59 and indicates the minute of each hour in which the cron job must be executed.
- Time: This field can assume values from 0 to 23 and indicates the hour of the day in which the cron job must be executed.
- Day of the month: This field can assume values from 1 to 31 and indicates the day of the month in which the cron job must be executed.
- Month: This field can assume values from 1 to 12 and indicates the month of the year in which the cron job must be executed.
- Day of the week: This field can assume values from 0 to 7, where both 0 and 7 represent Sunday. Indicates the day of the week the cron job should run.
- Command or script: This is the command you want to run or the path to the script file to run. It can include any command that you would normally run from the command line.
Each field is separated by a space. You can also use special operators like “", "/", "-", "," And "/” to specify cron jobs that repeat at regular intervals or occur at specific times.
For example, the line 0 14 * * * /percorso/allo/script.sh
in the crontab indicates a cron job that will run the specified script every day at 14:00.
How to set up a cron via SSH
Before we begin, it's important to note that you'll need SSH access to your server. If you haven't already, you should get it, as SSH is a vital tool for managing a remote server.
SSH, short for Secure Shell, is a network protocol that allows secure communication between two systems using a client-server architecture. It allows users to control and modify their remote servers over the Internet.
To access your server via SSH, you'll need user credentials, which include the server's IP address, username, and password. If you don't have this information, you'll need to ask your system administrator for it.
Here are the steps to set up a cron via crontab:
- Access to Server: First, connect to your server using SSH. You can do this from your terminal using the command
ssh username@your_server_ip
. - Opening the Crontab: Once connected, you can access the crontab, the table that cron uses to track scheduled jobs, using the command
crontab -e
. If this is your first time using crontab, you will be prompted to choose your text editor of choice. You can choose from options like nano, vi or emacs. - Job writing: Now, you can post your job. Each crontab line represents a job and consists of six parts: minute (0-59), hour (0-23), day of month (1-31), month (1-12), day of week (0- 7, where both 0 and 7 represent Sunday), and the command or script to run.
- Saving the Job: After entering your job, save and close the editor. If you're using nano, for example, you can do this by pressing
CTRL+X
, thenY
to confirm saving and finallyEnter
to confirm the file name.
Examples of Chron
To help you better understand how to set up crons, here are some practical examples:
- Run a command every minute: To do that, you would enter
* * * * * comando
in the crontab. The command will run every minute. - Run a script every day at 2pm: Insert
0 14 * * * /percorso/allo/script.sh
in the crontab. The script will run every day at 14:00. - Run a command on the first day of every month: Insert
0 0 1 * * comando
in the crontab. The command will be executed at 00:00 on the first day of each month. - Run a command every Monday at 6am: Insert
0 6 * * 1 comando
in the crontab. The command will run every Monday at 6:00. - Run a script every 15 minutes: Insert
*/15 * * * * /percorso/allo/script.sh
in the crontab. The script will run every 15 minutes. - Run a command every hour: Insert
0 * * * * comando
in the crontab. The command will run every hour. - Run a command every half hour: Insert
*/30 * * * * comando
in the crontab. The command will run every half hour. - Run a script every two hours: Insert
0 */2 * * * /percorso/allo/script.sh
in the crontab. The script will run every two hours. - Run a command every day at 8 in the morning and 8 in the evening: Insert
0 8,20 * * * comando
in the crontab. The command will run every day at 8:00 and 20:00. - Run a script on the first Monday of every month: Insert
0 0 * * 1 [ "$(date +\%m)" != "$(date +\%m -d '1 week')" ] && /percorso/allo/script.sh
in the crontab. The script will run on the first Monday of every month. - Run a command at 10.30am every day: Insert
30 10 * * * comando
in the crontab. The command will run every day at 10:30. - Run a script every 5 minutes during business hours (9-17): Insert
*/5 9-17 * * * /percorso/allo/script.sh
in the crontab. The script will run every 5 minutes during business hours. - Run one command every year: Insert
0 0 1 1 * comando
in the crontab. The command will be executed once a year, precisely at 00:00 on January 1st. - Run a script every 10 minutes on weekends: Insert
*/10 * * * 6,7 /percorso/allo/script.sh
in the crontab. The script will run every 10 minutes during Saturday and Sunday. - Run a command every quarter hour Monday through Friday: Insert
*/15 * * * 1-5 comando
in the crontab. The command will run every 15 minutes from Monday to Friday. - Run a script at 3pm on the first day of spring (March 21): Insert
0 15 21 3 * /percorso/allo/script.sh
in the crontab. The script will run at 15pm on March 00st. - Run a command every 5 hours: Insert
0 */5 * * * comando
in the crontab. The command will run every 5 hours. - Run a script every day at 2pm, except weekends: Insert
0 14 * * 1-5 /percorso/allo/script.sh
in the crontab. The script will run at 14pm Monday to Friday. - Run a command every day at 22pm: Insert
30 22 * * * comando
in the crontab. The command will run every day at 22:30. - Run a script on the 15th of every month: Insert
0 0 15 * * /percorso/allo/script.sh
in the crontab. The script will run on the 15th of every month at 00:00.
Remember, cron is a powerful tool, but with great power also comes great responsibility. It is important to always double-check cron job settings to avoid potential problems.
Cron to get URLs via curl or wget
If you are a webmaster or manage a web server, you may need to perform scheduled tasks by calling up specific URLs. This can be particularly useful for automating processes such as polling data from an API, triggering maintenance operations, triggering backups or cleaning up databases. The most common way to achieve this is to use the `curl` or `wget` utilities.
`Curl` and `wget` are two very popular command line commands in Unix-like systems, used for transferring data to or from a server using various network protocols, including HTTP, HTTPS, FTP and many more. While `curl` is more powerful and flexible, `wget` is known for its robustness and ease of use, especially when it comes to recursive downloads.
Here are five examples of cron jobs they use curl
o wget
:
- Call up a URL every day at 6am with curl: Insert
0 6 * * * curl http://miosito.com/operazione
in the crontab. This cron job will call the specified URL every day at 6:00. - Download a file every night at 2 with wget: Insert
0 2 * * * wget -O /percorso/al/file http://miosito.com/file.zip
in the crontab. This cron job will download the specified file and save it in the specified location every night at 2:00. - Call a service's API every hour with curl: Insert
0 * * * * curl https://api.mioservizio.com/update
in the crontab. This cron job will call the specified API every hour. - Call WordPress wp-cron.php every 15 minutes with wget: Insert
*/15 * * * * wget -q -O - http://miosito.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
in the crontab. This cron job will call your WordPress site's wp-cron.php file every 15 minutes. The output will be ignored to avoid filling the logs with useless information. - Download a list of files every day at 5pm with wget: Insert
0 17 * * * wget -i http://miosito.com/lista-di-file.txt
in the crontab. This cron job will download a list of files from a specified text file every day at 17pm.
Remember it is curl
is wget
they have many options and features; therefore, it may be useful to consult the related man pages to find out how best to customize your cron jobs.