0

I need to execute a cronjob to run once for 7 days alone. I have tried like this:

 0 0 * * 0-6  myscript.sh

It gets run for once a day and running every day of the week since I gave as 0-6.

But I need to run a job for one week alone not followed by multiple days. I do not want to mention the date of the month, since I need it to run at various times.

(or)

Using special command time in cron, I use @daily option alone

How can I set the job to run for 7 days alone?

1
  • 2
    use at jobs for this. Commented Sep 19, 2016 at 8:00

1 Answer 1

3

You cannot do this with cron. However, you can do it with at. See man at for more details. Here is a sample script to submit the job myscript.sh for the next seven days, running at midnight:

for n in {1..7}
do
    echo 'myscript.sh' | at midnight + $n days
done

You can list the submitted jobs with atq:

atq | sort -n
3       Tue Sep 20 00:00:00 2016 a roaima
4       Wed Sep 21 00:00:00 2016 a roaima
5       Thu Sep 22 00:00:00 2016 a roaima
6       Fri Sep 23 00:00:00 2016 a roaima
7       Sat Sep 24 00:00:00 2016 a roaima
8       Sun Sep 25 00:00:00 2016 a roaima
9       Mon Sep 26 00:00:00 2016 a roaima

Each job will run precisely once at the date and time specified, and will then be deleted from the queue.

If you need to remove a job before it has started, the atrm command will do this. For example,

atrm 3

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .