Search code examples
linuxshellsystemd

How to execute multiple command in ExecStart


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


Solution

  • 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/"'