0

I have a .sh script that creates a .tar.gz file and transfer's it to a remote server using RSA keys for authentication.

The script will run from terminal but it will not when I try to set it up as a Cron job.

I receive an email from the Cron Daemon stating this failure : "/bin/sh: /home/backup.sh: Permission denied" , and the file is not transferred to the other server.

I have established the most likely cause from this askubuntu post that this is most likely due to the fact that "Cron" passes a minimal set of environment variables to your jobs.

The fix seems to be to include the

#!/bin/bash
PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# rest of script follows

Any help with how to configure this would be greatly apprecited.

I am running Ubuntu Server 11.04

EDIT : ls -la backup.sh gives -rw-r--r-- 1 root root 1053 2012-10-13 15:08 backup.sh

2
  • 2
    Based on what you've told us (ie, the contents of cron's email) other explanations present themselves. Could you edit the output of ls -la /home/backup.sh into your question?
    – MadHatter
    Oct 15, 2012 at 11:14
  • Added in there @MadHatter
    – Ríomhaire
    Oct 15, 2012 at 11:21

4 Answers 4

1

Thanks for the ls output. It seems that cron's complaint about the file not having the right permissions can be taken at face value (specifically, it lacks execute permissions); try doing chmod 755 ./home/backup.sh and see if that helps.

Edit: I'm glad we got that sorted out. You should firstly look at which user is running this job, under cron, and examine the ownership of the backupdir directory. The user needs to have permissions to write in that directory; blindly chmodding things won't help.

1
  • Thanks @MadHatter, that has helped. I now get emailed the output of the cron. However, the script saves the server-backup.tar.gz in a directory named backupdir. And the cron is unable to access that folder to change scp it across. Should I run the same chmod 755 on backupdir, to fix this? Thanks very much
    – Ríomhaire
    Oct 15, 2012 at 11:51
0

The "Permission denied" message seems to be suggesting that the user that cron is running the script as doesn't have suitable permission to access the script /home/backup.sh

Check that the user who's crontab is being used has at least r-x on /home and on backup.sh.

0

You can set any environment variable you like in a crontab. One way is to put a line in the crontab file in the format

varname=value

That environment variable will then be set for any cron jobs which appear after that line in the file. If you want to be sure that it only applies to that command, just put the variable declaration at the start of the command, in the same way you would if changing a variable for just one command at the console:

varname=value command

So it might look like

0 5 * * * varname=value command

For a personal crontab or

0 5 * * * root varname=value command

For a system one.

Note: I've answered your question, but the others are right that this is probably not the cause of your problem.

0

It would probably be best to append to $PATH than to overwrite it in this instance.

PATH=${PATH}:/others/bin

You could also debug what your PATH variable is set to by using the following

#!/usr/bin/env bash
logger "Path-dump: "${PATH}

You can check the output in /var/log/syslog and you should see some output

cat /var/log/syslog | grep "Path-dump"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .