Find the answer to your Linux question:
Results 1 to 6 of 6
Alright, I have this script, and it works pretty well if I execute it from the terminal. However, it seems that cron has an inability to correctly carry out the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux User Agent-X's Avatar
    Join Date
    May 2005
    Location
    Dimension X
    Posts
    261

    Why doesn't my backup script work with cron?


    Alright, I have this script, and it works pretty well if I execute it from the terminal. However, it seems that cron has an inability to correctly carry out the tasks.

    To say the least, I don't really see any different between what cron and I are doing.

    Maybe one of you can figure it out:

    details:
    1. the cronfile is called "cronfile"
    2. it's located inside of the user's home folder: $HOME/.cronjobs/
    3. I installed the cronfile from rc.local
    4. The part I'm concerned with is in red

    Code:
    #!/bin/sh
    crontab -u workstation /home/workstation/.cronjobs/cronfile
    Code:
    #
    # This is the crontab for $HOME/
    #
    ##########################################################################
    # Update the background wallpaper every _1_ minutes
    */1 * * * * bash $HOME/.GUI/random-background.sh
    ##########################################################################
    #
    ##########################################################################
    #
    # openoffice.org backups
    # */5 * * * * bash $HOME/.cronjobs/backups/ooo_backups.sh
    ##########################################################################
    #
    ##########################################################################
    # openoffice.org file collection
    # */20 * * * * bash $HOME/.cronjobs/backups/ooo_filecollect.sh
    ##########################################################################
    #
    ##########################################################################
    # AbiWord file collection
    */5 0-23 * * * bash $HOME/.cronjobs/backups/abw_filecollect.sh
    ##########################################################################
    Not too much of a big deal.

    Now, every five minutes, the script does execute. But it seems to give me a file with the extension ".tar"; it doesn't seem to give me that archived file itself.

    Here is abw_filecollect.sh

    Code:
    #!/bin/sh
    # 
    # This script collects scattered abiword.org documents and archives them
    #
    # Change the 5 variables below to fit your computer/backup
    #
    #
    
    # create the archive folders
    mkdir $HOME/archives/
    mkdir $HOME/archives/abiword/
    
    # Collect files
    mkdir /home/workstation/abiword/
    mkdir /home/workstation/abiword/Desktop/
    # copy from /home
    cp /home/workstation/*.abw /home/workstation/abiword/
    cp /home/workstation/*.awt /home/workstation/abiword/
    cp /home/workstation/*.doc /home/workstation/abiword/
    cp /home/workstation/*.rtf /home/workstation/abiword/
    cp /home/workstation/*.txt /home/workstation/abiword/
    cp /home/workstation/*.bak~ /home/workstation/abiword/
    # copy from ./Desktop
    cp /home/workstation/Desktop/*.abw /home/workstation/abiword/Desktop/
    cp /home/workstation/Desktop/*.awt /home/workstation/abiword/Desktop/
    cp /home/workstation/Desktop/*.doc /home/workstation/abiword/Desktop/
    cp /home/workstation/Desktop/*.rtf /home/workstation/abiword/Desktop/
    cp /home/workstation/Desktop/*.txt /home/workstation/abiword/Desktop/
    cp /home/workstation/Desktop/*.bak~ /home/workstation/abiword/Desktop/
    
    COMPUTER=workstation # name of this computer
    DIRECTORIES="/home/workstation/abiword/" # directories to backup
    BACKUPDIR=$HOME/archives/abiword # where to store the backups
    TAR=/bin/tar                                            # name and locaction of tar
    
    #You should not have to change anything below here
    
    PATH=/usr/local/bin:/usr/bin:/bin
    DM=`date +%m-%d-%Y@"("%H.%M.%S")"`             # mm/dd/yy@HH:MM e.g. 07-04-2005@(07.14.03)
    
    # Backup the moment
    $TAR -cf $BACKUPDIR/$DM-$COMPUTER.tar $DIRECTORIES
    
    # Remove used materials
    rm -r /home/workstation/abiword/
    So, that's that.
    Cron doesn't seem to actually make an archive file; it makes a file with the extension ".tar"; each time, the file is like 0 bytes. I don't know what is up with that.



    The last file was executed by terminal.
    The others were done by cron.

    But, if I execute it from the terminal while logged in as the user "workstation," then it actually creates the archived ".tar" of whatever abiword files might be around.

    I don't know why it's not working.

    Any guesses?

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Need more information. I suggest you do the following:

    Enable verify mode by appending -vx to the 1st line of your script:

    Code:
    #!/bin/sh -vx
    Change your crontab to pipe all output to a log file:

    Code:
    */5 0-23 * * * bash $HOME/.cronjobs/backups/abw_filecollect.sh > $HOME/abw_filecollect.log 2>&1
    Then look at the log for errors or post the log.

  3. #3
    Linux User Agent-X's Avatar
    Join Date
    May 2005
    Location
    Dimension X
    Posts
    261
    I believe it has to do with the rc.local file.
    Rc.local runs as root during bootup.
    WHile in terminal, I was the user, not root.

    I assume that's the problem, because when I reloaded the cronfile as the user, it executed correctly.

    So, anyway I can fix this problem, or should I make it a startup program in gnome?

  4. #4
    Linux Guru bigtomrodney's Avatar
    Join Date
    Nov 2004
    Location
    Ireland
    Posts
    6,133
    I assume you are not leaving the $HOME variable as a variable in the cron script?

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2006
    Location
    Portsmouth, UK
    Posts
    539
    As bigtom says, $HOME is a no no.

    I'm not sure if ~ will work, I usually give the full path to scripts in cron.
    RHCE #100-015-395
    Please don't PM me with questions as no reply may offend, that's what the forums are for.

  6. #6
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by matonb View Post
    As bigtom says, $HOME is a no no.

    I'm not sure if ~ will work, I usually give the full path to scripts in cron.
    ~ should work in bash and ksh (as will ~username for any other user's home directory) but I concur that using fully qualified pathnames is The Way To Go with cron jobs - never assume a useful environment and you won't go far wrong.

Posting Permissions

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