3

I am regularly doing a long running (~5 day) data processing programme. I'm using Ubuntu and running the command with a systemd transient task via systemd-run --unit data_import /path/to/my-script.sh. It's working well. I can look at the logging standard output of the script with journalctl -u data_import.service.

I would like the stdout (& stderr) from the script to be saved to a file as well as the systemd journal. I see that systemd-run --unit data_import -p "StandardOutput=file:/path/my-logging-file.txt … will save stdout to that file. However it won't log to the journald. I tried to provide the argument twice systemd-run -p "StandardOutput=file:/path/my-logging-file.txt" -p "StandardOutput=journal" …, but that didn't work.

Is it possible with systemd to log the stdout to a file and to the systemd journal? (likewise for stderr?)

Ubuntu 18.04 & 20.04, systemd v250. or v245 etc

Amandasaurus
  • 31,471
  • 65
  • 192
  • 253

3 Answers3

2

I guess we can't but found one reference https://www.freedesktop.org/software/systemd/man/systemd.exec.html#StandardOutput= which might help (pay attention to Takes one of string).

Controls where file descriptor 1 (stdout) of the executed processes is connected to. Takes one of inherit, null, tty, journal, kmsg, journal+console, kmsg+console, file:path, append:path, truncate:path, socket or fd:name.

Though we can change default standard output type.

asktyagi
  • 2,860
  • 2
  • 8
  • 25
0

Did you try to create a log file directly into your script and add the info every time necessary ? I know its pretty obvious but I'm curious to see if it works.

For example with bash, directly in your script :

# Declaration
LOG=/var/log/your_script_log.log
# Or with a date 
TODAY=$(date +%Y-%m-%d)
LOG=/var/log/your_script_log-$(TODAY).log
...
# Beginning of main script
echo -e $TODAY > $LOG
...
# Every time needed during the script
if [[ $state == *"stopped"* ]];then
    echo "A very important message." >> $LOG
...

With a system like this you will need to configure a logrotate to avoid the hundreds logs after a few month.

Matias V
  • 51
  • 6
  • 2
    Yes I _could_ do that, but that's implementing half of what `systemd` I supposed to do (AIUI). I want to know if systemd can do it. – Amandasaurus Aug 31 '22 at 08:20
0

@Amandasaurus Okay I understand. Then I recommend you create a service file under "/etc/systemd/system/[The name of your script].service" and add the minimum necessary information :

[Unit]
Description=*The name of your script*

[Service]
ExecStart=/path/to/my-script.sh
ExecStop=/bin/kill $MAINPID
KillMode=process
Type=simple
StandardOutput=/path_to_log/service.log
StandardError=/path_to_log/service_error.log

[Install]
WantedBy=multi-user.target

And then configure a logrotate or add "append" to the StandardOutput argument. After that do the following :

systemctl daemon-reload
systemctl start *The name of your service*

To enable start at boot of your service :

systemctl enable *The name of your service*

Source of the log argument in service file : here

Matias V
  • 51
  • 6