Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to create backup from directory using cron. I wrote following command:

cd path && sudo zip -r "backup-$(date +"%Y-%m-%d").zip" git/ && sudo mv "backup-$(date +"%Y-%m-%d").zip" /backup/

when I call it from commandline then everything is ok. But when I put it to cron then it does not work. Log contains only part of this command and nothing more. What shall I do?

Crontab content:

* * * * * cd path && zip -r "backup-$(date +"%Y-%m-%d").zip" git/ && mv "backup-$(date +"%Y-%m-%d").zip" /backup/

share|improve this question
You should put that on a .sh file, chmod +x file, and then call that file from cron – LnxSlck Feb 25 '13 at 14:21
Please post the contents of your crontab file. – green Feb 25 '13 at 14:23
I know, i put it because it does not work ;) sorry for confusing you – szaman Feb 25 '13 at 14:29
Please follow @LnxSlck's comment ;) (you can use a pipe to a text file to output errors :) ) – Rinzwind Feb 25 '13 at 14:31
Try to use a full path for every command like /usr/bin/sudo instead of sudo – prophecy201 Feb 25 '13 at 15:10
up vote 1 down vote accepted

As requested i move this to an answer:

Put the commands you wan to run in a .sh file, for example:

1. vi

cd path && sudo zip -r "backup-$(date +"%Y-%m-%d").zip" git/ && sudo mv "backup-$(date +"%Y-%m-%d").zip" /backup/

Save and exit.

Run on a console

2. chmod +x

Call file fron cron with the periocity you want:

3. crontab -e

#Run every hour
0 * * * * /path/to/file/

Make sure the the user that uses cron to execute the file, has permissions to run the file.

share|improve this answer

This happens because (crontab manpage):

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.

As you're using % in your backup command, "you should put that on a .sh file, chmod +x file, and then call that file from cron", as suggested by LnxSlck.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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