Linux

systemd サービスマネージャー

ユニットファイルの作成
vi /etc/systemd/system/folder_watcher.service
[Unit]
Description=Folder Watcher Service
After=network.target

[Service]
ExecStart=/opt/inotify/folder_watcher.sh
ExecStop=/bin/bash -c 'pkill -f /opt/inotify/folder_watcher.sh'
Restart=always
User=root

[Install]
WantedBy=multi-user.target

Wants=network.target を有効にするとこのサービスが起動する前にネットワークが必須であることを条件として加える事ができる。

systemd デーモンのリロード
sudo systemctl daemon-reload
サービスの有効化と起動

enableとする事で、システム起動時に自動起動されます。

sudo systemctl enable folder_watcher.service
--
-- 具体的に自動起動を設定する
[sooni@vm022 ~]$ sudo systemctl enable folder_watcher.service
Created symlink /etc/systemd/system/multi-user.target.wants/folder_watcher.service → /etc/systemd/system/folder_watcher.service.
--
-- 念のために確認する
[sooni@vm022 ~]$ sudo systemctl is-enabled folder_watcher.service
enabled
[sooni@vm022 ~]$
--
-- 自動起動を取りやめるには
[sooni@vm022 ~]$ sudo systemctl disable folder_watcher.service
Removed /etc/systemd/system/multi-user.target.wants/folder_watcher.service.
[sooni@vm022 ~]$ sudo systemctl is-enabled folder_watcher.service
disabled
[sooni@vm022 ~]$
--
-- サービスへの登録から削除するには
[sooni@vm022 ~]$ sudo systemctl disable folder_watcher.service
-- ユニットファイルも削除する
[sooni@vm022 ~]$ sudo rm /etc/systemd/system/folder_watcher.service
[sooni@vm022 ~]$
sudo systemctl start folder_watcher.service
[sooni@vm022 ~]$ sudo systemctl start folder_watcher.service
[sooni@vm022 ~]$ sudo systemctl status folder_watcher.service
● folder_watcher.service - Folder Watcher Service
   Loaded: loaded (/etc/systemd/system/folder_watcher.service; enabled; vendor preset: disabled)
   Active: activating (auto-restart) (Result: exit-code) since Sun 2024-07-28 00:26:50 JST; 2s ago
  Process: 87029 ExecStart=/opt/inotify/folder_watcher.sh (code=exited, status=203/EXEC)
 Main PID: 87029 (code=exited, status=203/EXEC)

 7月 28 00:26:50 vm022 systemd[1]: folder_watcher.service: Main process exited, code=exited, status=203/EXEC
 7月 28 00:26:50 vm022 systemd[1]: folder_watcher.service: Failed with result 'exit-code'.
[sooni@vm022 ~]$
-- なぜかエラー、、、
-- いろいろ調べた結果、SELinuxのポリシー違反に引っかかっていた模様、、
-- 仕方ないのでいったん、一時的にSELinuxによる制限を一時的に無効化する
-- 
[sooni@vm022 ~]$ sudo systemctl status folder_watcher.service
● folder_watcher.service - Folder Watcher Service
   Loaded: loaded (/etc/systemd/system/folder_watcher.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2024-07-28 00:40:07 JST; 2min 33s ago
 Main PID: 88333 (folder_watcher.)
    Tasks: 10 (limit: 23559)
   Memory: 2.3M
   CGroup: /system.slice/folder_watcher.service
           tq88333 /bin/bash /opt/inotify/folder_watcher.sh
           tq88335 /bin/bash /opt/inotify/folder_watcher.sh
           tq88336 /bin/bash /opt/inotify/folder_watcher.sh
           tq88337 /bin/bash /opt/inotify/folder_watcher.sh
           tq88341 inotifywait -m -r -e modify,create,delete /usr/local/share/test/F02
           tq88342 /bin/bash /opt/inotify/folder_watcher.sh
           tq88343 inotifywait -m -r -e modify,create,delete /usr/local/share/test/F03
           tq88344 /bin/bash /opt/inotify/folder_watcher.sh
           tq88345 inotifywait -m -r -e modify,create,delete /usr/local/share/test/F01
           mq88346 /bin/bash /opt/inotify/folder_watcher.sh

 7月 28 00:40:07 vm022 systemd[1]: Started Folder Watcher Service.
 7月 28 00:40:07 vm022 folder_watcher.sh[88333]: /opt/inotify/folder_watcher.sh: 行 32: /bin: ディレクトリです
 7月 28 00:40:07 vm022 folder_watcher.sh[88333]: /opt/inotify/folder_watcher.sh: 行 32: /bin: ディレクトリです
 7月 28 00:40:07 vm022 folder_watcher.sh[88333]: /opt/inotify/folder_watcher.sh: 行 32: /bin: ディレクトリです
 7月 28 00:40:07 vm022 folder_watcher.sh[88333]: Setting up watches.  Beware: since -r was given, this may take a while!
 7月 28 00:40:07 vm022 folder_watcher.sh[88333]: Watches established.
 7月 28 00:40:07 vm022 folder_watcher.sh[88333]: Setting up watches.  Beware: since -r was given, this may take a while!
 7月 28 00:40:07 vm022 folder_watcher.sh[88333]: Watches established.
 7月 28 00:40:07 vm022 folder_watcher.sh[88333]: Setting up watches.  Beware: since -r was given, this may take a while!
 7月 28 00:40:07 vm022 folder_watcher.sh[88333]: Watches established.
[sooni@vm022 ~]$
フォルダを監視するスクリプト(サービスへ登録)
#!/bin/bash

# ログファイルのパスを指定
LOGFILE="/var/log/folder_watcher.log"

# ソースディレクトリと対応するデスティネーションディレクトリを定義
#SOURCE_BASE="/usr/local/share/test"
SOURCE_BASE="/home/sooni/ftpdir"
DEST_BASE="/usr/local/share/work"

# フォルダごとの配列を定義
FOLDERS=("F01" "F02" "F03")

# ログに初期メッセージを追加
echo "$(date '+%Y-%m-%d %H:%M:%S') - folder_watcher started!" >> $LOGFILE

# トラップ設定
trap 'pkill -P $$; exit' EXIT

# フォルダごとに独立して監視プロセスを開始
for FOLDER in "${FOLDERS[@]}"; do
    SOURCE_DIR="$SOURCE_BASE/$FOLDER"
    DEST_DIR="$DEST_BASE/$FOLDER"

    # コピー先のディレクトリが存在しない場合は作成
    if [ ! -d "$DEST_DIR" ]; then
        mkdir -p "$DEST_DIR"
        echo "$(date '+%Y-%m-%d %H:%M:%S') - Created destination directory: $DEST_DIR" >> $LOGFILE
    fi

    # フォルダの監視とファイルのコピー/削除
    inotifywait -m -r -e modify,create,delete "$SOURCE_DIR" --format '%w%f %e' |
    while read full_path event; do
        if [[ "$event" == "CREATE" || "$event" == "MODIFY" ]]; then
            DEST_PATH="$DEST_DIR/$(basename "$full_path")"
            if [ -f "$full_path" ]; then
                cp "$full_path" "$DEST_PATH"
                echo "$(date '+%Y-%m-%d %H:%M:%S') - $event - Copied $full_path to $DEST_PATH" >> $LOGFILE
            fi
        elif [[ "$event" == "DELETE" ]]; then
            DEST_PATH="$DEST_DIR/$(basename "$full_path")"
            if [ -f "$DEST_PATH" ]; then
                rm "$DEST_PATH"
                echo "$(date '+%Y-%m-%d %H:%M:%S') - $event - Deleted $DEST_PATH" >> $LOGFILE
            fi
        fi
    done &

done

# スクリプトが終了しないように待機
wait
スポンサーリンク