0

I need to schedule a job to run on the 25th of each month. If the 25th falls on a weekend I need the job scheduled on last Friday before the 25th. The Windows Task Scheduler doesn't have an option for this kind of task. Can anybody tell me what I should do?

John Gardeniers
  • 27,458
  • 12
  • 55
  • 109
user77709
  • 3
  • 1

3 Answers3

1

A wrapper that evaluates such conditions.

If you know shell scripting, you can make it using gnu date.

If you know Perl, try Time::Piece (in core since Perl 5.009).

Then call the wrapper from cron.daily, calculate a date is a light daily task for a machine.

poisonbit
  • 827
  • 5
  • 6
1

Poisonbit's answer is basically correct. If the scheduling functions built-in to the Windows task scheduler can't provide the type of specific schedule you need, you'll need to work around the limitations yourself. His suggestion is to execute your job from a script that the task scheduler will run on a simple daily schedule. But the script will be smart enough to only execute the "real" job on the proper day. I've taken the liberty of providing a PowerShell based sample you could use.

$today = Get-Date
$d = $today.Day
$wd = $today.DayOfWeek

if ( ($d -eq 25 -and $wd -ne "Saturday" -and $wd -ne "Sunday") -or (($d -eq 23 -or $d -eq 24) -and $wd -eq "Friday") )
{
    "time to execute my job"
}

All it does it check the current date. If it's the 25th and not Saturday/Sunday or if it's Friday and the 23rd or 24th, it will print the "time to execute my job" message which you would replace with the command to actually execute your job.

**Updated with better logic thanks to Paul*

Ryan Bolger
  • 16,755
  • 4
  • 42
  • 64
0

Ryan Bolger has got it for the Friday condition...

If the 25th falls on a Monday through Thursday though, you'll still want to run the program on the 25th, so change his conditional to

if (($day -eq 25) -or ($weekday -eq "Friday" -and $day -ge 23 -and $day -le 25))

Note: I've never done PowerShell scripting, I'm just guessing on syntax from the code Ryan provided.

Paul Hanbury
  • 101
  • 1