ubuntu - Why does cron task run twice?

15
2014-01
  • flypen

    I have a task that should run each day in Ubuntu10.04. So I use crontab and copy the scripts of the task to the directory /etc/cron.daily.

    Here is the my crontab:

    # /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
    22 0    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
    48 2    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
    52 3    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
    #
    

    But I found that the task in /etc/cron.daily ran twice each day! I didn't know why so I added a script (mainly called pstree -a) in /etc/cron.daily to list all processes. And then I found that actually there were two cron sub-processes!

      |-cron
      |   |-cron
      |   |   |-(cron)
      |   |   `-sh -c test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
      |   |       `-run-parts --report /etc/cron.daily
      |   |           `-aa /etc/cron.daily/aa
      |   |               `-pstree -a
      |   `-cron
      |       |-(cron)
      |       `-sh -c root\011test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
      |           `-run-parts --report /etc/cron.daily
      |               `-aa /etc/cron.daily/aa
      |                   `-pstree -a
    

    Does anyone know why cron runs twice here?

  • Answers
    Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

    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