1

Hi guys i'm trying to run my script the 23:55 of every day. This is my code:

# m h  dom mon dow   command
  PATH=/usr/sbin:/sbin:/usr/bin:/bin

  55 23 * * * bash -l /home/as/bin/backupAutoEtc.sh

And this works fine in crontab -e, but doesn't work if I put it in sudo crontab -e.
I have an empty line at the end of the file (I've also tried without the bash -l). The log file is the same for root crontab and normal crontab (no errors), so I don't get why the first works and the latter doesn't. I have already read a lot of answers about the topic, but none worked for me. Do you have any idea?

Thank you in advance

PS The script is the following (a simple daily backup of the /etc directory):

#!/bin/bash
dayOfTheWeek=$(date +%u)
nomeFile=backupEtc${dayOfTheWeek}.tar.gz
tar -czvf ~/${nomeFile} /etc
  • It is difficult to help, when we don't know, what the script contains. Please edit your original question and show us the content of the script backupAutoEtc.sh. Indent each line 4 spaces to render it as code. – sudodus Oct 7 '17 at 14:48
  • Root should be able to run date and tar and set variables. You can also use the explicit paths /bin/date and /bin/tar and /bin/bash in the script. -- Do you really need -l? -- Maybe root cannot read the shellscript file from your home directory. Please check the permissions. – sudodus Oct 7 '17 at 15:34
  • I added the paths for the command date and tar, but anything change. I also tried without the -l, but the result is the same! The permission for the file is 777, however i thought that permissions were ignored for root and that he could do everything, isn't that true? – Marco Cadei Oct 7 '17 at 15:42
  • 2
    Are sure it's not working - but writing the .tar.gz file in /root (which will be ~ when the script is run as root)? (You should probably change ~/${nomeFile} to an absolute path.) – steeldriver Oct 7 '17 at 16:07
  • 1
    Thank you @steeldriver that was my problem!! I didin't think that when root is executing my script the ~ is expanded in the root home directory. Thank you a lot! And thanks to everyone for the suggestions! – Marco Cadei Oct 8 '17 at 16:31
0

You could put sudo in crontab. Like this:

55 23 * * * sudo /home/as/bin/backupAutoEtc.sh

But the script needs to be added to sudoers as an exception, so the cron can run it without prompting you for root password. To do this, in case you don't know:

sudo visudo -f /etc/sudoers.d/rewrite

Inside use this line:

user ALL = NOPASSWD: /home/as/bin/backupAutoEtc.sh

Use your own username instead of user. Problem solved!

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.