0

i'm trying to run a php script 24/7 with systemd. the terminal output of the script is normally irrelevant so i set 'StandardOutput=0' and 'StandardError=0' to not mess logfiles with gigabytes of data.

but it were great to see both when i from time to time use:

sudo systemctl status xyz.service

i found no way to accomplish this - that's what i have:

[Unit]
Description=xyz.php

[Service]
ExecStart=/xyz.php
Restart=always
RestartSec=10
StandardOutput=null
StandardError=null

[Install]
WantedBy=multi-user.target
  • When you remove the statements journald will keep and rotate the last few lines written to,stdout/err. – eckes Nov 03 '17 at 06:03
  • no - then it writes ongoing every line into syslog and daemon.log - what ends in gigabytes of sd-card writing on a raspberry pi – user8613418 Nov 03 '17 at 08:34
  • It does not do that for Linux distributions I have seen. Most don’t write to syslog, store the journald Files in Ramdisk and overwrite them. You might need to adjust your systemd/journald settings for this. What distribution is that? – eckes Nov 03 '17 at 08:36
  • it's raspbian stretch. yes - but my question is for a solution with systemd and whether there is one? – user8613418 Nov 03 '17 at 08:39
  • Yes there is one, default systemd does not have this problem. See the loggly article it describes the volatile version which certainly is a good option to save flash in embedded systems: https://www.loggly.com/ultimate-guide/linux-logging-with-systemd/ (storage=volatile, forwardtosyslog=no) the verbose systemctl status command will list the last n lines from this ram ring buffer. – eckes Nov 03 '17 at 08:41
  • thanks for the link i'll have to look into this! but is there not a different solution to say: write to garbage but when opened status via ssh then write to status output - something like StandardOutput=status>null – user8613418 Nov 03 '17 at 08:50
  • ok - that's really a very good explanation the link you gave me! forward to syslog and write persistent seems to be the default setting - and your solution should work fine. but the/my problem is i want to share this script to users and do not want to force them to change/mess around in their log settings. they should just put the php-script in a directory and copy/paste/enable the systemd service - that's why i'm despaired searching for such a kind of solution ... – user8613418 Nov 03 '17 at 09:07
  • You can implement something in the php script, an if around the echo or using log method with levels which are triggered by flag file. Or you can run the script in screen or tmux then you can attach to it if you want. However I think especiallly on a Pi the volatile setting protects the hardware. And as I said the other distributions mostly log to /run by default. The mechanism to see the last view logged lines before an crash or to attach (filtered) to the output of a service is very convenient in journald, however. – eckes Nov 03 '17 at 10:01
  • thank you for all the time and ideas you invested in my problem - much appreciated :) yes i will to go for one of the mentioned solutions - i was just thinking there were a different one in systemd service setup for the php-script that i was not aware of ... – user8613418 Nov 03 '17 at 11:15
  • BTW, this shows the power you gain by using the systemd journal (only): https://www.digitalocean.com/community/tutorials/how-to-use-journalctl-to-view-and-manipulate-systemd-logs and in regards of logging, you can use `<0>Emergency` syslog priority markers in your php script and it will show the message levels in color. – eckes Nov 03 '17 at 19:21

0 Answers0