CodexBloom - Programming Q&A Platform

Ubuntu 22.04 - Systemd service scenarios with 'Bad unit' scenarios for custom script

👀 Views: 37 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-08
systemd ubuntu python Python

I'm following best practices but I've been banging my head against this for hours... I'm trying to create a systemd service for a custom Python script that processes files every hour, but I'm running into a 'Bad unit' behavior when I attempt to start the service. My service file is located at `/etc/systemd/system/my_script.service` and contains the following: ```ini [Unit] Description=My Custom Script [Service] ExecStart=/usr/bin/python3 /home/user/myscript.py WorkingDirectory=/home/user User=user Restart=on-failure [Install] WantedBy=multi-user.target ``` After creating the service file, I ran `sudo systemctl daemon-reload` to refresh systemd, but when I try to start the service using `sudo systemctl start my_script.service`, I get the following behavior: ``` Failed to start my_script.service: Bad unit file, or unit file has bad syntax. ``` I have checked that the script itself is executable and runs fine when executed directly using Python. I also verified that there are no extra spaces or errors in the service file, but I'm still getting this behavior. I tried running `sudo systemctl status my_script.service` to get more details, and it returns: ``` ● my_script.service Loaded: bad-unit (reason: No such file or directory) Active: inactive (dead) ``` Is there something I might be missing with the service file structure or syntax? Could it be a permissions scenario or perhaps a question with the path to the Python interpreter? Any guidance would be greatly appreciated. This is part of a larger CLI tool I'm building. I'd really appreciate any guidance on this. Is this even possible?