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.
$ 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"} ▍
- Recursivewatching of the folder and every subfolder
- 1JSON line per file event, with type, path and time
- systemdservice that restarts itself if it crashes
- 40lines of Python, a single dependency
How it works
One script, one systemd unit.
-
Watch
file_watcher.pystarts watchdog'sObserveron the watched folder withrecursive=True, so subfolders are covered too. -
Skip folders
Every event goes through one handler. Events for directories are skipped; only file events are logged.
-
Write a JSON line
Each event is appended to the log file as one JSON object with
event_type,src_pathandtimestamp, so the file can be read line by line with tools likejq. -
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.
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}")
[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.
-
Set the paths
PythonLOG_FILE = "/home/ubuntu/bsm/logs/changes.json" WATCH_DIR = "/home/ubuntu/bsm/test" -
Try it in the foreground
Shellpip install watchdog mkdir -p /home/ubuntu/bsm/logs /home/ubuntu/bsm/test python3 file_watcher.py
-
Install it as a service
Edit
User,Groupand theExecStartpath infile_watcher.servicefor your system; the service user needs write access to the log folder.Shellsudo cp file_watcher.service /etc/systemd/system/ sudo systemctl daemon-reload sudo systemctl enable --now file_watcher journalctl -u file_watcher -f
Known limitations
- The paths are hard-coded; there's no config file or command-line option.
- Move events record only the source path, not the destination.
- The log file isn't rotated, so it keeps growing.