9

I am trying to make a crontab that makes a new .tar file each minute every business day by using the following code:

*/1 * * * 1,2,3,4,5 tar -cf /home/user/Archiv/$(date +"%Y%m%d%H%M").tar /home/user/Textverarbeitung/ -P

This doesn't work. Why?

2
  • I can't stop reading the title as "Make every archive minute using crontab" (minute as in small) because the article is missing (not OP's fault), so I'm fixing it.
    – cat
    May 11 '16 at 22:24
  • 3
    Why would you use */1? Isn't that the same as just *?
    – naught101
    May 12 '16 at 1:35
15

cron treats % as newlines. You need to use escape it i.e. use \% to get literal % as used in date.

So you need:

date +"\%Y\%m\%d\%H\%M" 

Or you can use a script and use all the commands in the script and call the script from crontab.

From man 5 crontab:

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. There is no way to split a single command line onto multiple lines, like the shell's trailing "\".

0
4

Use \% instead of % - cron uses % as a newline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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