How to use cron and crontab in linux
Table of contents:
It is very likely that everyone has listened to or used at some point the scheduled Windows tasks, which are nothing more than the configuration for a file, program or process to run in a certain time and under conditions specified by us users. This same concept applies to Linux, however, the process is not carried out through the graphical environment, for this we use the terminal. For this reason today we bring to you the explanation of cron and crontab that are essential commands to carry out these tasks.
Cron and Crontab
Index of contents
What is Cron?
Its name comes from the Greek expression chronos and its meaning is time. It is one of the most important and common demons or “daemon” (background process) in the system. Its execution begins from the first instant of startup.
Its main function is to take care of launching scheduled tasks on specific dates and automatically and repetitively. The definition of the tasks is located in the / etc / crontab file. The operation is simple, check if there are tasks (jobs) to execute according to the system schedule. This leads us to emphasize that it is important to keep the system time zone correctly adjusted, otherwise we will not obtain the expected results with cron.
According to the distribution that we are using in Linux, it can be initialized using the /etc/init.d or etc / rc.d / directories and every minute it checks the / etc / crontab or / var / spool / cron locating possible pending executions.
What is Crontab?
It is as simple as a text file. Yes, even if it doesn't seem like it. What makes it special is its content. Its content specifies a list of all the scripts to be executed by the system. As well as specifying the dates, times and the permissions to execute them.
In Linux, each user generally has their own crontab file and the one located in the etc directory is owned by the root user.
To generate your own file (in case you are not a root user) you just use the command:
crontab
Crontab is the simplest way to manage cron tasks on multi-user systems, either as root user or simple system user.
Next, I leave you a little help regarding the definition of the cron within the crontab, with a sample example. (Do you have a # in front so that you can put it at the beginning of your crontab file as a comment and will you always have it at hand?
# Example definition of cron: #.---------------- minutes (0 - 59) # |.------------- hours (0 - 23) # | |.---------- days of the month (1 - 31) # | | |.------- months (1 - 12) O jan, feb, mar, apr… # | | | |.---- days of the week (0 - 6) (Sunday = 0 or 7) # | | | | | # * * * * * USER COMMAND MAILTO = "cron @ localhost" SHELL = / bin / sh
Where each asterisk represents a fraction of time that determines the moment of execution, followed by the user under which the execution will be carried out (that user can be root or one with assigned execution permissions) and finally the command to execute.
Cron job administration
As you have already noticed, the operation is very simple. To close the topic, I will show you the basic and essential commands to control and verify the cron of our Linux system.
WE RECOMMEND YOU Canonical officially announces Ubuntu 17.04 `` Zesty Zapus ''If what you want is to replace the existing file with one defined by another that you choose, use the following command:
crontab (file-name)
If we want or require to modify the current file we use the following:
crontab -e
To get the list of all the tasks that are configured in crontab, we make use of:
crontab -l
To remove the current crontab from the system, we have:
crontab -d
You may be interested to read: Quick guide to basic commands for Ubuntu and Linux
Another operation that we can perform is to define the directory where the crontab will be stored. It is important to clarify that we must have execution permissions in the chosen directory, otherwise it will not work. The command to define it is as follows:
crontab -c dir
And finally, we can manage the existing crontab of other users belonging to the system.
crontab -u user
As you have seen, it is not complicated at all to schedule tasks in Linux and with all the clear points it can be done quickly.
I hope you found the article helpful. You can leave your concerns or any good practice you have for programming your cron in Linux in the comments! ?
Ping command: use and operation in linux
In the next post we will show you the Use and operation of the ping command in Linux. Essential tool to diagnose network errors.
Ssd m.2: what is it, use, pros and cons and recommended models
Today we bring you everything you need to know about M.2 SSDs, the fastest storage units are the future, we must know them
Playstation 5 and xbox series x will use rdna 2 and ray tracing
AMD hinted at the financial analyst conference that both PlayStation 5 and Xbox Series X will be equipped with RDNA2 architecture.