We have scheduled a Windows Task to kick off some custom code (as an executable) that checks for existence of a file every 15 minutes.
If the file is eventually found, our service does some processing on it. Here's the rub: after the file is processed, the business requires that Task Scheduler stops these 15-minute checks until the next day.
Is there a way to insert logic into Windows Task Scheduler to stop running its trigger once some condition is met? Or is there a better way to architect this process?
We are using Windows Server 2008 R2 Standard SP1 to run this.
How to end a scheduled task:
A. To terminate a currently running task (i.e. one that shows up in Task Scheduler -> "Display All Running Tasks) from the command line:
schtasks.exe /End /TN "My task name"
B. To stop a scheduled task from being triggered to run in the future:
schtasks.exe /Change /TN "My task name" /DISABLE
C. To re-enable the task:
schtasks.exe /Change /TN "My task name" /ENABLE
More details are available by typing: schtasks.exe /Change /?
D. To ask a process to terminate (here: Notepad, but use the name displayed in Task Manager -> Processes\Image Name) that does not terminate when you run A. above:
taskkill.exe /IM Notepad.exe
E. To forcefully terminate a process that does not terminate when you run D. above:
taskkill.exe /F /IM Notepad.exe /T
Note: Using taskkill isn't a clean way of ending a process. You can read more in this post and in the article it links to.
How to set this up in Task Scheduler for your described situation:
Instead of having your task run the custom .exe directly, change it to run a script (e.g. a batch file, PowerShell script, Python script, etc.) which in turn:
Triggers the custom exe, then
Tests whether the file was found and "processed", then
Invokes command B. above to stop it from running again.
Add a second task that runs every morning (check "Run whether user is logged on or not) that re-enables the scheduled task by invoking command C. above.
Note: To invoke schtasks.exe requires elevated privileges, so set the task that executes that command to "Run with highest privileges".
How to detect that the file has been processed:
Have your custom .exe to add a Registry entry when it ran successfully. Your script can look for that entry using e.g. reg.exe (type REG QUERY /? for details).
There are other ways (e.g. posting a Windows event, sending a message, etc.), but using the Registry is an easy mechanism for a simple script to use.
Don't disable the task from directly your .exe. Keep the application and its invocation separate. That will also save you from having to recompile if you want to disable the task differently later.
Related
I am trying to schedule a bcp job in server 2012 task scheduler. My batch file works fine when I double-click on it. It includes this line:
bcp "SELECT * FROM [TIME_KEEPER]" queryout D:\DATA\TIMESHEET_DBASE.csv -S 10.0.0.54 /c /t, -T
The file is created from the command line. Scheduler has:
Action: start a program
Script: D:\DATA\myBatch.bat
Start in: D:\Data
I am using the same account for other scheduled tasks and they are running fine.
Sounds like a security issue.
Do any of the other scheduled tasks use the bcp executable and connect to the same server pulling data from teh same table? If not then you have to track down the security being used.
When you double click your batch, it is run as the account you are logged in as. Is it possible that your scheduled tasks are running as a different account than what you are logged in as?
As a test, are you able to log in to the windows server using the same account the task scheduler is executing the tasks (assuming they are different)?
Should get similar error at that point.
Just a start.
I need some help with this.
I've scheduled a task on Windows Task Scheduler, that calls the command SQLCMD using the parameter: -i "path\script.sql"
My problem is this:
My script starts with: USE [DatabaseX]
DatabaseX does not exist on the server, so the script fails.
But the Scheduled Task ends with a Successful result, even if the script fails.
I need to see that the last run failed in the scheduled task. Or other place, but somewhere...
Is this possible?
Thanks,
I'm trying to run a batch file from Windows 8 Task scheduler and I'm getting an access denied error on the command prompt for the following command line.
echo "Hello World" >> C:\File.txt
However, if I tick the checkbox "Run with higest privileges" under the task scheduler then it works fine.
The question is why do I require to tick the checkbox when I can run the batch manually without the "Run as administrator"?
A couple possibilities:
If a task is registered using the Administrators group for the security context of the task, then you must also make sure the Run with highest privileges check box is checked if you want to run the task.
If you are using the S4U functionality, make sure the Logon as batch job policy is set for the user.
Make sure the user account the task is using has write access to the C:\ directory
By default in modern Windows you cannot write to the root directory. Use "%temp%\file.txt" instead.
Currently working on a WCF service running as a windows service.
I've made an installer and written a little script which adds a user account for it to run under and then registers and starts the service under than account. And a build step in visual studio to copy it to a discrete folder so we're not running directly off the solution. All works fine.
However, as I extend and improve this service, testing it is becoming a bit of a pain. Each time I want to check my changes I have to stop the service, rebuild, re-start the service and re-attach visual studio to the process before I can step through.
Just wondering if there's a quicker or better method of doing this without writing some proxy host for all the services? Unit tests won't cut it in this instance because the service needs to do system-ish stuff like connecting to Active Directory endpoints.
I know this question is ancient, but I recently solved part of this using build events as follows:
Pre-build event:
if "$(ConfigurationName)" neq "Debug" goto NoStop
echo Attempting to stop windows service MyService
net stop MyService > MyService.log
if "%errorlevel%" neq "0" (call )
type MyService.log
:NoStop
Post-build event:
if "$(ConfigurationName)" neq "Debug" goto NoStart
findstr "/c:stopped successfully" MyService.log >nul
if "%errorlevel%" equ "0" net start MyService
(call )
:NoStart
Features of this approach:
The configuration is checked and if I'm not building in Debug, then the whole bit is skipped.
The NET STOP command will try to stop your service and the if command below it will swallow any errors that might occur. This is great if your service isn't installed or if it isn't running.
Since the service is stopped, the service EXE is unlocked and the build doesn't fail with those annoying "file is locked" errors.
The post-build event will only start the service if the pre-build event was successful in shutting it down. The FINDSTR command searches the log file created during the pre-build event for the string "stopped successfully." If it is found, the NET START is called. Either way, errors are swallowed by the (call ) on the next line.
This is working for me in Visual Studio 2015 running on Windows 7. Hope this helps somebody!
I am having issues with credentials being cached on a few of the laptops that area available to students at my university.
I have found that if i remove (remove not edit to NULL) a registry entry at
HKEY_CURRENT_USER\Software\Microsoft\EAPOL\UserEapInfo
this solves the problem.
Im also pretty sure that this will delete the registry entry through the command line
REG DELETE HKEY_CURRENT_USER\Software\Microsoft\EAPOL\UserEapInfo /f
What I need to know is how to make this line run when the machine is logged into.
A Scheduled Task maybe what you are looking for:
save your command into a .bat file somewhere
Go to Control Panel > Scheduled Tasks > Add Scheduled Task
config the scheduled task to run your .bat file, and schedule it to run "when I logon"