Linux: оповещение пользователя об изменении файла

3056
Sheldon Ross

Вот более общий вопрос использования.

Как я могу отслеживать файл журнала на предмет определенных изменений и использовать оповещение «уведомить-отправить» в Ubuntu, чтобы предупредить, когда произойдут указанные изменения?

ОРИГИНАЛЬНЫЙ ВОПРОС:

Я пытаюсь установить квоты на печать для принтеров здесь, в офисе. Тем не менее, он появляется, когда квота достигнута, печать просто молча терпит неудачу, и пользователь не имеет ни малейшего представления, что происходит.

Квота страницы устанавливается путем изменения директивы PageLimit в /etc/cups/printers.conf

Похоже, я получаю сообщение

E [04 / Mar / 2013: 15: 34: 28 -0700] Возврат IPP-ошибки клиента невозможен для Create-Job (ipp: // localhost: 631 / printers / Hewlett-Packard-HP-LaserJet-4100- МФУ) от localhost

в моем журнале в / var / log / cups / error_log.

Я пытался взломать что-то вместе с помощью команды, как

`tail -f /var/log/cups/error_log | grep 'client-error-not-possible for Create-Job' DISPLAY=:0 notify-send -t 30000 -i 'notification-message-email' 'Printing Quota reached for this Printer'` 

Но, похоже, это не сработает, так как в первом сообщении об ошибке отображается сообщение только один раз.

Есть мысли или решения? Похоже, что для этого должно быть готовое решение.

РЕШЕНИЕ

incrontab работал отлично.

sudo apt-get install incrontab 

Добавить пользователя root в /etc/incron.allow

sudo nano /etc/incron.allow 

Создать скрипт monitorCUPSlog.sh

#!/bin/bash tail -n 1 /var/log/cups/error_log | grep 'client-error-not-possible' | DISPLAY=:0 notify-send -t 30000 -i 'notification-message-email' 'Daily Print Quota exceeded for this printer' 

И, наконец, добавьте событие в таблицу incrontab.

sudo incrontab -e /var/log/cups/error_log IN_MODIFY /usr/local/bin/monitorCUPSlog.sh 

Кажется, работает, ура.

4

2 ответа на вопрос

5
Semafoor

I would look into using incrontab for monitoring changes to the file system, and combine that with the little script you already have.

This looks approximately as follows.

First, save the script that you want to execute as a file, e.g. as cups_monitor.sh in /usr/local/bin (don't forget to make it executable).

#!/bin/sh VAR=`tail -n 1 /var/log/cups/error_log | grep 'client-error-not-possible for Create-Job'` if [[ -n "$VAR" ]]; then echo "$VAR" | DISPLAY=:0 notify-send -t 30000 -i fi 

You might want to check this. My scripts are never right first try :). Also note that it is possible that this script does not capture your error message if it is followed by other message: I only look at the last line of the log file (-n 1); this should be easy to change.

Then edit your incrontab

incrontab -e 

by adding the line

/var/log/cups/error_log IN_CLOSE_WRITE /usr/local/bin/cups_monitor.sh 

The IN_CLOSE_WRITE is called an 'event symbol' and indicates that you want to want to call your script when/var/log/cups/error_log was closed after it was opened for writing; you can find other events in the man page of incrontab.

Keep in mind that I did not test this. You can see whether the incrontab file was changed successfully and whether it calls your script or not by looking in the syslog (tail /var/log/syslog).

(My first answer on Stack Exchange ever! Yippee!)

0
amphibient

You can use incron :: inotify to attach customized triggers on files that do things like make log file entries or send emails upon change events.

Похожие вопросы