Program does not run properly as Scheduled Task
Situation
I have a batch script that prepares some files, executes a program (.exe
) and then deletes said files.
This task should run hourly, so I'm trying to configure this using Scheduled Tasks. The problem is that the previously mentioned program does not run properly when invoked from the task (neither via the .bat
script, nor when calling the .exe
directly), but I don't get any warning or error messages in the logs.
Setup
The task is configured to run as a Windows Service Account that has all the privileges set properly. When using this account to logon via RDP, I can execute the .bat
and .exe
directly without problems, but still the task appears to do nothing. This is easily observed because the program always modifies a file, and the modified on timestamp does not change through the task.
In the scheduled task logs I get the information messages for the task starting a process, exiting, etc. The "result code", however, is 111
(tried to Google this without luck, the only association I get is "file name is too long", which is just completely irrelevant AFAIK). In the application logs, I get absolutely nothing.
What I suspect is the problem
The program is an old monstrosity that spawns some sort of splash screen (it's actually a normal window), even though the GUI is not needed because it requires no interaction and closes itself after operations. The window appears for about 2 seconds.
I suspect that this requirement for a GUI has something to do with the task failing, but I'm not sure. When I log in with the user that the task runs under (via RDP), no window appears when I start the scheduled task.
Edit about the GUI
I've built a very small C# executable that launches the program without the main window (using ProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden
). Even this way, the scheduled task still does not succeed to launch the program correctly, but the return code is now 0
.
Update
When I configure the task to say "run whether the user is logged on or not", and the run with highest privileges
option is unchecked, the error value is 2147943859
.
What can I do to troubleshoot?
OS = Windows Server 2008 R2 SP1
If more info is needed, please let me know in the comments.
I believe your problem has to do with either the permissions of the account being used to run the task, or the context of the account as exists when trying to run the task.
Test for Console Session Requirement
It's possible your .EXE must be run in Console
session (aka Session 0) on the computer. To test for this:
- Configure the task to Run only when user is logged on and specify a task start time of 2 minutes in the future
- Log on to the machine with the same user account used to run the task (preferably log on to the console session, either by physically being at the console or using a remote access program that gives access to the console. To confirm you are using the console session, from a Command Prompt run
QWINSTA
, observe theSESSIONNAME
column, and confirm the>
indicator is next toconsole
, in other words it should appear as>console
) - Wait for the task to run
If the task runs correctly, try scheduling the task with SCHTASKS.EXE
using the /IT
parameter. Failing that, you may have no choice but to configure the computer to automatically log on as your service user account and run the task as a startup program.
Check Permissions
Additionally, as I've already suggested, check the following to confirm the account used to run the task is properly permissioned:
- Grant the account the Logon as a batch job user right (Found in Local Group Policy at
Computer Configuration/Windows Settings/Security Settings/Local Policies/User Rights Assignments
) - Confirm the task is configured to Run with highest privileges
- Confirm the user has full NTFS permissions to all folders & files it must interact with. Make no assumptions; instead confirm by navigating to such file locations and using the
Effective Permissions
tab in the file/folder's Properties atSecurity > Advanced
Additional things to check/try
- Does the task require access to access to network resources? Things like mapped drives may be present when you logon with the user account, but depending on the server's configuration may not be present in the context of the user account when executed from Task Scheduler.
-
Add some logging to your batch file. After every line it executes, have it write some output to a log file so you know where it's getting stuck. For example:
@echo off echo Line 1 >> "C:\MyLog.txt" "C:\My Folder\myOldProgram.exe" echo Line 2 >> "C:\MyLog.txt" DEL somefile.dat echo Line 3 >> "C:\MyLog.txt"
Try running your .EXE with
START
, for exampleSTART "myTitle" "C:\full\path\to\my.EXE"
I'm responding to an old post in case it helps someone else. I had the same issue. The event log said the program completed normally, but not even the first line of code would write to the log for me. It ended up being the "Start In" option in the Task Scheduler. It occurred to me that the program ran fine from the command line when I was in the current directory. There are manifest files and other dependencies in the same directory. So if you tell the scheduled job to start in the same directory as the EXE, you may get favorable results. It was the solution for me.
maybe this helps you?
https://stackoverflow.com/questions/6939548/a-workaround-for-the-fact-that-a-scheduled-task-in-windows-requires-a-user-to-be
We had a similar problem and your only solution was that we made a special account on the server with autologin. So if the task ran under the already loged in user our .exe worked well...
i know this is not a very nice solution but for us it was the only thing that worked. i don't know if this works for you... (But with this work around you have to check if the user is really logged in all the time...)