4

I have a script (buphomebasis.sh)that uses rsync to make backups from my home directory. Its content is:

sudo rsync -avz /home /media/myname/mybackupdsk

It works very well, just by invoking that script from the commandline. Now I made a cronjob (backup.sh); its content is:

SHELL=/bin/bash
45 5 * * * buphomebasis.sh

This should make a backup every day at 45 minutes past 5 AM. Both scripts are in my home/myname/cronjobs/ directory, but running backup.sh results in the following error message:

cronjobs/backup.sh: regel 23: 45: opdracht niet gevonden

or in English:

cronjobs/backup.sh: line 23: 45: command not found

Can anybody help me with what is wrong/missing in this cronjob?

jfh
  • 51

3 Answers3

4

Use the full path. Cron does not inherit your path, so you need to use full-paths for a script to properly be executed in Cron.

So, you'd have a cron entry like follows:

45 5 * * * /home/myname/cronjobs/buphomebasis
Thomas Ward
  • 78,878
0

Are you using sudo crontab -e or just crontab -e when you make your changes? One will add it to your user's crontab the other to root's.

Full path wouldn't hurt either

m_krsic
  • 559
0

Note: Replace all occurrences of $USER with your actual user name.

You have to use the full path, or else the Cron job won't be able to find it. You need to put something like

SHELL=/bin/bash
45 5 * * * /home/$USER/buphomebasis.sh

in your crontab in order for it to run. You also need to make sure the script is executable, so be sure to have #!/bin/bash in the beginning of your script. Then, make it executable with chmod +x /home/$USER/buphomebasis.sh.

I recommend you write your script like:

#!/bin/bash
rsync -avz /home /media/myname/mybackupdsk >> /home/$USER/backup.log

so you can see if rsync has any errors. Also, be sure to put it in the root crontab, so it can run without a password, or else it will fail. You can edit the root crontab with

sudo crontab -u root -e
user8292439
  • 3,878
  • 7
  • 32
  • 57