Redirecting output from a systemd service (python script) to a log file?

Continuing from here: Force rc.local to wait for network, I have been working on implementing my own systemd service to run a custom python script. Everything seems to be working, except that I cannot seem to get the script stdout to redirect to a log file.

I recognize this has nothing to do with OSMC, just wondering if someone knows the answer offhand… what changes would I make to get stdout to log into a file somewhere?

Here are the contents of the new ttcbutler.service file I have defined in /etc/systemd/system:

Description=ttcbutler Daemon


ExecStart=/usr/bin/python -u /home/pi/ 1> /tmp/ttc_sms.log 2>&1 &


The service is running correctly, as confirmed by sudo systemctl status ttcbutler.service -l, except that I do not get anything populated into ‘/tmp/ttc_sms.log’

This tutorial indicates I should just be able to use:
ExecStart=/usr/bin/python /home/pi/ > /home/pi/myscript.log 2>&1

As shown in the version of the .service file I posted above, I also tried with the -u switch that I used to have in rc.local, but no luck.

Finally, I tried enclosing the whole ExecStart line in quotes (’ and "), based on this post. Again, no joy:
ExecStart=/usr/bin/python "/home/pi/ > /tmp/ttc_sms.log 2>&1"

Is there anything else I should try?

Well, I am still not sure why the log file redirect did not work. But I have been able to get the outputs I need from journalctl. I gather this is the way systemd was meant to be used, so it’s probably just a matter of me getting used to the new world order.

This won’t work, because standard output redirection using a greater than sign is a shell (bash/sh) feature, but ExecStart does not run your program in a shell, it runs it directly without a shell. So the tutorial is in error on this point.

As you’ve noted, systemd by default captures all stdout and stderr emitted from the program you run in a service and writes it to the system journal, so you can view all of the stdout from the program using journalctl. You can filter based on service name thus you can view only the portion of the journal belonging to your service like this:

sudo journalctl -u ttcbutler.service

Using journalctl I am able to see log file for the service, not the application (daemon) running in the service. Can anybody help me with this please.

@anjali resurrecting a year old thread, with no useful information/question is frowned upon here. If you have a question, start a new thread and ask an actual question.

ExecStart=/bin/sh -c ‘/usr/bin/python -u /home/pi/ 1> /tmp/ttc_sms.log 2>&1 &’