
Making life easier on Linux or automating process startup with cron

Introduction
Sitting in the evening at a laptop and
Atd and cron daemons
A little googling and reading literature, I learned about two demons : atd and cron . I refused the first in view of its limitations and inconvenience of working with him. But I want to talk about the second in more detail.
If your computer, suddenly, as it seems, for no reason, starts to search the disk, send you mail, etc., then most likely this is the work of the cron daemon ...
Michael Kofler “ Linux. Installation, configuration, administration. ”- SPb .: Peter, 2014
So, what exactly does this very cron do . The daemon is activated with an interval of one minute, checks the crontab files and runs the programs specified in them. Initially, it is used during the maintenance of the system, but the user can use it to solve their problems.
If you have a regular distribution installed, then you have nothing to worry about, cron is installed automatically. If the minimum, then we are not upset - we go to the terminal.
yum install vixie-cron //(RHEL - Red Hat Enterprise Linux)
or
apt-get install cron //(Deabian-подобные дистрибутивы)
User access to the daemon is configured in the / var / spool / cron / tabs / user directory . Their rights are set in the / cron / allow and / deny files . By adding the user to / allow, we allow him to execute the cron command , and if you add the user to / deny , on the contrary, the user will be prohibited from using the daemon. Cron
itself is configured in the / etc / crontab directory . The file / crontab or files in / etc / cron. d contain a list of commands to be executed. The syntax is as follows:
in /etc/crontab
[минута][час][день][месяц][неделя][пользователь][команда]
For example, if I need to ping ya.ru every 15 minutes on behalf of a supervisor, then I need to add the following:
*/15 **** root ping ya.ru
If the symbol * is in any of the first five fields, then this field is ignored. In the previous command, neither a month nor a week is indicated, therefore, it will be executed every 15 minutes. To change the configuration, use the vi command in the terminal or manually change the contents of the / etc / crontab file .
Work with .hourly, .daily, .weekly, .monthly
By default, in almost all distributions, the / etc / crontab file contains only a few entries necessary to run scripts:
/etc/cron.hourly/* - scripts that run every hour
/etc/cron.daily/* - scripts that run every day
/ etc /cron.weekly/* - scripts running every week
/etc/cron.monthly/* - scripts running every month
For the daemon to execute your commands, add the script executing the commands to one of the directories. Remember to set the execute bit (chmod a + x file) . If you do not, then your script will simply not have access and it will not be executed!
To check whether your script will run, run the command
run-parts --test /etc/cron.daily
If the script is located in another directory, then change daily to monthly , etc.
And remember, there can be no dots in the script name, any characters except dots. The run-parts command simply ignores dotted scripts, I don’t know why.
Anacron
Besides the cron daemon . most distributions have Anacron task scheduler installed. Its task is a one-time (on demand) execution of the scripts /etc/cron.n where n can take three values: daily , weekly , monthly . After their execution, it exits, and does not hang on the system like cron . Also, Anacron does not execute scripts from the /etc/cron.hourly directory , this is the prerogative of cron . Anacron is configured globally in the / etc / anacrontab directory , but default settings are usually enough.
PS
To simplify the work with the daily tasks of the ssh admin, it is better to use cron and disable Anacron, since it performs tasks once, and cron ignores the tasks that Anacron performs. As a result, all the tasks you have will be performed only once. In most distributions, working with the daemon is almost no different, but if you encounter problems, use the wiki for your Linux.