Task scheduler, terminate started program in windows - windows

At my work we have a set up with the task scheduler periodically starting a java program to read mails.
the task is scheduled to run every minute and it calls a .bat file which starts the java program.
Now the problem.
Once in a month or so the jave.exe process doesn't end properly, so the next minute when it tries to run I get:
Task Scheduler failed to start "\XXX Jobs" task for user "NT AUTHORITY\System". Additional Data: Error Value: 2147750687.
And then I get that message every minute until I terminate the java.exe from the task manager.
Now my question, in task scheduler there are some options to choose.
Under settings there is "If the task is already running, then the following rule applies"
If I then choose "Stop the existing instance"
Will this stop the java.exe or just the task? Or is there a better way.
Some advice would be welcome.

At the end of your batch file kill the task.
taskkill /im java.exe
java.exe could be whatever process you are planning on killing. You can add multiple lines of tasskill in a batch file to kill multiple processes at a time.
taskkill /im java.exe
taskkill /im explorer.exe
taskkill /im svhost.exe

"Stop the existing instance" : The Task Scheduler service will stop
the instance of the task that is already running, and run the new
instance of the task.
that means it will kill the process that the scheduler has launched, cmd.exe in your case, as you told us your program is started from within a batch.
Now, i'm not familiar with java but i guess that stopping your batch will kill the java process that where launched if it is not started as a service.
The scheduler will then run another cmd process and execute your batch once again

Better solution would be fix the java hang. As the process starts every minute then in your java program should have the code to automatically exit if it takes more than a minute to complete the task. Another way would be in the batch file you can kill the process after 1 minute. Use the taskkill /im to kill the process.

Related

Windows Task Scheduler - Run task nonstop

I have a task/script, that I need to run nonstop.
However, I have set up the task, and I keep trying to run it. It does run, but after I refresh the tasks, it stops, says "Ready," and also says "Task Completed."
The thing is, I was able to do this before, and it continued running.
My question is, how can I make Task Scheduler continue to run nonstop? Any help would be appreciated.
The Task-Scheduler can not automatically wait on every derived process / application beeing executed. Once the main handle "finishes", the task is considered completed, and it's status turns to ready, even if there is still stuff running in the Background.
Example:
Consider two tasks, with the following actions:
Program cmd.exe, Parameters: /c pause
Program cmd.exe, Parameters: /c "start cmd.exe /c pause"
In case 1, the taskscheduler will say "running", until the cmd window closes. (Which is what you are asking for?) Case 2 invokes an action from within the first cmd window. So, even if the SECOND cmd-window remains open, the process created by the taskscheduler itself terminates - hence it considers the execution completed and switches back to "ready".
So: Make sure, your main-process started by the task scheduler (and maybe invoking other processes) does not end, before all the work is done.
in the above example, this could be achived like
Program cmd.exe, Parameters: /c "start /wait cmd.exe /c pause"
window 2 will be "paused" and window 1 waits for window 2 to close, leaving that process in an active state. So, the task scheduler keeps displaying "running".
to provide a less generic answer, you should update your question with more information: What task are you executing? What kind of script is it? What are you task settings, and what is your expected behaviour?

Some Nircmd command not working on task scheduler

i currently try to make script that prevent user to mute volume using nircmd .
but the script is fine when i run manually but when i run using task scheduler but only 2 line of system sound script executed, i try to make 2 different script between system sound and application sound result still same only system sound script executed.
nircmd.exe setsysvolume 13104 -->Working on task scheduler and can manually run
nircmd.exe mutesysvolume 0-->Working on task scheduler and can manually run
nircmd.exe muteappvolume mstsc.exe 0 -->Not working on task scheduler and can manually run
nircmd.exe setappvolume mstsc.exe 13104 -->Not working on task scheduler and can manually run

what is considered an instance in windows task scheduler

If I have a handful of tasks scheduled that execute the same .exe file but have different parameters, are they all considered the same instance or different?
One of these tasks failed with output 0x41301 "task is already running"
tasklist /V shows each as separate PID if I run them manually in a cmd window.
So I changed all my tasks that trigger the same .exe but different params to "queue a new instance" if one is already running. However I'm not sure what is considered an instance, the entire command with parameters or just the executable?

Command Prompt doesn't close after scheduled task runs

I have a job scheduled to run at 9:30 every day using Windows task scheduler. The problem is after it runs, the command prompt stays open. Does anyone know how to get it to close?
The full text in the "Add arguments (optional):" field is:
C:\WinPython64bit\notebooks\TreasuryTest.py exit 0
I have searched all over, but most fixes are for use directly in the command prompt, and it seems to function differently from the task scheduler.

In batch programing can one command run before the previous command finishes executing?

In batch programing is one command waited until completed until the next one is run? What I mean is for example
net stop wuauserv
net start wuauserv
Since net stop wuauserv takes a while to complete is it given time to complete or do I need another command to wait until it completes?
The NET STOP command does wait (or timeout while waiting) for a service to stop or start.
You can check the %ERRORCODE% from the command to get more information about if there was a problem or if it worked as expected.
In general most system command line tools return control once they are done executing. A few specialized programs will call into other services or systems and may return control before execution is complete. You will need to check the docs for whatever you are trying to run, but generally processes exit once the 'task' they perform is complete.
In a batch file, all commands are run sequentially, and execution waits for the command to complete.
In your example, net stop wuauserv would complete before net start wuauserv gets run.
You could confirm that by running something you know will take a long time, such as
ping www.google.com
ping www.stackoverflow.com
and you'll see that the second ping does not start until the first completes.
In your case, yes the second command will not execute until the first finishes.
However, GUI apps will start up and return control the batch file.
For example,
PING localhost
NOTEPAD
DIR
The DIR command will execute even if NOTEPAD is still running.

Resources