Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, I was wondering if I can get some help with trying to run a script via crontab on Ubuntu Server 10.04. In the script called simplescript.sh, I have the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    9

    issue getting a script to run via crontab


    Hi,

    I was wondering if I can get some help with trying to run a script via crontab on Ubuntu Server 10.04.

    In the script called simplescript.sh, I have the following:

    #!/bin/bash
    sudo cp ~/Documents/hello_world hello_world1

    and that's it. a really simple script.

    In the crontab, I have written as follows
    00 13 * * * root /home/user1/Documents/simplescript.sh

    and then saved it.

    After 1pm, I went to check to see if it worked and I found it did not. I did not see hello_world1 file being copied.

    Could anyone tell me where I did it wrong.

    Thank you so much

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    663
    Since you have a username inthe crontab, you must be using the system crontab rather than your own user's crontab.

    If you at using your own user's crontab, remove "root" as all jobs will run as you.

    Have you changed the permissions on your script to be executable?

  3. #3
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    626
    The way you wrote it, you don't need sudo. You told the script to run as root.

    If you invoke sudo though, you will need to ensure that you can run the script with sudo WITHOUT a password. Also, you will need to comment out "requiretty" in your sudoers file. Otherwise it will not allow you to invoke sudo.
    linux user # 503963

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Mar 2010
    Posts
    9
    Thanks alf55 for your reply.

    When I run $ls -l at ~/Documents, I got it as follows

    rwxr-xr-x 1 root root 91 2011-11-15 ... simplescript.sh

    So it's been given 755 permission.

    Then, I removed the root in the crontab as shown below

    30 09 * * * /home/user1/Documents/simplescript.sh

    This did not work either. I run out of clues as to why it does not run.
    Another question I'd like to ask is whether or not I need to out a ./ in front of simplescript.sh in the crontab. I am not sure if crontab recognises this is a script that it needs to execute.

    Thank you

  6. #5
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    626
    If you are using crontab, give a username you are going to invoke the script as:
    Code:
    0 13 * * * someuser /path/to/script
    Use of ./ will not work. As cron's present working directory will not be where your script is located. So give it the full path. The script is executable. If its not doing anything, something is wrong with your script then.

    Check messages, or cron log to ensure it is indeed firing off the cronjob.

    And if you are using sudo, read my first reply.
    linux user # 503963

  7. #6
    Just Joined!
    Join Date
    Mar 2010
    Posts
    9
    Thank you scathefire for your replies. Sorry I missed your first reply. For some unknown reason, I did not get to see your reply when refreshing my firefox browser.

    Anyway, it works now. Thanks for your tips and help. The problem was in the script, I did not make it clear with the path to the script.

    before:
    sudo cp ~/Documents/hello_world hello_world1

    After:
    cp /home/user1/Documents/hello_world /home/user1/Documents/hello_world1

    Apparently it doesn't recognize the ~ character. Do you happen to know why.

    Anyway, it worked like a charm after the amendments. Thanks once again.

  8. #7
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    626
    If I had to take a guess, it would probably have something to do with environment variables like that '~' are not properly passed inside of cron.

    But that's just my guess.
    linux user # 503963

Posting Permissions

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