ubuntu - Cron unable to run shell script

06
2014-04
  • oneonetwo

    I have been trying to get a shell script to run for hours using cron to no avail. I am trying to have the following bash script run every minute

    #!/bin/bash
    PATH=/usr/local/bin/ices:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    ps cax | grep 'ices' > /dev/null
    if [ $? -eq 0 ]; then
      echo "Process is running."
    else
      /usr/local/bin/ices /etc/ices/playlist.pls
    fi
    

    It checks if a process is running, and if not reloads it. It works as an executable (permissions are set to 755).

    My crontab looks like

        # m h  dom mon dow   command
        */1   *    *    *    *    /etc/ices/checkIces.sh
        */1   *    *    *    *    env > /tmp/env.output
    

    The second line is to check if cron will at least log out the env in the tmp folder. Does anyone see anything wrong with my current implementation?

  • Answers
  • MariusMatutiae

    Basically, this is a problem about getting enough info to perform debugging. I can offer the following suggestions.

    The script looks fine, except that I would modify the PATH command as follows, just out of caution:

       PATH=/usr/local/bin/ices:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH  
    

    You should include this statement

      exec >>${0}.out 2>>${0}.out
    

    into your small bash script. It redirects all output, standard and error, to a file which has the same name as the script, followed by .out. So, if your script is called *my_script.sh*, you will find all output into *my_script.sh.out*. Just make sure writing permissions allow the creation and modification of the file. You will find there error messages, which otherwise may get lost.

    Third, try running the problem with the at command, instead of cron, for the moment. The difference lies in the environment. If the program runs with at, but fails with cron, you will know that you have to import some feature of your environment. Hopefully, the second suggestion will give you some hint there.


  • Related Question

    crontab - Run a shell script using cron
  • Questioner

    I have this FeedIndexer.sh:

    #!/bin/sh java -jar FeedIndexer.jar
    

    Just to run FeedIndexer.jar which is in the same directory as the .sh, I would like to run it using crontab, so I did this:

    # /etc/crontab: system-wide crontab
    # Unlike any other crontab you don't have to run the `crontab'
    # command to install the new version when you edit this file
    # and files in /etc/cron.d. These files also have username fields,
    # that none of the other crontabs do.
    
    SHELL=/bin/sh
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    
    # m h dom mon dow user command
    17 * * * * root    cd / && run-parts --report /etc/cron.hourly
    25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
    47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
    52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
    01 01 * * * root run-parts --report /home/slosada/workspace/FeedIndexer/target/FeedIndexer.sh
    #
    

    But I don't know how to run it. Have I made any mistake?


  • Related Answers
  • whitequark

    You don't need the run-parts part. run-parts is used to run every script in a certain directory, and you only need to run one script, which is handled by cron itself. So, this should work:

    01 01 * * * root /home/slosada/workspace/FeedIndexer/target/FeedIndexer.sh
    
  • Justin Ethier

    Have you tried using single-digit hour and minute fields:

    1 1 * * * 
    
  • Michael Mrozek

    I'm going to wildly guess based on all the "it's not running!" that the job was intented to execute every hour, which is not what you wrote.

    01 01 * * * root run-parts --report /home/slosada/workspace/FeedIndexer/target/FeedIndexer.sh
    

    The minute and hour fields are both 1. That means when the current hour is 1 and the minute is 1, the job should run, which only happens at 1:01 in the morning. If you want the job to run every hour, do:

    0 * * * * root run-parts --report /home/slosada/workspace/FeedIndexer/target/FeedIndexer.sh
    

    This will run whenever the current minute is 0, which naturally happens every hour