Using cronjobs, you can automate specific tasks, such as sending automated emails, automatic backups, clearing your server's cache, deleting temporary files, and, in short, anything you have in mind. Some practical examples of the use of cron jobs: When you receive an email at 7 AM advertising for a department store, when you receive a government payment, or when your cell phone alarm rings.
The use of cronjobs today is not a theoretical novelty, but a utility that has supported technological development for years and is important for understanding the algorithmic operation that occurs automatically.
How to install Cron Job on Linux?
To install this utility, you must run the following command on your Debian/Ubuntu machine:
apt install cron
The above command will install the utility, creating a file called crontab in the /etc/ folder (/etc/crontab).
How to edit Crontab in Linux?
To edit crontab in Debian/Ubuntu, you can use your favorite editor; in our case, we'll use nano.
nano /etc/crontab
Which will open a file with example syntax like the following:
SHELL=/bin/sh
PATH=/usr/local/sbin :/usr/local/bin:/sbin:/bin :/usr/sbin:/usr/bin
17 root cd / && run-parts --report /etc/cron.hourly
25 6 * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
Basic crontab / cronjob syntax
The basic syntax structure for running a crontab is as follows:
minute hour day_of_month month day_of_week command_to_execute
A basic example of using crontab to run a URL at 7 AM:
0 7 * curl https://mox.cl
In this case, at 7 AM, run the CURL function to call the website in question (the asterisk represents any number or, in this example, any day).
Another basic example, to reset the RAM of your Debian/Ubuntu virtual machine, could be:
0 free -m
Conclusions
Running a cronjob will undoubtedly make running any program easier. Safely automatable task (since it occurs within your VPS). You can run tasks using commands directly on your server or execute scripts you've created in another programming language. In addition to running them on your virtual server, you can run tasks externally using CURL or WGET.
When you use cPanel Hosting, you can manage these commands in a more user-friendly way using the Cron Jobs module available in cPanel. Or, if you use a VPS with cPanel or DirectAdmin, there are platforms with templates that will allow you to run these tasks.
When you use a CMS like Prestashop or WordPress, you can directly install a module that allows you to run these tasks automatically, such as generating and clearing caches.