All projects

Systems · Linux

Linux File System Monitoring Service

A small Python service that watches a folder on Linux, subfolders included, and writes every file change, such as a file being created, modified, deleted or moved, to a log file as one line of JSON. It's installed as a systemd service, so it keeps running in the background.

  • Python
  • watchdog
  • systemd
  • JSON Lines
  • Linux
$ tail -f /home/ubuntu/bsm/logs/changes.json
{"event_type": "created", "src_path": "/home/ubuntu/bsm/test/rapor.txt", "timestamp": "2024-12-20 14:02:10"}
{"event_type": "modified", "src_path": "/home/ubuntu/bsm/test/rapor.txt", "timestamp": "2024-12-20 14:02:10"}
{"event_type": "modified", "src_path": "/home/ubuntu/bsm/test/notlar.txt", "timestamp": "2024-12-20 14:03:51"}
{"event_type": "moved", "src_path": "/home/ubuntu/bsm/test/taslak.txt", "timestamp": "2024-12-20 14:04:37"}
{"event_type": "deleted", "src_path": "/home/ubuntu/bsm/test/eski/liste.csv", "timestamp": "2024-12-20 14:05:02"}
▍
Sample output in the format the service writes.

How it works

One script, one systemd unit.

  1. Watch

    file_watcher.py starts watchdog's Observer on the watched folder with recursive=True, so subfolders are covered too.

  2. Skip folders

    Every event goes through one handler. Events for directories are skipped; only file events are logged.

  3. Write a JSON line

    Each event is appended to the log file as one JSON object with event_type, src_path and timestamp, so the file can be read line by line with tools like jq.

  4. Show it in the journal

    The same entry is printed to standard output, so when it runs as a service it also shows up in journalctl.

file_watcher.py
class ChangeHandler(FileSystemEventHandler):
    def on_any_event(self, event):
        if event.is_directory:
            return
        log_entry = {
            "event_type": event.event_type,
            "src_path": event.src_path,
            "timestamp": time.strftime('%Y-%m-%d %H:%M:%S')
        }
        with open(LOG_FILE, "a") as log_file:
            log_file.write(json.dumps(log_entry) + "\n")
        print(f"Logged: {log_entry}")
file_watcher.service
[Unit]
Description=File Watcher Service
After=network.target

[Service]
ExecStart=/usr/bin/python3 /home/ubuntu/bsm/file_watcher.py
Restart=always
User=yigit
Group=yigit

[Install]
WantedBy=multi-user.target

Setup

The watched folder and the log file are the two constants at the top of file_watcher.py.

  1. Set the paths

    Python
    LOG_FILE = "/home/ubuntu/bsm/logs/changes.json"
    WATCH_DIR = "/home/ubuntu/bsm/test"
  2. Try it in the foreground

    Shell
    pip install watchdog
    mkdir -p /home/ubuntu/bsm/logs /home/ubuntu/bsm/test
    python3 file_watcher.py
  1. Install it as a service

    Edit User, Group and the ExecStart path in file_watcher.service for your system; the service user needs write access to the log folder.

    Shell
    sudo cp file_watcher.service /etc/systemd/system/
    sudo systemctl daemon-reload
    sudo systemctl enable --now file_watcher
    journalctl -u file_watcher -f

Known limitations

More projects