1

I have systemd service below, If I try to redirect application/service output to different files, it is not working. By default logs are redirecting to /var/log/messages, despite I defined log file in system service ( StandardOutput=file:/var/log/mylogfile.log and StandardError=file:/var/log/mylogerror.log)

I need help to fix this issue

OS: Cent OS 7

rsyslog: service running

[Unit]
Description=my-apps
After=network.target

[Service]
User=root
Group=root
EnvironmentFile=/apps/config
WorkingDirectory=/apps
Environment="PATH=/apps/pyenv/bin/activate"
ExecStart=/apps/pyenv/bin/python /apps/mycode.py

StandardOutput=file:/var/log/mylogfile.log
StandardError=file:/var/log/mylogerror.log
3
  • Your python interpreter generally outputs to standard out/error, so those are probably redirecting okay. What is unknown is how /apps/mycode.py is handling it's output. If the python script is sending output to the system log facilities, instead of standard out, you cannot redirect that. You will have to modify the python script to either output to the file you want, or give it some kind of command line parameter to do so--if the script supports such a command line.
    – C. M.
    Apr 26 at 6:24
  • Its not output to any file, its output to console, Let me update my question
    – Vidya
    Apr 26 at 7:22
  • Understand that nothing outputs or redirects to /var/log/messages unless it is specifically programmed to do so. So if you have output to the 'messages' log file, something is instructing it to. You need to find out what is doing that, and tell it to output to the file you want the output to appear in. While the same output may be appearing on your console, that usually means that it is being duplicated to your console, by various methods.
    – C. M.
    Apr 26 at 8:25
2

The systemd version in CentOS 7 (version 219) does not support the StandardOutput=file:.../StandardError=file:... syntax, which was only added in systemd version 236:

StandardOutput=
     Controls where file descriptor 1 (STDOUT) of the executed processes is connected to. Takes one of inherit, null, tty, journal, syslog, kmsg, journal+console, syslog+console, kmsg+console or
           socket.

(from the systemd.exec man page in CentOS 7).

I think since you've not specified a valid value, the default value of journal is used and the journal forwards everything to syslog:

journal connects standard output with the journal which is accessible via journalctl(1). Note that everything that is written to syslog or kmsg (see below) is implicitly stored in the journal as well, the specific two options listed below are hence supersets of this one.

syslog connects standard output to the syslog(3) system syslog service, in addition to the journal. Note that the journal daemon is usually configured to forward everything it receives to syslog anyway, in which case this option is no different from journal.

If you are able to modify the python application, you can use pythons logging module to log to a file:

import logging
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
logging.error('And non-ASCII stuff, too, like Øresund and Malmö')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.