I made a Twitter bot with Python, but I couldn't see it with the log systemctl status
command output by the print function, so I'll remember the solution. I would be grateful if you could tell me if there is a better way.
CentOS Linux release 8.1.1911 (Core)
Description of Unit file in ʻetc \ systemd \ system \ `
bot.service
[Unit]
Description=Python Tweet Collector
[Service]
ExecStart=Executable file(Here is a shell script that launches a bot)Path
Type=simple
StandardOutput=journal #Make standard output visible in journalctl
StandardError=journal #Make standard errors visible in journalctl
[Install]
WantedBy=multi-user.target
It seems that the standard output of Python may be buffered and the output of the print statement may not be displayed with only the above settings ... So I wrote a shell script and started the bot from there
start_bot.sh
#!/bin/bash
cd $(dirname $0) #Move to the directory where this file is located
. venv/bin/activate #Enable Python virtual environment
python -u bot.py # -Disable buffer with u option
Finally, grant execute permission to the shell script with chmod 744 start_bot.sh
Launch the bot
systemctl start bot.service
Check the status of the bot
systemctl status bot.service
Check the log
journalctl -u bot.service
You can see that the content output by the Python print statement is displayed in the log.
I am still a beginner about Linux, so I would be very grateful if you could teach me any suggestions or advice regarding inappropriate points.
Recommended Posts