Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
3  
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. –  green7 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
1  
Try to use a full path for every command like /usr/bin/sudo instead of sudo –  prophecy201 Feb 25 '13 at 15:10

2 Answers 2

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 test.sh

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 test.sh

Call file fron cron with the periocity you want:

3. crontab -e

#Run every hour
0 * * * * /path/to/file/test.sh

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

 
discard

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.