0

I have this crontab

* * * * * tar -czf /backup/$(date +%F--%T)-localusers.tgz /vagrant

It does not work. But if I do

tar -czf /backup/$(date +%F--%T)-localusers.tgz /vagrant/

It works.

Anybody have a clue what's going on? I do keep getting a mail though:

N 10 (Cron Daemon) Thu Aug 23 10:43 28/1130 "Cron <root@localhost> tar -czf"

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
iamAguest
  • 483

1 Answers1

1

Your problem is likely because of the cron special treatment of percent sign:

The entire command portion of the line, up to a newline or % character, will be executed by `/bin/sh or by the shell specified in the SHELL variable of the crontab file. Percent-signs (%) in the command, unless escaped with backslash (), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.

So you need to escape them:

* * * * * tar -czf /backup/$(date +\%F--\%T)-localusers.tgz /vagrant
weirdan
  • 551
  • 4
  • 9
  • Would it be bad if I changed the shell that crontab uses into /bin/bash? Would I break anything? – iamAguest Aug 23 '18 at 14:58
  • It would only affect you particular crontab, so it should be fairly safe. Of course you would still need to escape percent signs, as those are processed before the command line is handed out to shell (whatever that shell may be). – weirdan Aug 23 '18 at 15:00