0

This question already has an answer here:

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"

marked as duplicate by Jeff Schaller, roaima, Community Aug 23 '18 at 15:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.