Delay in task scheduler job - powershell

I have a job that runs every 15 minutes and gets a datetime. The batch file contains the following PowerShell command to get the current datetime minus 15 minutes:
for /f "delims=" %%a in ('"powershell [DateTime]::Now.AddMinutes(-15).ToString('yyyy-MM-ddTHH:mm:ss')"') do set "dt=%%a"
I write the results of the PowerShell command in my logs. I've been able to notice that every 5 or so job runs, there is a small delay of anywhere between 1-7 seconds. For example, if the job is scheduled to run at 15:00:00, and there is a delay of 5 seconds, my logs will show 14:45:05 as the datetime returned by the PowerShell command.
What could be the reason for this delay? Perhaps the PowerShell command takes longer than expected sometimes? Or could it be that the OS is not running the task on time because it might be struggling to handle resources?
Please let me know if I should provide any more info - I'm having a hard time to be less general/open-ended with the question.

Related

Can a PowerShell script be dependent of another script's execution?

I have a situation where I want to make the execution of my scripts smarter. I have a set of scripts that execute at a given time, but because sometimes the input files are not posted at the correct times the scripts run into errors and get unexpected results. so one of the solutions I was thinking of is to make the execution of the scripts dependent of each other. Here is what I mean:
script 1 runs at 6 pm
validates that the file is there
if it's there, set a flag
the flag is active so execute script 2 at 9 pm
if it's NOT there, the flag is not set
the flag is not set so script 2 is not executed
Right now script 1 and script 2 are set with the Task Scheduler at those times, I checked the Scheduler for those type of conditions, but didn't find anything.
You can set triggers in Task Scheduler, like when an event happens for basically everything you can see in eventviewer.
I would suggest Write-Eventlog from the script which works on the file, and depending on the result the sched task would get triggerd.
I suggest you to have single script running every N-minutes on single scheduled task via Task Scheduler.
The master script will analyze activities and have all logical conditions those determine when and which external script to run. You can also have flag files.

How to get the elapsed time of a bunch of PBS Torque jobs?

I'm using PBS Torque to run multiple jobs. The idea is simple, each job works on a shunk of data. The PBS_Torque job_script to launch a job is called run_appli.sh
Here is the simple code (code 1) to launch 10 jobs
for i in 1 2 3 4 5 6 7 9 10 do; qsub run_appli.sh ; done
Indeed, I can monitor the execution of each of those jobs using qstat (see the command below) and have elapsed time of each job.
watch -n1 -d `qstat`
However, I am interested by the overall elapsed time. That means the time starting from when I launched all the jobs (code 1) and when the last job finished its execution.
Does anyone have an idea on how to do this ?
If you know the job id of the first job, you can look at it's ctime (creation time, or the time it is queued). You can then check the end time for the last job's comp_time. The difference between the two would be the total time elapsed.
qstat -f $first_job_id | grep ctime # Shows the first job's queued time
qstat -f $last_job_id | grep comp_time # Shows the final job's completion time.
If the last job's isn't completed, then the running elapsed time would just be the current time - the first job's queue time.

How do schedule a task in 1 hour using command prompt

I need some help setting up a batch file in Windows 7. I want the batch file to be able to create a scheduled task that would execute 1 hour from the moment I click it. I don't want to manually have to put the date and time in, just want it to schedule a task that will execute a set number of hours after I have run the batch file (I'm using 1 as an example).
Please can somebody help me out. I've been searching for an answer all day to no avail.
Recent versions of Windows come with a DOS utility called WAITFOR. Depending on how interactive you want your batch file, and whether it should run a single static command or run whatever you need at the time, you could easily make it work. Like for instance, you could create a batch file on your desktop and drag a program to it and drop it on the batch file. The first thing it would do is prompt for the number of minutes to delay, then it could run the program you dropped on it.
#echo off
setlocal enabledelayedexpansion
set /p _min=Enter the minutes to delay:
set /a _min*=60
waitfor /t !_min! delay
start "" %1
setlocal
Using the start command makes it possible to drop other things too, like a BMP or Word DOC. Anything that you can launch by double-clicking it from Windows Explorer should launch just fine.
After you enter the minutes to delay, just minimize the DOS window. It will close automatically after the delay and after it launches the program or file you dropped on the batch file.
invoke windows task scheduler directly from command line
schtasks /create /TN "Task Name" /TR script.bat /ST 18:00 /SD 21/03/2014 /SC ONCE

Edit triggers for a Windows Task Scheduler Task

We currently have several hundred tasks scheduled in Windows Task Scheduler on our server 2008 box. There are a number of tasks that are running every half hour indefinitely. Is there any way to use a script to change the triggers on those tasks so that they will only run every half hour M-F?
Yes, if you use the TaskService COM object. For some example scripts that use this object, see the following:
Rename Scheduled Tasks in Windows 7, Windows Server 2008, and Windows Vista
How-To: Use PowerShell to Report on Scheduled Tasks
Updating a Scheduled Task's Credentials
Script: https://gist.github.com/Bill-Stewart/363fdf761499a071439c8ac0c18dcd54
Using the schtasks.exe utility is rather limited past Windows XP/Server 2003.
Here are the allowed options for /SC:
/SC allows for the following frequency schedules:
MINUTE: 1 - 1439 minutes.
HOURLY: 1 - 23 hours.
DAILY: 1 - 365 days.
WEEKLY: weeks 1 - 52. (/D MON,TUE,WED,THU,FRI,SAT,SUN)
ONCE: No modifiers.
ONSTART: No modifiers.
ONLOGON: No modifiers.
ONIDLE: No modifiers.
MONTHLY: 1 - 12, or FIRST, SECOND, THIRD, FOURTH, LAST, LASTDAY.**
I keep editing this because I'm not happy with my findings...
I have looked into the SCHTASKS and noticed that you cannot CHANGE for the schedule through CMD.
From technet.microsoft
Changes one or more of the following properties of a task.
The program that the task runs (/tr).
The user account under which the task runs (/ru).
The password for the user account (/rp).
I am going to continue to do some research on this, and hopefully will edit this answer with a workable solution.
EDIT
If you look in C:\windows\system32\tasks you will find all of your scheduled tasks
They're not listed as XML, but they are indeed XML
What I think should be done is using either; VBS or C# application dig to each of the following Nodes
//Task/Triggers/CalendarTrigger to delete /ScheduleByDay and it's child node /DaysInterval
//Task/Triggers/CalendarTrigger to create the following:
<?xml version="1.0" encoding="UTF-16"?>
-<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task" version="1.2">
+<RegistrationInfo>
-<Triggers>
-<CalendarTrigger>
<StartBoundary>2013-03-13T15:20:00</StartBoundary>
<Enabled>true</Enabled>
-<ScheduleByWeek>
-<DaysOfWeek>
<Monday/>
<Tuesday/>
<Wednesday/>
<Thursday/>
<Friday/>
</DaysOfWeek>
<WeeksInterval>1</WeeksInterval>
</ScheduleByWeek>
</CalendarTrigger>
</Triggers>
If anyone can pick up and run with this - I don't think I'll be able to do much with this in the next week.
I found that if you use SCHTASKS /Create and use the same /TN name with a different /SC and /ST options it will overwrite the existing scheduled task with the updated schedule

Pausing a batch file for amount of time [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Sleeping in a DOS batch file
How to wait in a batch script
I have a program that is kicked off with a batch file.
The first module takes 10 seconds or so to initialize, and I want a way to "sleep" for 15 seconds before the second module is called, but I don't want it to require the user to hit a key like "pause" seems to require.
So, this is what I mean:
echo %PATH%
pause 10
echo %PATH%
In this example, I want there to be 10 seconds in between the echos. Is this possible? I've seen some examples using "ping 1.1.1.1" but it doesn't seem to work all the time correctly.
ping -n 11 -w 1000 127.0.0.1 > nul
Update
Beginner's mistake. Ping doesn't wait 1000 ms before or after an request, but inbetween requests. So to wait 10 seconds, you'll have to do 11 pings to have 10 'gaps' of a second inbetween.
If choice is available, use this:
choice /C X /T 10 /D X > nul
where /T 10 is the number of seconds to delay.
Note the syntax can vary depending on your Windows version, so use CHOICE /? to be sure.