Search code examples
pythonlinuxbotssystemdvps

Systemd says my service is active and started, but I receive no output


I'm trying to run a bot on a VPS and im able to get a systemd service create so as to be able to run my python code automatically if the server were to ever reboot for any reason. The service is enabled, the status is showing as active when I check its status, and journalctl shows that the .py file has started, but that's where my progress ends. I receive no other output after the notification that the service has started. And when I check my VPS console there is 0 CPU usage meaning that the script is in fact not running.

The script is located at /home/user/projects/ytbot1/bot/main.py and runs perfectly fine when executed manually through python3 main.py.

both the script and the .service file were given u+x permissions to the root and user, and the service is set to run only when the user is logged in (I think,... all I did was set User=myusername in ytbot1.service)

[Unit]
Description=reiss YT Bot

[Service]
User=reiss
Group=reiss
Type=exec
ExecStart=/usr/bin/python3 "/home/reiss/projects/ytbot1/bot/main.py"
Restart=always
RestartSec=5
PrivateTmp=true
TimeoutSec=900


[Install]
WantedBy=multi-user.target

here's the output from sudo systemctl status ytbot1

● ytbot1.service - reiss YT Bot
     Loaded: loaded (/etc/systemd/system/ytbot1.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-05-16 10:34:04 CEST; 9s ago
   Main PID: 7684 (python3)
      Tasks: 1 (limit: 19141)
     Memory: 98.4M
     CGroup: /system.slice/ytbot1.service
             └─7684 /usr/bin/python3 /home/reiss/projects/ytbot1/bot/main.py

and sudo journalctl -fu ytbot1.service

root@vm1234567:~# journalctl -fu ytbot1.service
-- Logs begin at Mon 2022-05-16 07:41:00 CEST. --
May 16 10:07:18 vm1234567.contaboserver.net systemd[1]: Starting reiss YT Bot...
May 16 10:07:18 vm1234567.contaboserver.net systemd[1]: Started reiss YT Bot.

and it stops there. the log doesn't update or add new information.

desired output:

-- Logs begin at Mon 2022-05-16 07:41:00 CEST. --
    May 16 10:07:18 vm1234567.contaboserver.net systemd[1]: Starting reiss YT Bot...
    May 16 10:07:18 vm1234567.contaboserver.net systemd[1]: Started reiss YT Bot.
Handling GoogleAPI
2022 5 15 14 38 2
./APR_2022_V20 MAY_2022_V15.mp4
            DOWNLOADING VIDEOS...
[...] *Script runs, you get the picture*

Any help? Could it be that I have my .py file in the wrong place or maybe something's wrong with the .service file/working directory? Maybe I should use a different version of python? The script i'm trying to run is pretty complex so maybe forking could be an issue (the code calls on a couple google apis but setting Type=forking just forces the service startup to infinitely load then time-out for some reason)? I don't know mayn... I appreciate feedback. Thanks!


Solution

  • Try using /usr/bin/python3 -u and then the file path.

    The -u option tells Python not to fully buffer output.

    By default, Python uses line buffering if the output is a console, otherwise full buffering. Line buffering means output is saved up until there's a complete line, and then flushed. Full buffering can buffer many lines at a time. And the systemd journal is probably not detected as a console.