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
    Use the full path to your script. lol . SHELL=/bin/bash 45 5 * * * /home/myname/cronjobs/buphomebasis.sh – Panther Nov 07 '17 at 18:33
  • The error message has a lot of information, read it carefully: There's a syntax error on line 23 of your script. Something on that line is being interpreted as a command. That command is not in cron's $PATH...which is NOT the same as your $PATH. – user535733 Nov 07 '17 at 18:58
  • The primary problem is that you are trying to execute a crontab file (backup.sh) as a shell script - they are different things with different syntaxes and live in different places – steeldriver Nov 07 '17 at 19:33
  • Steeldriver, I'm not sure what you mean. Made backup.sh with crontab -e, so the syntax should be correct. I stored backup.sh in the same directory as buphomebasis.sh and then used the commandline to execute backup.sh from that directory – jfh Nov 07 '17 at 20:37

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
  • 74,764
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
  • 549
  • just using crontab -e You mean full path to buphomebasis? Like: – jfh Nov 07 '17 at 18:49
  • 1
    just using crontab -e You mean full path to buphomebasis? Like: 45 5 * * * /home/myname/cronjobs/buphomebasis – jfh Nov 07 '17 at 18:52
  • Exactly in terms of the path. If you need to use sudo for the command you should run it from root's crontab though since yours does not have permissons to execute sudo without including the password in the user crontab in some way. See here for previous detailed answer https://askubuntu.com/questions/173924/how-to-run-a-cron-job-using-the-sudo-command – m_krsic Nov 07 '17 at 19:14
  • removed sudo from buphomebasis.sh – jfh Nov 07 '17 at 20:01
  • removed sudo from buphomebasis.sh but that made no difference. could'nt find my backup.sh in /etc/cron.d but I'm not sure if that is needed because I run it from the directory /home/myname/cronjobs/ Again, what am I missing in my appraoch – jfh Nov 07 '17 at 20:10
  • Do everything the same as you did before, but include the full path to the script and do it with sudo crontab -e. That will use root's chrontab to run the script from your home folder location while allowing it to execute the sudo command in the script. – m_krsic Nov 07 '17 at 20:33
  • m_krsic thank you for your comment. I will try it tomorrow and let you know the result – jfh Nov 07 '17 at 21:28
  • to my friendly advisors: every thing works well now. My big mistake was that I made the modifications to backup.sh with gedit. When I finally realised that crontab -e was needed for proper editing backup.sh everything worked. Thank you all: problem solved! – jfh Nov 10 '17 at 10:53
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,808