I have a systemd service spawned periodically from a timer deployed on a Centos 7 server. ExecStart directive is calling a bash script and its output is redirected to both console and a file using tee -a. Bash script is a wrapper calling a python script.
Strangely even if I can see both bash script (wrapper) output and Python script output using journalctl -xe -u my_service_name, there is no output from python script visible in log file, only output from bash commands.
Systemd service
# my_service.service
[Unit]
Description=My service
Wants=my_service.timer
[Service]
ExecStart=/bin/bash -c '/opt/services/my_service/my_service.sh | tee -a /var/log/my_service/my_service.log'
RuntimeDirectory=my_service
WorkingDirectory=/opt/services/my_service
[Install]
WantedBy=multi-user.target
Bash script
#!/bin/bash
# my_service.sh (Bash wrapper for python script)
echo "My Bash script starts"
script_dir=$(realpath "$(dirname "${BASH_SOURCE[0]}")")
python3 -m venv "${script_dir}/venv"
. "${script_dir}/venv/bin/activate"
python my_service.py
echo "My Bash script ends"
Python script
# my_service.py (called by Bash wrapper)
import logging
import sys
logger = logging.getLogger(os.path.basename(sys.argv[0]))
logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('[%(asctime)s] [%(process)d] [%(levelname)s] (%(name)s) %(message)s',datefmt='%Y-%m-%d %H:%M:%S %z')
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
logger.info("My python script {} starts".format(os.path.basename(sys.argv[0])))
logger.info("My python script {} ends".format(os.path.basename(sys.argv[0])))
journalctl output
# journalctl -xe -u my_service
-- Unit my_service.service has finished start-up with result done
feb. 0X 0X:XX:00 jenkins.hksiop.local bash[19140]: My Bash script starts
feb. 0X 0X:XX:00 jenkins.hksiop.local bash[19140]: [2021-0X-0X XX:XX:00 +0XXX] [19149] [INFO] My python script my_service.py starts
feb. 0X 0X:XX:00 jenkins.hksiop.local bash[19140]: [2021-0X-0X XX:XX:00 +0XXX] [19149] [INFO] My python script my_service.py ends
feb. 0X 0X:XX:00 jenkins.hksiop.local bash[19140]: My Bash script ends
-- Subject: Unit my_service.service has finished start-up
-- Defined-By: systemd
-- Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
log file content
# /var/log/my_service/my_service.log
My Bash script starts
My Bash script ends
What I I missing here? Why is journalctl logs showing bash output + Python output and my log file only showing bash output? How to have bash and python script output written to my log file?