I am trying to create a systemd service to clear a directory of old files automatically. If l were to do this manually, l would be using a pair of commands like
$ /usr/bin/touch -t $(/usr/bin/date -d'-7day' +%Y%m%d%H%M) /tmp/cutoff
$ /usr/bin/find /path/to/dir ! -newer /tmp/cutoff -exec rm {} \;
The former line does in fact generate the placeholder file in the /tmp
directory. However, the following service definition
[Unit]
Description=Cleanup index cache left behind by bup backup
Type=oneshot
[Service]
ExecStart=/usr/bin/touch -t $(/usr/bin/date -d'-7day' +%%Y%%m%%d%%H%%M) /tmp/cutoff
SyslogIdentifier=%p
Fails to generate the place-holder
$ /usr/bin/touch -t $(/usr/bin/date -d'-7day' +%Y%m%d%H%M) /tmp/cutoff
$ ls -l /tmp/cutoff
-rw-r--r--. 1 user group 0 Mar 20 14:36 /tmp/cutoff
$ rm /tmp/cutoff
$ sudo systemctl start cleanup_index_cache
$ ls -l /tmp/cutoff
ls: cannot access '/tmp/cutoff': No such file or directory
$
$ sudo systemctl start cleanup.service
$ ls -l /tmp/cutoff
If there is a way to delete files/directories older than a certain age using systemd? Is there something l'm missing in just transcribing shell commands to systemd directives?
Best Answer
systemd does not send commands in
ExecStart=
and its relatives through the shell. It executes them directly. So you cannot use shell constructs such as$()
.It's not clear why you're generating a date and touching a file with it anyway.
find
is perfectly capable of finding files older than a certain time on its own.From the man page:
That bit explains:
find
is also capable of deleting files on its own and does not need to call out torm
(and it's potentially a bad idea to do so for a variety of reasons).So to delete files older than 7 days, you can simply run
find
directly, with no other funny stuff: