Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to write a crontab entry to run every 30 minutes for the last week of every month. I know to do the final Sunday for instance the cron is:

0 0/30 * ? * 0L

When I try to comma separate the weekday field like so:

0 0/30 * ? * 0L,1L,2L,3L,4L,5L,6L 

I get an error:

Support for specifying 'L' with other days of the week is not implemented

Is there a way to short hand this crontab expression, or do I need to make 7 separate crontab entries each with a different day?

share|improve this question

migrated from stackoverflow.com Mar 31 '12 at 18:50

This question came from our site for professional and enthusiast programmers.

    
do you need last week of month starting from Sunday or just last seven days of the month? –  Dada Mar 31 '12 at 19:15
    
Which cron? There's many: vixie-cron, cronie, dcron, fcron, mcron, … –  ephemient Mar 31 '12 at 19:33

2 Answers 2

As the error message says you can't do this. You'll have to have your script check if today is within the last week of the month and exit/proceed as required.

#!/bin/bash

MonthDays=$(echo $(cal) | awk '{print $NF}')
Today=$(date '+%d')
let DaysLeft=MonthDays-Today

if [ $DaysLeft  -gt  6 ]
then
    exit 1
fi
echo "Run the rest of the script"

Alternatively you could use the script below in your crontab - save it as last-week

#!/bin/bash

MonthDays=$(echo $(cal) | awk '{print $NF}')
Today=$(date '+%d')
let DaysLeft=MonthDays-Today

if [ $DaysLeft  -gt  6 ]
then
    exit 1
fi

Use it like this

0,30 * * * * last-week && your-script

Which says run your-script if the last-week script exits with a 0 status.

share|improve this answer
    
That works for last 7 days of the month not the last week. Though it is ambiguous what to call 'last week' –  Dada Mar 31 '12 at 19:53
    
@Dada: Given the OPs defective crontab spec 0L ... 6L and their later statement ... or do I need to make 7 separate cron ... It seemed fairly clear to me that they meant last 7 days of the month. –  Iain Mar 31 '12 at 20:06

And if you want real 'last week' (as from last Sunday of the month to whatever it is left), make some changes to @Iain's script:

#!/bin/bash

Today=$(date +%d)
LastSun=$(ncal | awk 'NR==2 { print $NF }')

if [ $Today -ge $LastSun ]
then
    exit 1
fi

Use it in a same manner.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.