Page 3 of 3 FirstFirst 123
Results 21 to 27 of 27

Thread: Getting script to run in cron

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

    Re: Getting script to run in cron

    Move "2>&1" to the end of the crontab entry

  2. #22
    Join Date
    Jul 2009
    Beans
    426
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Getting script to run in cron

    The command to run your script from cron should be
    Code:
    /home/trevor/backuplogs/scripts/dailybackup >>/mylogs/diagnostic  2>&1
    Mark your thread as [SOLVED], use Thread Tools on forum page.

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

    Re: Getting script to run in cron

    I'm an idiot.

    The script didn't have execute permissions, and it called sudo.

    Have now fixed these and I'm experimenting - will report back.

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

    Talking Re: Getting script to run in cron

    Quote Originally Posted by btindie View Post
    The script could be simplified and rewritten in the following way: ....

    ...
    Schedule the script to run via cron with user trevor. When it runs it will re-execute itself as root. To be able to do that without having to enter a password you'll have to edit /etc/sudoers with visudo adding the following lines to allow user trevor to run the backup script as root.
    Just tested this, and (fingers crossed!) I think it is all working.

    Fantastic!

    Could I just trouble you with a couple of queries, because while I am familiar in general with programming, I am a bash newbie...

    what is the syntax in
    Code:
    if [ "$(id -u)" != "0" ]; then
        sudo "$0"
        exit $?
    fi
    I understand that this is meant to re-execute as root, but I don't understand how it works.

    What does the following? I don't get what the bracket symbols do but it seems to be some sort of existence check for the directory?
    Code:
    [ -d /media/Elrond ]
    and I'm guessing the following is a kind of "then" statement to make the directory if it doesn't exist?
    Code:
    || mkdir /media/Elrond
    Many thanks for your help btindie, I honestly didn't think I'd find a way out of this

    Trevor

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

    Re: Getting script to run in cron

    Quote Originally Posted by Trevor Burton View Post
    Just tested this, and (fingers crossed!) I think it is all working.

    Fantastic!

    Could I just trouble you with a couple of queries, because while I am familiar in general with programming, I am a bash newbie...

    what is the syntax in
    Code:
    if [ "$(id -u)" != "0" ]; then
        sudo "$0"
        exit $?
    fi
    I understand that this is meant to re-execute as root, but I don't understand how it works.
    That checks to see if root is running the script, and if not, runs it with sudo.

    What does the following? I don't get what the bracket symbols do but it seems to be some sort of existence check for the directory?
    Code:
    [ -d /media/Elrond ]
    and I'm guessing the following is a kind of "then" statement to make the directory if it doesn't exist?
    Code:
    || mkdir /media/Elrond
    Many thanks for your help btindie, I honestly didn't think I'd find a way out of this

    Trevor
    As for that one, the -d part checks to see if a directory exists or not.

    It would look something like this:

    Code:
    if [[ -d /path/to/some/dir ]]
    then
    do this
    else
    do that
    fi
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

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

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

    Re: Getting script to run in cron

    http://tldp.org/LDP/Bash-Beginners-G...ect_07_01.html
    Here is a pretty in depth guide to BASH. The page linked is an explanation of the basic usage of "if", along with some of the primary expressions like the [ -d /file/path ] in your script. Very good info to keep bookmarked while learning the ways of BASH

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

    Re: Getting script to run in cron

    Thanks to all of you for your help (and the bash tutorial link).

    Not only do I have a working backup, but I actually understand how it works.

    Many many thanks.

    I'll switch the thread to SOLVED

Page 3 of 3 FirstFirst 123

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
  •