3

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?

  • 3
    Use the full path to your script. lol . SHELL=/bin/bash 45 5 * * * /home/myname/cronjobs/buphomebasis.sh – Panther Nov 7 '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 7 '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 7 '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 7 '17 at 20:37
3

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
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
  • I would not use variables with chron, use the user name not $USER – Panther Nov 7 '17 at 19:32
  • I know, it's just the place holder I like the most. I added a note at the top of my answer. – NerdOfLinux Nov 7 '17 at 19:35
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

  • just using crontab -e You mean full path to buphomebasis? Like: – jfh Nov 7 '17 at 18:49
  • 1
    just using crontab -e You mean full path to buphomebasis? Like: 45 5 * * * /home/myname/cronjobs/buphomebasis – jfh Nov 7 '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 askubuntu.com/questions/173924/… – m_krsic Nov 7 '17 at 19:14
  • removed sudo from buphomebasis.sh – jfh Nov 7 '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 7 '17 at 20:10

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.