I created a systemd file unit (Centos 7) and I wanted to save the Python output to a file but the service won't start with the below code.

[root@static ~]# cat /etc/systemd/system/pykms.service

ExecStart=/usr/bin/python2.7 /usr/local/py-kms-master/server.py 1688 -v > /usr/local/py-kms-master/pykmsss.log


NOTE: if I delete the line after ">" above, then everything works fine but I want to save the logs to a file.

systemctl status pykms -l

 [root@static ~]# systemctl status pykms -l
pykms.service - PY-KMS
   Loaded: loaded (/etc/systemd/system/pykms.service; enabled)
   Active: active (running) since Tue 2015-11-24 20:54:28 IRST; 2s ago
 Main PID: 2788 (server.py)
   CGroup: /system.slice/pykms.service
           └─2788 /usr/bin/python2.7 /usr/local/py-kms-master/server.py 1688 -v

Nov 24 20:54:28 server.de systemd[1]: Starting PY-KMS...
Nov 24 20:54:28 -server.de systemd[1]: Started PY-KMS.

The man page for a systemd unit states:

Specifically, redirection using "<", "<<", ">", and ">>", pipes using "|", running programs in the background using "&", and other elements of shell syntax are not supported.

It then goes on to explain how these can be used:

ExecStart=/bin/sh -c 'dmesg | tac'

In your situation you could do this:

ExecStart=/bin/sh -c 'exec /usr/bin/python2.7 /usr/local/py-kms-master/server.py 1688 -v > /usr/local/py-kms-master/pykmsss.log'
| improve this answer | |
  • Thank you i used your code and restarted the server but i got the error below when i wanted to see the service status – reza Nov 23 '15 at 23:45
  • Nov 24 03:21:45 static. sh[2049]: /bin/sh: exec /usr/bin/python2.7 /usr/local/py-kms-master/server.py 1688 -v > /usr/local/py-kms-master/kms.log: No such file or directory – reza Nov 24 '15 at 0:16
  • i faced with this error, what should i do? – reza Nov 24 '15 at 0:16
  • @JdeBP you're correct, the -c is essential. I'm not sure how I missed it from the implementation example but thank you for fixing it – roaima Nov 24 '15 at 10:00
  • it does not work. what should I do then? – reza Nov 24 '15 at 10:21

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.