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

2
  • 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. Commented Jun 18, 2022 at 1: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: unix.stackexchange.com. Commented Jun 18, 2022 at 9:30

1 Answer 1

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/"'
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.