Search code examples
croncron-taskcronexpression

How can I set up a cron task that stops after a certain date?


I would like some help to setup a cron job to run every 10 minutes and stop on next Monday at 00:00:00.

0 0 30 3 * /var/www/html/reports/Test.php

Solution

  • There is no such facility in cron per se. But you can achieve your goal as explained below. I have not put exact commands - but this is the idea that will work.

    1. Don't place your cron job in a crontab. Place it in /etc/cron.d with a file name like: /etc/cron.d/<your login name>-test-reports.php. Using your login name as part of the script file name reduces the possibility of file name conflicts. Place time specification and the command you want to run in the script. This may need root privileges because /etc/cron.d is rw only to root user.
    2. Figure out the day of next Monday. Say, if it is 30-March-2015 schedule one at script to delete your cron file from /etc/cron.d on the date and time of your interest. You can also determine next Monday's date using command: date --date "next Monday" to avoid hard coding a date in your at script.