May 22, 2023

How to set up a cron on a Linux server via SSH

What is a Scheduled Task on Linux and how to set up a cron via crontab ?

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:

  1. 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.
  2. now: This field can assume values ​​from 0 to 23 and indicates the hour of the day in which the cron job must be executed.
  3. 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.
  4. 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.
  5. 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.
  6. 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:

  1. 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.
  2. 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.
  3. 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.
  4. 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, then Y to confirm saving and finally Enter to confirm the file name.

Examples of Chron

To help you better understand how to set up crons, here are some practical examples:

  1. Run a command every minute: To do that, you would enter * * * * * comando in the crontab. The command will run every minute.
  2. 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.
  3. 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.
  4. Run a command every Monday at 6am: Insert 0 6 * * 1 comando in the crontab. The command will run every Monday at 6:00.
  5. Run a script every 15 minutes: Insert */15 * * * * /percorso/allo/script.sh in the crontab. The script will run every 15 minutes.
  6. Run a command every hour: Insert 0 * * * * comando in the crontab. The command will run every hour.
  7. Run a command every half hour: Insert */30 * * * * comando in the crontab. The command will run every half hour.
  8. Run a script every two hours: Insert 0 */2 * * * /percorso/allo/script.sh in the crontab. The script will run every two hours.
  9. 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.
  10. 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.
  11. Run a command at 10.30am every day: Insert 30 10 * * * comando in the crontab. The command will run every day at 10:30.
  12. 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.
  13. 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.
  14. 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.
  15. 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.
  16. 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.
  17. Run a command every 5 hours: Insert 0 */5 * * * comando in the crontab. The command will run every 5 hours.
  18. 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.
  19. Run a command every day at 22pm: Insert 30 22 * * * comando in the crontab. The command will run every day at 22:30.
  20. 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 vs wget

`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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

 

Do you have doubts? Don't know where to start? Contact us!

We have all the answers to your questions to help you make the right choice.

Chat with us

Chat directly with our presales support.

0256569681

Contact us by phone during office hours 9:30 - 19:30

Contact us online

Open a request directly in the contact area.

INFORMATION

Managed Server Srl is a leading Italian player in providing advanced GNU/Linux system solutions oriented towards high performance. With a low-cost and predictable subscription model, we ensure that our customers have access to advanced technologies in hosting, dedicated servers and cloud services. In addition to this, we offer systems consultancy on Linux systems and specialized maintenance in DBMS, IT Security, Cloud and much more. We stand out for our expertise in hosting leading Open Source CMS such as WordPress, WooCommerce, Drupal, Prestashop, Joomla, OpenCart and Magento, supported by a high-level support and consultancy service suitable for Public Administration, SMEs and any size.

Red Hat, Inc. owns the rights to Red Hat®, RHEL®, RedHat Linux®, and CentOS®; AlmaLinux™ is a trademark of AlmaLinux OS Foundation; Rocky Linux® is a registered trademark of the Rocky Linux Foundation; SUSE® is a registered trademark of SUSE LLC; Canonical Ltd. owns the rights to Ubuntu®; Software in the Public Interest, Inc. holds the rights to Debian®; Linus Torvalds owns the rights to Linux®; FreeBSD® is a registered trademark of The FreeBSD Foundation; NetBSD® is a registered trademark of The NetBSD Foundation; OpenBSD® is a registered trademark of Theo de Raadt. Oracle Corporation owns the rights to Oracle®, MySQL®, and MyRocks®; Percona® is a registered trademark of Percona LLC; MariaDB® is a registered trademark of MariaDB Corporation Ab; REDIS® is a registered trademark of Redis Labs Ltd. F5 Networks, Inc. owns the rights to NGINX® and NGINX Plus®; Varnish® is a registered trademark of Varnish Software AB. Adobe Inc. holds the rights to Magento®; PrestaShop® is a registered trademark of PrestaShop SA; OpenCart® is a registered trademark of OpenCart Limited. Automattic Inc. owns the rights to WordPress®, WooCommerce®, and JetPack®; Open Source Matters, Inc. owns the rights to Joomla®; Dries Buytaert holds the rights to Drupal®. Amazon Web Services, Inc. holds the rights to AWS®; Google LLC holds the rights to Google Cloud™ and Chrome™; Facebook, Inc. owns the rights to Facebook®; Microsoft Corporation holds the rights to Microsoft®, Azure®, and Internet Explorer®; Mozilla Foundation owns the rights to Firefox®. Apache® is a registered trademark of The Apache Software Foundation; PHP® is a registered trademark of the PHP Group. CloudFlare® is a registered trademark of Cloudflare, Inc.; NETSCOUT® is a registered trademark of NETSCOUT Systems Inc.; ElasticSearch®, LogStash®, and Kibana® are registered trademarks of Elastic NV This site is not affiliated, sponsored, or otherwise associated with any of the entities mentioned above and does not represent any of these entities in any way. All rights to the brands and product names mentioned are the property of their respective copyright holders. Any other trademarks mentioned belong to their registrants. MANAGED SERVER® is a registered trademark at European level by MANAGED SERVER SRL Via Enzo Ferrari, 9 62012 Civitanova Marche (MC) Italy.

Back to top