We’ve started experiencing a strange problem at work recently with some Windows Scheduled Tasks that are run manually by our testing group on our test server (a Windows 2003 server). They will run a scheduled task, which runs a batch file which in turn runs a Java application, and the batch file will run to completion but the task in the Scheduled Task window continues to show a status of “Running”. Right-clicking the task and selecting “End Task” doesn’t work - we have to go into the Services window on the server and stop and restart the Task Scheduler service in order to get the status to reset.
Looking in the log file for the Task Scheduler, I see what looks like a file sharing conflict when it is trying to update the task, but it’s one of those generic sort of error messages that really doesn’t give you much information.
"MyScheduledTask.job" (taskBatchFile.cmd)
Started 11/2/2009 2:10:55 PM
"MyScheduledTask.job" (taskBatchFile.cmd) 11/2/2009 2:11:00 PM ** WARNING **
Unable to update the task.
The specific error is:
0x00000020: The process cannot access the file because it is being used by another process.
"MyScheduledTask.job" (taskBatchFile.cmd)
Finished 11/2/2009 2:11:00 PM
Result: The task completed with an exit code of (0).
This just started happening a couple of weeks ago and seems to only be happening with a couple of specific tasks, and it doesn’t happen every time the jobs are run, but maybe once every two or three days. I’ve tried looking up the problem on Microsoft’s support site but I’m not finding much that’s helpful.
Start cmd.exe as the user that the scheduled task runs under. You can use “runas /user:foo * cmd.exe” to do this from another account, but actually logging in with the user is better if you can (due to some environment settings that don’t quite work right in a runas context).
Copy and paste the Run line from the scheduled task to the command line, and see what actually happens when it runs.
My guess is that the batch file may run to completion, but some of it’s child processes (most likely the java app or the java vm) is not terminating for whatever reason. Windows usually doesn’t care about “zombie” or orphaned processes the way that *nix does, but the task manager service might.
That’s just a WAG, though. More information gathering is needed.
Just speaking to the general case, though – the windows task scheduler doesn’t work well (if it all) with any “interactive” program, even if that program doesn’t actually require any user input. That is, if the program uses any GUI functions at all, you’ll get unpredictable results from running it with the task manager. If your java app is CLI-only, then it shouldn’t be an issue, but you may have problems with it if it’s java GUI app.
Seconding child processes. Sometimes you can change your batch files from this
>program.exe
to
>start program.exe
That will spawn it into a new cmd shell instead of locking up the original batch process.
Java is kinda a pain. How are you launching it? Is it GUI? The one Java app I support starts via IE so it opens a IE window and then opens a Java window. I use AutoHotKey to run and close it, specially the WinClose() command to cleanly close the IE Window, which then in turn close the Java app properly. If you have hanging Java icons in your system tray then youre not closing it correctly.
>0x00000020: The process cannot access the file because it is being used by another process.
Something is open when you are starting your job. Close it.
Was this problem ever resolved? All of a sudden, the exact problem Shoeless described has shown up on one of my servers. I also have all scheduled tasks running under one administrator account. Don’t know what changed but, like Shoeless said, the error shows up out of the blue once every day or so and essentially locks the scheduled task in a Running state.
Actually, the way we eventually “resolved” the problem was to go into the Services window and stop and restart the Task Scheduler service whenever it happens. Oddly enough this has never happened in our production environment where we have a couple of dozen tasks running regularly. (Although we do try to stagger the schedules so that multiple tasks aren’t running at the same time.) It has only ever happened on a test server with tasks that are disabled and run manually by a tester.
Thanks for the link richblr - I’ll have to check that out next time it happens.