[CentOS8] How to output Python standard output to systemd log

Introduction

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.

environment

CentOS Linux release 8.1.1911 (Core)

Unit file description

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

Options when executing Python commands

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.

end

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

[CentOS8] How to output Python standard output to systemd log
How to output "Ketsumaimo" as standard output in Python
[GCP] How to output Cloud Functions log to Cloud Logging (Stackdriver Logging) (Python)
Output Python log to console with GAE
How to install python3 with docker centos
How to install python
Output python log to both console and file
How to erase the characters output by Python
Introducing Python 2.7 to CentOS 6.6
Output log in JSON format with Python standard logging
[Python] How to output the list values in order
[2020.8 latest] How to install Python
How to install Python [Windows]
python3: How to use bottle (2)
[Python] How to use list 1
How to update Python Tkinter to 8.6
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
Switch from python2.7 to python3.6 (centos7)
How to run Notepad ++ Python
How to change Python version
How to develop in Python
[python] How to judge scalar
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
Decorator to silence standard output
How to install Python2.7 python3.5 with pyenv (on RHEL5 CentOS5) (2016 Nov)
Python logging standard library for file output by log level
How to debug the Python standard library in Visual Studio
Change the standard output destination to a file in Python
Python learning basics ~ How to output (display) a character string? ~
[Python] How to output a pandas table to an excel file
How to start Apache by specifying httpd.conf with systemd (CentOS7, CentOS8)
How to log in to AtCoder with Python and submit automatically
How to install python using anaconda
How to write a Python class
[Python] How to FFT mp3 data
[Python] How to do PCA in Python
Python: How to use async with
[Python] How to derive nCk (ABC156-D)
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
Output to csv file with Python
[Introduction to Python] How to parse JSON
Write standard output to a file
Make standard output non-blocking in Python
How to get the Python version
How to install PyPy on CentOS
How to get started with Python
[Python] How to import the library
Unit test log output with python
How to install TensorFlow on CentOS 7
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
[Python] How to swap array values