1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Virtualbox CentOS7にincronをインストールしてみた

Last updated at Posted at 2024-11-01

はじめに

VirtualBoxですがCentOS7にてincronをインストールして動作させてみました。

CentOS7にてincronをインストールしてみた

リポジトリの設定は以下の記事の対応をしました。
https://qiita.com/gama1234/items/cee4d198229c48adb6e6
CentOS7はサポート終了したため、yum install incrondを実行してデフォルトのリポジトリからは
パッケージが存在しません。

epelのリポジトリからincronのパッケージはインストールできました。

incronのパッケージをダウンロードしました。

# yumdownloader --resolve --destdir=./ incron

-rw-r--r--  1 root root 98362  3月 21  2019 incron-0.5.12-11.el7.x86_64.rpm

その後、incronのパッケージをインストールしました。

# rpm -ivh incron-0.5.12-11.el7.x86_64.rpm
準備しています...              ################################# [100%]
更新中 / インストール中...
   1:incron-0.5.12-11.el7             ################################# [100%]
新しいメールが /var/spool/mail/root にあります
[root@localhost ~]#

incronを動作させてみた

以下の記事が参考になりました
https://qiita.com/k-suzuki/items/4a94ebeda9ec75fdad40

incrondのサービスのステータスを確認しました。

# systemctl status incrond
● incrond.service - Inotify System Scheduler
   Loaded: loaded (/usr/lib/systemd/system/incrond.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

incrondのサービスを起動しました。

# systemctl start incrond

incrondのサービスのステータスを確認しました。

systemctl status incrond
● incrond.service - Inotify System Scheduler
   Loaded: loaded (/usr/lib/systemd/system/incrond.service; disabled; vendor preset: disabled)
   Active: active (running) since 金 2024-11-01 21:47:06 JST; 4s ago
  Process: 1533 ExecStart=/usr/sbin/incrond (code=exited, status=0/SUCCESS)
 Main PID: 1534 (incrond)
   CGroup: /system.slice/incrond.service
           mq1534 /usr/sbin/incrond

11月 01 21:47:06 localhost.localdomain systemd[1]: Starting Inotify System Scheduler...
11月 01 21:47:06 localhost.localdomain systemd[1]: Can't open PID file /run/incrond.pid (yet?) after start: No such file or directory
11月 01 21:47:06 localhost.localdomain incrond[1534]: loading system tables
11月 01 21:47:06 localhost.localdomain incrond[1534]: loading user tables
11月 01 21:47:06 localhost.localdomain incrond[1534]: ready to process filesystem events
11月 01 21:47:06 localhost.localdomain systemd[1]: Started Inotify System Scheduler.

incrontabでジョブの登録コマンドを実行したらエラーになりました。

# incrontab -e
editor finished with error: No such file or directory 

以下のサイトが参考になりました。
https://hasen.hatenablog.com/entry/2015/09/15/210350

icronの設定ファイルをバックアップを取得しました。

# cp -p /etc/incron.conf /etc/incron.confbk

以下のincronの設定ファイルの行末にeditor = viを追加しました。

# vi /etc/incron.conf

editor = vi

incronのサービス再起動しました。

# systemctl restart incrond

監視対象のファイルを作成する。

# touch /root/file1

簡単なスクリプトを作成しました。

# cat /root/test.sh
#!/bin/bash

echo "file1 modify" >> /root/incron_output.log

ジョブを登録しました。

incrontab -e
[監視対象ファイル] [イベント] [実行するコマンド]
/root/file1 IN_MODIFY,IN_NO_LOOP sh /root/test.sh 

ジョブの反映しました。

# incrontab -d
requesting table reload for user 'root'...
request done

登録したジョブを確認しました。

# incrontab -l
/root/file1     IN_MODIFY       sh test.sh

以下のフォルダに登録したジョブの情報が格納されていることを確認した。

# cat /var/spool/incron/root
/root/file1     IN_MODIFY       sh test.sh

incronを動かしてみた

なぜかカレントディレクトリでパスを指定せずにfile1に対して文字を追記してもincronは動作しませんでした。

# cd /root 
# echo "文字列" >> file

以下の様に絶対パスでincrontab -eで指定したパス「/root/file1」で設定したら、incronが動作しました。

# echo "文字列" >> /root/file1

cronのログに/root/file1の編集イベントの後にsh test.shが実行されたログを確認しました。

# tail -n 2 /var/log/cron
Nov  1 22:15:32 localhost incrond[1670]: PATH (/root/file1) FILE () EVENT (IN_MODIFY)
Nov  1 22:15:32 localhost incrond[1670]: (root) CMD (sh test.sh)

スクリプトに設定したファイルも出力されました

# cat /root/incron_output.log
file1 modify

まとめ

incronはファイル変更を検知して設定したスクリプトを動作させるミドルウェアです
ログはcronのログに出力されていることを確認しました。
incron は監視対象のファイルは絶対パスを指定する必要があるため、相対パスでは動作しないことが検証した結果、分かりました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?