1

I have demon process ex. sample. I have service file which will trigger main process launch file. When system boots up based on this file it will create /var/log/sample.out and /var/log/sample.err file for logging.

I need to redirect printf log into this sample.out file. Is there way I can do this?

I added below statement to do the same but its not showing any logs in /var/log/sample.out or /var/log/sample.err.

${binary} > ${logs}.out 2> ${logs}.err

sample.c

#include<stdio.h>
int main()
{
    printf("Demon starteed \n");
}

sample.service

[Unit]
Description="sample service"

[Service]
ExecStart=/usr/bin/sample.service.start

[Install]
WantedBy=multi-user.target

sample.service.start

service=sample
binary=/usr/bin/${service}
logs=/var/log/${service}
${binary} > ${logs}.out 2> ${logs}.err
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
raj123
  • 564
  • 2
  • 10
  • 27
  • 1
    `I need to` Why do you *need* to? Wouldn't you want to use your system logging features? And, https://stackoverflow.com/a/48052152/9072753 should answer your question. – KamilCuk Jan 05 '22 at 21:01
  • @KamilCuk this I need to do for debug purpose not for production – raj123 Jan 05 '22 at 21:06

1 Answers1

0

You cannot add arbitrary shell commands to a systemd service file. Read the documentation of systemd, in particular the part about executing binaries for how to modify the behavior of a program. To redirect standard output and error messages use:

StandardOutput=/var/log/sample.out
StandardError=/var/log/sample.err
G. Sliepen
  • 7,637
  • 1
  • 15
  • 31