📜 ⬆️ ⬇️

Making “life” in Linux easier or automating process startup using cron

Mega tambourine for nix admin
Introduction

Sitting in the evening at the laptop and picking the next Linux distribution on a virtual machine, I wondered: Is it possible to simplify the routine start of processes? If it is interesting to you, I ask under kat. The article is informative and calculated, more on newcomers to the Linux family of operating systems, but experienced Linux users will probably be able to learn something new for themselves.

Daemons atd and cron

A little googling and reading literature, I learned about two demons : atd and cron . I refused the first one in view of its limitations and inconvenience of working with it. But I want to talk about the second in more detail.
If your computer suddenly, as it seems, for no reason, starts searching the disk, sending 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 at one minute intervals, checks the crontab files and starts the programs specified in them. Initially, it is used in the course of maintaining the system, but the user can use it to solve their problems.

If you have installed a normal distribution, then you have nothing to worry about, cron is installed automatically. If the minimum, then not upset - 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 specified in the / cron / allow and / deny files. By adding a user to / allow, we allow him to execute the cron command, and if you add a user to / deny , then on the contrary, the user will not be allowed to use the daemon.
')
The cron itself is configured in the / etc / crontab directory. File / crontab or files in / etc / cron. d contain a list of commands to be executed. The syntax is:
 in /etc/crontab [][][][][][][] 

For example, if I need to ping ya.ru every 15 minutes on behalf of the superuser, then I need to add the following:
 */15 **** root ping ya.ru 

If the character * is in any of the first five fields, this field is ignored. In the previous team neither month nor 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.

Working with .hourly, .daily, .weekly, .monthly

By default, in almost all distributions, the / etc / crontab file contains only a few entries needed to run the scripts:
/etc/cron.hourly/* - scripts run every hour
/etc/cron.daily/* - scripts that run every day
/etc/cron.weekly/* - scripts running every week
/etc/cron.monthly/* - scripts that run every month

To allow the daemon to execute your commands, add a script that executes the commands in one of the directories. Remember to set the execute bit (chmod a + x file) . If you do not do this, then your script simply will 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 a different directory, then correspondingly change daily to monthly , etc.
And remember, in the name of the script can not be dots, any characters other than dots. The run-parts command simply ignores scripts with a period, I don’t know why.

Anacron

In addition to the cron daemon . most distros have an Anacron task scheduler installed. Its task is to execute the /etc/cron.n scripts once (on demand) where n can take three values: daily , weekly , monthly . After executing them, it quits, and does not hang in the system as cron . Also, Anacron does not execute scripts from the /etc/cron.hourly directory, it is the cron prerogative. Anacron's global configuration is made in the / etc / anacrontab directory , but there are usually enough default settings.

PS

To simplify working with everyday tasks of a ssh admin working with ssh, it is better to use cron and disable Anacron, since it performs the 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 the same, but if you have problems, use the wiki for your Linux.

Source: https://habr.com/ru/post/217655/


All Articles