0

I wanted to use systemd to archive some logs periodically. However, it does not work as I wanted it to. According to my test, the second part of in string command failed and I really have no idea what going wrong. Would appreciate if anyone could give me some pointer. Thanks.

Inside Unit File:

ExecStart=/bin/tar -zcvf "/var/log/test/$(/bin/date)_syslog_archive.tar.gz" "/tmp/log/"

Getting error:

Main process exited, code=exited, status=2/INVALIDARGUMENT

gtfonow
  • 13
  • 1
  • 1
    Use ExecStart to start a shell script; put your logic in there. There's no shell otherwise, so you can't use shell syntax. – Charles Duffy Jun 18 '22 at 01:08
  • From the tag: systemd questions should be for *programming questions* using systemd or its libraries. Questions about *configuring the daemon* (including writing unit files) are better directed to Unix & Linux: https://unix.stackexchange.com. – Rob Jun 18 '22 at 09:30

1 Answers1

1

A separate script is a possible solution, as Charles wrote, or you can run the command with bash -c or sh -c, like this:

/bin/bash -c '/bin/tar -zcvf "/var/log/test/$(/bin/date)_syslog_archive.tar.gz" "/tmp/log/"'

As an aside, you probably want a + parameter of the date command, like date +%s or date +%Y%m%d so that you get something that is suitable for a filename, making it something like this:

/bin/bash -c '/bin/tar -zcvf "/var/log/test/$(/bin/date +%s)_syslog_archive.tar.gz" "/tmp/log/"'
Erwin
  • 844
  • 4
  • 14
  • That is essentially just executing a shell script file with all the logics or warping the chained command. Looks like ExecStart is NOT capable of doing multiple in line command execution. – gtfonow Jun 19 '22 at 20:01
  • Agreed on the "executing a shell script", with the advantage of not having to potentially manage an additional file/script, if you're creating a package or orchestrating the rollout somewhere. – Erwin Jun 19 '22 at 21:12