Improve error handling in daemon script