Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: Getting script to run in cron

  1. #1
    Join Date
    Feb 2010
    Location
    Yorkshire, United Kingdom
    Beans
    53
    Distro
    Ubuntu 10.04 Lucid Lynx

    Getting script to run in cron

    Hi everybody,

    Hoping you can help me with this one.

    Basic problem:
    I have a working bash script to do backup - it runs fine from the terminal. During the backup it makes a log file in my home area.
    I transferred it to root, and set it up to run as a cron job as root.
    It runs fine as long as I am logged in with my normal user name.
    It does not run if I am not logged in.
    Logs show the script was called by cron, but there is no output and no backing up is done.

    Here is the script
    Code:
    #!/bin/bash
    
    for i in `cat /proc/mounts | cut -d' ' -f2`; do
       if [ "$i" = "/media/Elrond" ]; then
    			sudo rsync -av --progress --delete --log-file=/home/trevor/backup_logs/$(date +%Y%m%d%H%M%S)_daily_rsync.log --exclude-from="/home/trevor/backup_logs/scripts/rsync-exclude" /home /media/Elrond/Backups/Thingol-Ubuntu/daily_home/
          exit
       fi
    done
    echo "Couldn't find Elrond, so am attempting to manually mount it"
    sudo mkdir /media/Elrond
    sudo mount -t ntfs-3g /dev/sdb1 /media/Elrond
    
    			sudo rsync -av --progress --delete --log-file=/home/trevor/backup_logs/$(date +%Y%m%d%H%M%S)_daily_rsync.log --exclude-from="/home/trevor/backup_logs/scripts/rsync-exclude" /home /media/Elrond/Backups/Thingol-Ubuntu/daily_home/
    This script is saved as /root/dailybackup (I heard about avoiding . and file extensions). root is the owner and file permissions are 755

    Here is the crontab from 'sudo crontab -l'
    Code:
    SHELL=/bin/bash
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/root
    # m h  dom mon dow   command
    45 20 * * * /root/dailybackup
    I heard about setting environment variables, but I'm not sure if I put them in the right place (the crontab).

    Any ideas?

  2. #2
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Getting script to run in cron

    Take out sudo and use the full path to each command.

    Also, you don't need to use a loop to check to see if that device is mounted.

    Try using mountpoint, or grepping the output of "mount" instead.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  3. #3
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Getting script to run in cron

    Quote Originally Posted by Trevor Burton View Post
    It runs fine as long as I am logged in with my normal user name.
    It does not run if I am not logged in.

    Any ideas?
    Is your home directory encrypted?

  4. #4
    Join Date
    Feb 2010
    Location
    Yorkshire, United Kingdom
    Beans
    53
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Getting script to run in cron

    Quote Originally Posted by CharlesA View Post
    Take out sudo and use the full path to each command.

    Also, you don't need to use a loop to check to see if that device is mounted.

    Try using mountpoint, or grepping the output of "mount" instead.
    Thanks CharlesA, will do the sudo thing (forgot to remove from the initial test script on my home area) - the loop is a bit stupid, but I was copying from something I found online. I'll try fixing it up later.

    Quote Originally Posted by DaithiF View Post
    Is your home directory encrypted?
    Hi DaithiF

    Yes it is. Do you think it is failing because it can't create the log file as root in an encrypted home directory?

    I'll report back on whether removing sudo works.

    Trevor

  5. #5
    Join Date
    Feb 2010
    Location
    Yorkshire, United Kingdom
    Beans
    53
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Getting script to run in cron

    OK, I think I got rid of sudo and put in the paths correctly, here is the script after my edits
    Code:
    #!/bin/bash
    
    for i in `/bin/cat /proc/mounts | cut -d' ' -f2`; do
    	if [ "$i" = "/media/Elrond" ]; then
    		/usr/bin/rsync -av --progress --delete --log-file=/home/trevor/backup_logs/$(date +%Y%m%d%H%M%S)_daily_rsync.log --exclude-from="/home/trevor/backup_logs/scripts/rsync-exclude" /home /media/Elrond/Backups/Thingol-Ubuntu/daily_home/
    		exit
    	fi
    done
    
    echo "Couldn't find Elrond, so am attempting to manually mount it"
    /bin/mkdir /media/Elrond
    /bin/mount -t ntfs-3g /dev/sdb1 /media/Elrond
    /usr/bin/rsync -av --progress --delete --log-file=/home/trevor/backup_logs/$(date +%Y%m%d%H%M%S)_daily_rsync.log --exclude-from="/home/trevor/backup_logs/scripts/rsync-exclude" /home /media/Elrond/Backups/Thingol-Ubuntu/daily_home/

    I checked the syslog and it showed
    Code:
    Nov  4 21:30:00 Thingol CRON[2260]: (root) CMD (/root/dailybackup)
    So if I have this right, the script was executed under the root user.
    "Elrond" did get mounted but no backups were done, and no log files were written to /home/trevor/backup_logs/

    Is it the encryption, and could I send backup logs elsewhere?

    Trevor

  6. #6
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Getting script to run in cron

    Yes the encryption will be a problem here. rsync will neither be able to read (the excludes-file), nor write (to the log file). You could change the location of these files to somewhere unencrypted (ie. outside your home directory).

    i've no experience of encrypting a home dir, so I'm going to assume that the rsync itself will handle the encrypted home ok, ie. it will blindly copy the data irrespective of the fact that it is garbage as far as it knows. I guess you'll end up with a byte-for-byte backup that you would have to unencrypt before you could do anything useful with it.

  7. #7
    Join Date
    May 2008
    Beans
    347
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Getting script to run in cron

    In your crontab, instead of putting just the path to the script, try having cron output to a log as well. Paste the following in:
    Code:
    45 20 * * * /root/dailybackup >> /path/to/log 2>&1
    This will send the stdout as well as stderr to the log file when the script attempts to run. Then you will be able to see any errors that may not have been part of the stdout for the script. If you post that, it may be easier for someone to pinpoint the problem.

  8. #8
    Join Date
    Feb 2010
    Location
    Yorkshire, United Kingdom
    Beans
    53
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Getting script to run in cron

    Quote Originally Posted by DaithiF View Post
    Yes the encryption will be a problem here. rsync will neither be able to read (the excludes-file), nor write (to the log file). You could change the location of these files to somewhere unencrypted (ie. outside your home directory).

    i've no experience of encrypting a home dir, so I'm going to assume that the rsync itself will handle the encrypted home ok, ie. it will blindly copy the data irrespective of the fact that it is garbage as far as it knows. I guess you'll end up with a byte-for-byte backup that you would have to unencrypt before you could do anything useful with it.
    OK, I'm trying to remember why I did the cron job as the root user. I think it was to avoid using sudo and the password issues that would cause if I ran it as "trevor". However, if I don't run it as "trevor" I hit the encryption issues. Am I stuck?

    Quote Originally Posted by trikster_x View Post
    In your crontab, instead of putting just the path to the script, try having cron output to a log as well. Paste the following in:
    Code:
    45 20 * * * /root/dailybackup >> /path/to/log 2>&1
    This will send the stdout as well as stderr to the log file when the script attempts to run. Then you will be able to see any errors that may not have been part of the stdout for the script. If you post that, it may be easier for someone to pinpoint the problem.
    Thanks trikster, I'll try that too to see if it confirms DaithiF's diagnosis.

    I'll report back tomorrow.

    Thanks you guys - that was such a fast response!

  9. #9
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Getting script to run in cron

    I'm not really sure how to do it with an encrypted home directory, unfortunately.

    I have a similar script for my daily backups:

    Code:
    #!/bin/bash
    
    ###
    ### backup.sh
    ### Script to backup RAID Array
    ### Created by Charles
    ### Tested 10/04/2010
    ### Updated 10/04/2010
    ###
    
    #Normal Variables
    BIN=/bin/
    USR=/usr/bin/
    EMAIL=email@host
    ARRAYPATH=/array/
    DOCPATH=/docback/
    HOME=/home/charles/
    ERROR=${HOME}logs/daily_backup_error.log
    DATE=$(${BIN}date +%D)
    FOLDER=$(${BIN}date +%m%d%Y)
    
    # Check if RAID Array is mounted, and if so, mount both backup drives by UUID.
    if ${BIN}mountpoint -q $ARRAYPATH
    then
      { ${BIN}mount -t ext4 UUID=49a8cdd0-a4fd-43e5-9c6e-3a4a3363b770 $DOCPATH; } 2> $ERROR || { echo "Mounting of backup drives failed" | ${USR}mail -s "Backup failed for $DATE" $EMAIL; exit; }
    else
      { ${USR}mail -s "RAID Array not mounted! Backup Failed for $DATE" $EMAIL; exit; }
    fi
    
    # Variables for Doc Backup
    OLDDOCNAME=$(${BIN}cat ${DOCPATH}latest)
    NEWDOCDIR=$DOCPATH$FOLDER
    OLDDOCDIR=$DOCPATH$OLDDOCNAME
    DOCLOG=${DOCPATH}log/${FOLDER}.txt
    
    # Check if Doc is mounted and if so, do a incremental backup of it.
    if ${BIN}mountpoint -q $DOCPATH
    then
      { ${BIN}cp --archive --link $OLDDOCDIR $NEWDOCDIR && echo $FOLDER > ${DOCPATH}latest && ${USR}rsync --archive --itemize-changes --delete --log-file $DOCLOG $ARRAYPATH --exclude dvds --exclude lost+found $NEWDOCDIR && ${BIN}umount $DOCPATH; } 2> $ERROR || { ${BIN}cat $ERROR | ${USR}mail -s "Doc Backup failed for $DATE" $EMAIL; }
    fi
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  10. #10
    Join Date
    Feb 2010
    Location
    Yorkshire, United Kingdom
    Beans
    53
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Getting script to run in cron

    Thanks CharlesA, that is a useful thing to share - it will take me some time to study it though.

    Here is the log from redirecting STDOUT and STDERR in cron

    Code:
    Couldn't find Elrond, so am attempting to manually mount it
    /bin/mkdir: cannot create directory `/media/Elrond': File exists
    rsync: failed to open exclude file /home/trevor/backup_logs/scripts/rsync-exclude: No such file or directory (2)
    rsync error: error in file IO (code 11) at exclude.c(1062) [client=3.0.6]
    I believe this confirms DaithiF's diagnosis that it is the encryption which is causing the problem.

    This is difficult for me, as I need the encryption as I am dealing with personal data under the Data Protection Act, yet I also need backup to ensure data security. One will have to give way.

    I'm not sure what to do now, as encryption is vital in case of burglary.

    Perhaps I arrange a script to run to a drive which doesn't need mounting, say a network share for backup?

    Anyway, may I just thank you all for your help, I am amazed at how much community support there is. I can't pay you back, but perhaps in the future I might be able to help others in a similar way. Thanks so much.

Page 1 of 3 123 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •