Cronjobs or scheduled tasks in Linux
What are cronjobs? How to use cronjob in VPS?
Scheduled tasks or cronjobs are process executions that happen automatically in the background. Generally speaking, a cronjob is a Linux utility that sets a command or command set at times or frequencies to perform specific tasks on any VPS.
Through cronjob you can automate specific tasks , such as automated email sending, automatic backups , clearing your server's cache, deleting temporary files, and ultimately whatever you have in mind. Some practical examples of the use of cron jobs: When you receive in your email at 7 AM advertising of a multi-store, when you receive a payment from the government or when the alarm of your mobile phone sounds.
The use of cronjobs today is not a theoretical novelty, but a utility that has supported technological development for years and is important to understand 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 will use nano
nano /etc /crontab
Which will open a file with sample 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 in executing crontab is as follows
minute hour day_of_month month day_of_the_week command_to_run
A basic example of using crontab, to run a URL at 7 AM
0 7 * * * curl https: //mox.cl
In which case, at 7AM you 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, it can be like this:
0 * * * * free -m
Without a doubt, the execution of cronjob will facilitate the operation of any task that can be automated safely (since it occurs within your VPS). You can run tasks from commands directly on your server or run scripts that you have made in another programming language. In addition to executions on your own virtual server, you will be able to execute tasks externally using CURL or WGET.
When you use a Hosting with cPanel, you will be able to manage these commands in a more friendly way through 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 carry out these tasks.
When you use a CMS such as Prestashop or Wordpress, you can directly install a module that allows you to use these tasks automatically, such as the generation and cleaning of caches.