I am trying to run a systemd service as a different user (db2inst1) which looks as follows:
[Unit]
Description=Dummy Service
After=multi-user.target
Conflicts=getty@tty1.service
[Service]
Type=simple
User=db2inst1
Group=db2iadm1
ExecStart=/usr/bin/python3 /usr/bin/dummy_service.py
StandardInput=tty-force
[Install]
WantedBy=multi-user.target
dummy_service.py looks as follows:
import logging
import random
import time
from systemd.journal import JournaldLogHandler
# get an instance of the logger object this module will use
logger = logging.getLogger(__name__)
# instantiate the JournaldLogHandler to hook into systemd
journald_handler = JournaldLogHandler()
# set a formatter to include the level name
journald_handler.setFormatter(logging.Formatter(
'[%(levelname)s] %(message)s'
))
# add the journald handler to the current logger
logger.addHandler(journald_handler)
# optionally set the logging level
logger.setLevel(logging.DEBUG)
if __name__ == '__main__':
while True:
# log a sample event
logger.info(
'test log event to systemd! Random number: %s',
random.randint(0, 10)
)
# sleep for some time to not saturate the journal
time.sleep(5)
But when I start the service I keep getting:
[root@dashmpp-head-0 - Db2wh /]# systemctl status dummy.service
ā segment.service - Segment Service
Loaded: loaded (/usr/lib/systemd/system/dummy.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2020-09-21 21:16:37 UTC; 8s ago
Process: 241156 ExecStart=/usr/bin/python3 /usr/bin/dummy_service.py (code=exited, status=208/STDIN)
Main PID: 241156 (code=exited, status=208/STDIN)
Sep 21 21:16:37 dashmpp-head-0 systemd[1]: Started Dummy Service.
Sep 21 21:16:37 dashmpp-head-0 systemd[1]: dummy.service: main process exited, code=exited, status=208/STDIN
Sep 21 21:16:37 dashmpp-head-0 systemd[1]: Unit dummy.service entered failed state.
Sep 21 21:16:37 dashmpp-head-0 systemd[1]: dummy.service failed.
If run the python script as root user in dummy.service file it works fine. Any pointers why I am not able to execute the python script as different user?
I am on Centos7.