Installing windows update after patch Tuesday - windows

The Saturday after each patch Tuesday (which is the second Tuesday of the month), I want to install updates via Puppet. I made a scheduled task for this, itself managed via a scheduled_task resource from the puppetlabs/scheduled_task module. I am using this trigger for that task:
$trigger = {
schedule => monthly,
start_time => '13:15',
which_occurrence => second,
day_of_week => [sat],
minutes_interval => '80',
minutes_duration => '560',
}
. However, on months like this one, where the second Tuesday comes AFTER the second Saturday, this trigger launches the task on the wrong day. How can I make the task fire on the first Saturday following the second Tuesday of each month?
It might be possible to count the days after patch Tuesday, but this would require Puppet to be able to get the date of every second Tuesday of the month (which it can't AFAIK).

The puppetlabs/task_scheduler module manages tasks that are to be run via Windows' built-in Task Scheduler service. Puppet does not itself run the tasks; it just registers them with the system service and manages their properties. This is a problem for you, because as far as I can determine, the Windows task scheduler does not support the type of scheduling you're looking for. Of course, puppetlabs/scheduled_task cannot provide for something that the underlying OS does not support.
Your alternatives include, but are not necessarily limited to:
Choose a different schedule for your task. For example, schedule the task for the third Saturday of the month, which will always follow the second Tuesday.
Use an Exec to perform the update, with a combination of conditional logic and a Puppet schedule for controlling the date and frequency of application.
It is true that the latter requires Puppet to perform some date computations. Puppet can do that, but you would probably need to write a custom function (in Ruby) for that purpose. Custom functions are accessible during catalog building, and they can use substantially all the capabilities of Ruby.

Related

CRON JOB - Schedule Laravel command for different timezones - Manage for different regions

I'm using Laravel 5.8 and I like to auto-generate Invoices should be generated auto from Monday - Sunday.
every Sunday night at 23:59:00
Let's say I have
"23:59:00 according to the region time zone".
Every store is related to a region, from the region table you'll find the time zone.
Questions
How should I set the CRON JOB so I can generate invoices automatically according to the list of timezone I will get from the table?.
I have two suggestions;
if you want it to be static, define each command with the corresponding timezone from your database. Different timezones may have same time such as Europe/Amsterdam and Europe/Berlin
$schedule->command('generate:report')->timezone('one-of-the-timezone')->at('23:59');
$schedule->command('generate:report')->timezone('another-timezone')->at('23:59');
$schedule->command('generate:report')->timezone('yet-another-timezone')->at('23:59');
$schedule->command('generate:report')->timezone('some-other-timezone')->at('23:59');
If you want it to be dynamic, make it run every 59. min hourly and try to match it with the timezones via using hour.
$schedule->command('generate:report')->hourlyAt(59);
Inside your command class;
public function handle(TimezoneRepository $timezoneRepository)
{
$timezones = $timezoneRepository->getUniqueTimezones(); // the unique timezones in your database - cache if you want
foreach ($timezones as $timezone) {
$date = Carbon::now($timezone); // Carbon::now('Europe/Moscow'), Carbon::now('Europe/Amsterdam') etc..
if ($date->hour === 23) { // you are in that timezone
$currentTimezone = $date->getTimezone();
dispatch(new ReportMaker($currentTimezone)); // dispatch your report maker job
}
}
}
With the dynamic one, you will hit to multiple timezones at one iteration(when generate:report is executed) as i said at then beginning.
one of the possible flaw may be; if the execution of getting timezones etc takes more than 1 minute you may be in 00:00 instead of 23:59. It is better to calculate your report asynchronous and cache the list of timezones to not face problems while executing this loop.
Another possible flaw;
According to wiki;
A few zones are offset by 30 or 45 minutes (e.g. Newfoundland Standard Time is UTC−03:30, Nepal Standard Time is UTC+05:45, Indian Standard Time is UTC+05:30 and Myanmar Standard Time is UTC+06:30).
If you want to cover any of these, then it is better to execute the command like this
$schedule->command('generate:report')->cron('14,29,44,59 * * * *');
and make both hour and minute comparison such as;
$date->hour === 23 && $date->hour === 59
You can use the timezone() method in your task Schedulding
Using the timezone method, you may specify that a scheduled task's
time should be interpreted within a given timezone:
$schedule->command('report:generate')
->timezone('America/New_York')
->at('02:00')

How to restrict windows tasks to weekdays only?

I have a task that triggers on "user session logon". I now want to restrict that task to fire only on weekdays, and being ignored on the weekend.
Is that possible?
Sidenote: I cannot use the trigger on schedule as I do not want to run the task periodically, but only on logon, and only on weekdays.
click Weekly (NOT Daily)
Choose the days you want
As far as I understand, this is not possible using the task scheduler alone.
You could use a piece of VBScript to achieve this.
Set up a file, e.g. mytask.vbs, like this:
If DatePart("w", Date, vbMonday) < 6 Then
Set Shell = CreateObject("WScript.Shell")
WScript.Quit(Shell.Run("C:\Windows\System32\notepad.exe", 10, True))
End If
Replace notepad by the task you actually want to run. What this things does is: It checks whether the current day is Mo-Fr (this is done by specifying the start of the week as Monday, so DatePart will return values from 1=Monday to 7=Sunday, and then we're checking if it's below 6), and if yes, it runs a certain program, waits for it to finish and forwards its exit code. (The magic number 10 here means that it will respect whatever setting for window display (normal, maximized, minimzed) was passed by the task scheduler, if any, and also forward it to the program.)
Then you can create a scheduled task with a logon trigger only, which runs wscript.exe /e:vbscript c:\path\to\your\mytask.vbs. That's it!

Run a job every other monday

I would like to use Heroku scheduler to run every OTHER Monday. However, it only runs hourly, daily, every 10 minutes.
I read this...
How can I schedule a 'weekly' job on Heroku?
However, I'm not sure what code can be used. I think I can figure out every Monday, but not every OTHER Monday.
thanks
As you get more complicated, I'd recommend checking out scheduling gems. If you want to stick to vanilla Ruby, look at a combination of monday? and cweek, which tells you the week number in the current year. Run your job on Mondays in even-numbered weeks.
date = Date.today
date.monday? && date.cweek.even?
Note that cweek can return 53, since 365 isn't divisible by 7 and it has to handle that last, partial week. The new year's first week will be 1 (it doesn't count from 0), so you have to either skip a week or do two runs in a row when Monday falls in week 53.

Schedule a task with 'AND' and 'WAIT' conditions with Windows Tasks scheduler

I want a task to start when both of these conditions are met:
Once a week, every week. like every friday at 3PM
When a session is started and not locked
I know how to do 1 OR 2, but I'm not able to schedule the task only when the 2 conditions WILL meet.
In other words, IF the session is locked at 3PM, I don't want the task to start now, but I don't want it to be skipped! I want the task to wait for the session to be opened (or unlocked) and then to start immediately.
So if a session is unlocked only Saturday morning and not Friday, then the task shall execute on Saturday; and then it will repeat the same process next Friday.
I am not sure how to correct specify the conditions such that:
If its Friday at 3 PM and the session is unlocked; the task should run.
If the interval (Friday at 3 PM) has passed, but the task has not run (because the session was not available); then it should run at the very first instance the session is available, irrespective of the date and time.

Scheduling a task to run at the end of every month. (Windows Server 2003 - Scheduled Tasks)

I need to schedule a task to run on the last night of each month, on a Windows 2003 Server.
I see that you can schedule it to run on the "first or last Mon-Fri", or even on the nth day of each month - but, not how to get it to run on the last day (regardless of day of the week or number).
Thanks in advance.
Note: I did check "How do you schedule tasks in Windows?", etc...
Looks like you have to set up multiple schedules for your task. One schedule for the months with 31 days, another for those with 30, and one more for February. See this: http://support.microsoft.com/kb/936627
I do it a little differently - I run one task every day but since the task is in vbscript - I do this:
DIM datecur, datefut
datecur = DATEPART("m",NOW())
datefut = DATEPART("m",NOW()+1)
If (datecur <> datefut) then
'insert code you want to run here
end if
Simple and it works - hope this helps someone
As of November 2022, much newer Windows version (2019)
I see this option in Triggers:
WinScheduler Task Triggers
Begin the task: On a schedule
Settings:
Monthly
On: Last > then select All weekdays

Resources