Results 1 to 5 of 5

Thread: Can't get crontab to run a bash script

  1. #1
    Join Date
    Jun 2008
    Location
    southeast Alaska
    Beans
    353
    Distro
    Ubuntu 12.04 Precise Pangolin

    Can't get crontab to run a bash script

    I have been trying to get crontab to run a bash script. I have checked all the common errors, and can't figure it out. My crontab, which does end in a new line:
    Code:
    # m h  dom mon dow   command
    * * * * * /home/username/development/cron_test/hello_sh.sh
    * * * * * echo "hello from cron `date`" >> /home/username/development/cron_test/hello_sh_log.txt
    The script I am trying to run, which runs from the command line:
    Code:
    #!/bin/bash
    
    echo "hello bash! `date`" >> 'hello_sh_log.txt'
    echo "greeted bash."
    The last entries in syslog:
    Code:
    May 17 07:04:01 compname CRON[3750]: (username) CMD (echo "hello from cron `date`" >> /home/username/development/cron_test/hello_sh_log.txt)
    May 17 07:04:01 compname CRON[3751]: (username) CMD (/home/username/development/cron_test/hello_sh.sh)
    May 17 07:04:01 compname CRON[3749]: (CRON) info (No MTA installed, discarding output)
    Any suggestions?

  2. #2
    Join Date
    May 2010
    Location
    uk
    Beans
    9,324
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Can't get crontab to run a bash script

    Hi


    Here is how i did mine. I just changed the paths.
    Code:
    matthew@matthew-Aspire-7540:~$ pwd
    /home/matthew
    matthew@matthew-Aspire-7540:~$ ls hello*
    ls: cannot access hello*: No such file or directory
    matthew@matthew-Aspire-7540:~$ cat <<'EOF' > hello_sh.sh
    > #!/bin/bash
    > 
    > echo "hello bash! `date`" >> 'hello_sh_log.txt'
    > echo "greeted bash."
    > EOF
    matthew@matthew-Aspire-7540:~$ cat hello_sh.sh 
    #!/bin/bash
    
    echo "hello bash! `date`" >> 'hello_sh_log.txt'
    echo "greeted bash."
    matthew@matthew-Aspire-7540:~$ chmod 755 hello_sh.sh 
    matthew@matthew-Aspire-7540:~$ ls -l hello_sh.sh 
    -rwxr-xr-x 1 matthew matthew 104 May 17 22:17 hello_sh.sh
    matthew@matthew-Aspire-7540:~$ crontab -e
    I added this to the cron tab entry

    Code:
    # m h  dom mon dow   command
    * * * * * /home/matthew/hello_sh.sh
    Close crontab and it will update crons configuration.

    Code:
    matthew-Aspire-7540:~$ crontab -l
    # m h  dom mon dow   command
    * * * * * /home/matthew/hello_sh.sh
    matthew@matthew-Aspire-7540:~$ tail -f /var/log/syslog
    ...
    <snip>
    ...
    May 17 22:29:01 matthew-Aspire-7540 CRON[2732]: (matthew) CMD (/home/matthew/hello_sh.sh)
    May 17 22:29:01 matthew-Aspire-7540 CRON[2731]: (CRON) info (No MTA installed, discarding output)
       
    matthew@matthew-Aspire-7540:~$ ls hello*
    hello_sh_log.txt  hello_sh.sh
    matthew@matthew-Aspire-7540:~$ cat hello_sh_log.txt 
    hello bash! Thu May 17 22:17:07 UTC 2012
    matthew@matthew-Aspire-7540:~$
    Kind regards
    Last edited by matt_symes; May 17th, 2012 at 11:55 PM.
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

    Thinking about becoming an Ubuntu Member?

  3. #3
    Join Date
    Jul 2010
    Location
    /run/shm
    Beans
    823
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Can't get crontab to run a bash script

    Try prepending bash to it?
    Code:
    bash /home/username/development/cron_test/hello_sh.sh
    Also change the bash echo to point to an absolute path. I don't know 100% how cron works but it might be not setting $PWD or sets one under which you don't have write access.
    Last edited by Lisiano; May 17th, 2012 at 11:51 PM.
    Quote Originally Posted by Linus Torvalds
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

  4. #4
    Join Date
    Jun 2008
    Location
    southeast Alaska
    Beans
    353
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Can't get crontab to run a bash script

    Just a quick note to say that I resolved the issue. I believe it was a path issue, but unfortunately I forget the details now. Thank you both.

  5. #5
    Join Date
    May 2012
    Beans
    4

    Re: Can't get crontab to run a bash script

    Quote Originally Posted by japhyr View Post
    I have been trying to get crontab to run a bash script. I have checked all the common errors, and can't figure it out. My crontab, which does end in a new line:
    Code:
    # m h  dom mon dow   command
    * * * * * /home/username/development/cron_test/hello_sh.sh
    * * * * * echo "hello from cron `date`" >> /home/username/development/cron_test/hello_sh_log.txt
    The script I am trying to run, which runs from the command line:
    Code:
    #!/bin/bash
    
    echo "hello bash! `date`" >> 'hello_sh_log.txt'
    echo "greeted bash."
    The last entries in syslog:
    Code:
    May 17 07:04:01 compname CRON[3750]: (username) CMD (echo "hello from cron `date`" >> /home/username/development/cron_test/hello_sh_log.txt)
    May 17 07:04:01 compname CRON[3751]: (username) CMD (/home/username/development/cron_test/hello_sh.sh)
    May 17 07:04:01 compname CRON[3749]: (CRON) info (No MTA installed, discarding output)
    Any suggestions?
    When running Cron Tab make sure your bash script starts with a change to directory where you have your script with execution permissions:

    #!/bin/bash
    cd /home/pathtomyscript/
    my commands....

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
  •