incr

botond published Jan. 2018, 10, 03:19 p.m. time

Incron is an event monitoring system that consists of a daemon (incrond) and consists of a management program. Its usage is similar to that of the traditional cron system, except that Incron starts the specified programs, scripts, not in the timing cycles, but in response to events in the file system (write, read, access, etc.). Also different from cron is that in Incron, the rules are not set directly by editing a crontab file, but by a custom incrontab using a management program. Once placed in the Incron table, the appropriate event monitor row in the background monitors the specified file or directory and then runs the configured command when a change occurs.

 

Installation

Installation is very simple. To install as a root on Debian-based distributions:

apt-get install incron

And the successful output:

Csomaglisták olvasása... Kész
Függőségi fa építése
Állapotinformációk olvasása... Kész
Az alábbi ÚJ csomagok lesznek telepítve:
  incron
0 frissített, 1 újonnan telepített, 0 eltávolítandó és 101 nem frissített.
Letöltendő adatmennyiség: 68,8 kB.
A művelet után 321 kB lemezterület kerül felhasználásra.
Letöltés:1 http://ftp.hu.debian.org/debian/ jessie/main incron amd64 0.5.10-2 [68,8 kB]
Letöltve 68,8 kB 0mp alatt (874 kB/s)
A korábban ki nem választott incron csomag kiválasztása.
(Adatbázis olvasása ... 66120 fájl és könyvtár van jelenleg telepítve.)
Kibontás előkészítése: .../incron_0.5.10-2_amd64.deb ...
Kibontás: incron (0.5.10-2) ...
Aktiválók feldolgozása: systemd (215-17+deb8u7) ...
Aktiválók feldolgozása: man-db (2.7.0.2-5) ...
Beállítás: incron (0.5.10-2) ...
Adding group `incron' (GID 126) ...
Kész.
Aktiválók feldolgozása: systemd (215-17+deb8u7) ...

 

Commissioning

Once installed, you cannot use incrontab immediately because it is not allowed even for root. To-do list:

incrontab -l

First throw this bug:

user 'root' is not allowed to use incron

Therefore, you must first add users to the whitelist:

nano /etc/incron.allow

And add the root user or who you want to allow:

root

Then, when we run the list command again, we get another output:

incrontab -l
no table for root

So there is no Incron task for root yet.

 

Create an Incron job

To create new Incron tasks, use the following command:

incrontab -e

This will open the default editor, where you can edit tasks in a temporary file.

The format of the task sets is as follows:

<útvonal> <maszk> <parancs>
  • Route: The path can be a specific file or a directory. Note that Incron does not work recursively, so it only monitors files and directories directly below it in the configured directory. You no longer listen to additional files in subdirectories. These require the creation of additional Incron tasks.
  • Mask: The mask is an identifier that identifies events that have occurred.
  • Command: Here you have to specify the command you want to execute when the event occurs.

 

masks

Incron uses the following masks to detect these events during file system changes:

  • IN_ACCESS: The monitored file has been opened
  • IN_ATTRIB: Metadata changed (changes to permissions, timestamps, other attributes, etc.)
  • IN_CLOSE_WRITE: A file opened for writing has been closed
  • IN_CLOSE_NOWRITE: A file not opened for writing has been closed
  • IN_CREATE: A file or subdirectory has been created in a watched directory
  • IN_DELETE: A file or subdirectory in a watched directory has been deleted
  • IN_DELETE_SELF: A watched file or directory has been deleted
  • IN_MODIFY: A file has been modified in a watched directory
  • IN_MOVE_SELF: A watched file or directory has been moved
  • IN_MOVED_FROM: A file has been moved from the monitored directory
  • IN_MOVED_TO: A new file has been placed in the watched directory
  • IN_OPEN: A file has been opened in the watched directory

 

It is important that you do not create infinite loops when creating tasks, as they can cause heavy system loads or even crashes!
Example: If, for example, an Incron task monitors the / tmp directory, and when it changes, the executed program makes changes to the same directory (write, read, delete, create, etc.), then the task takes an infinite loop, because the change causes another program call Incron. This puts a heavy load on the system.