overview edit doc new doc remove

Jan 26, 2018

Cron and Inotify

Cron

Crond is a daemon to execute scheduled commands, cron searches in /var/spool/cron for crontab files which are named after users from /etc/passwd. Cron also looks at /etc/anacrontab and any files in /etc/cron.d. The crontab command is linked to /var/spool/cron/user.

Add cronjobs by:

crontab -e

Crontab

Examples of crontab (tabs are not needed):

# template
#  .---------------- minute (0 - 59)
#  |  .------------- hour (0 - 23)
#  |  |  .---------- day of month (1 - 31)
#  |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
#  |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
#  |  |  |  |  |
   *  *  *  *  * user-name  command-to-be-executed

# run every day at 12:30
30 0 * * * root find /tmp -type f -empty -delete

# run every Sunday at 20:00
0 20 * * 6 dnf update -y

# run every minute
* * * * * echo `date` > /home/test

Special crontab

@daily command
@weekly command
@monthly command
@reboot command

# log command output to an external file (at 7h30)
30 7 * * * apt-get update && apt-get upgrade -y | while IFS= read -r line;
do echo "$(date) $line"; done >> /data/log1.log 2>&1

Inotify

The inotify cron daemon provides a mechanism for monitoring file system events. Inotify can be used to monitor individual files, or to monitor directories. When a directory is monitored, inotify will return events for the directory itself and for files inside the directory. To activate Inotify you need to start the incrond.service. Every activity from incrond is logged (journal, messages). if you got an error like a user is not allowed to use incron you should add them to /etc/incron.allow.

Add incronjobs by:

incrontab -e

Incrontab

Examples of incrontab:

# template
<directory> <file change mask> <command or action>  options

# example
/var/www/html IN_CREATE /root/scripts/backup.sh

File change mask

Mask is nothing more then various file system events such as deleting a file. Each event can result into command execution. Use the following masks:

Options

1