ROBUST Summary I've successfully added comprehensive error tracking and logging to your BatteryMon script. Here's what was implemented: ✅ Added Error Tracking Features: Comprehensive Logging System Logs to /var/www/html/BatteryMon_errors.log Captures startup, errors, and termination reasons with timestamps Signal Handlers Catches SIGTERM, SIGINT, SIGHUP signals Saves data before graceful exit when terminated by system Exception Handling Main loop: Handles temporary errors without crashing Serial communication: Detects Arduino disconnection File operations: Handles permission/disk space issues Data parsing: Recovers from corrupted Arduino data Atomic File Writing Modified updateStatusfile() to write to temp file first, then rename Solves your Ansible concurrency issue - prevents reading partial files Enhanced Error Recovery Script continues running on recoverable errors Only exits on fatal errors (with logging) ✅ Benefits for Your Use Case: No more mystery crashes - Every termination is logged with detailed reason Ansible-safe file access - No more corrupted reads during writes Better reliability - Handles temporary issues without full restart Historical tracking - Build patterns of when/why crashes occur 📋 To Monitor Terminations: Check the log file /var/www/html/BatteryMon_errors.log for: "Script terminating:" - Shows exit reason "Serial communication error:" - Arduino issues "Permission denied:" - File access problems "Fatal error during initialization:" - Startup failures The script is now much more robust and will tell you exactly why it terminates, plus it's safer for concurrent access by Ansible.