how from a console application detect "End" request from the Task Scheduler - scheduled-tasks

how in an console application detect that the user click on the "End" process in the task scheduler ?
i try to setup SetConsoleCtrlHandler, but unfortunatly it's seam to not work (but curiously it's work in the task manager)
i also try to catch the wm_quit or wm_close without any success ...
thanks for you help !

End process, not "end application"?
I believe you can't. End process does TerminateProcess, which cannot be handled.
This is an analog of POSIX SIGKILL.

Related

How do Windows task scheduler recognise if the task fails

I have configured a application in Windows task scheduler to run for every 5 mints.
Expected true , but the application returns false and I expect the windows task scheduler to recognise it & restart. But it's not happening so far. Scheduler keeps running.
Note : I have enabled the option "to restart if the task fails for every" as well in scheduler.
Pls let me know where do I make mistake in understanding. Thanks.
After couple of hours on trial & error, found that system event would solve the above issue. scheduler doesn't track the error code of ongoing application, rather it will only the status whether its running or not.

Listen for an event in powershell to run a function when a particular program is run?

Is it possible to subscribe to an event in powershell when a particular executable is run?
We have an application that hogs up memory and then causes the system to crash, and if I could attach an event that starts a timer when it starts running and just kills after a certain amount of time, that would fix the issue.
You can use task scheduler to trigger on an Windows Event.Task Scheduler Trigger
Then you can add your powershell script as an action. Even delay the task if you like
As Crusadin sugggests, have a script run when an eventlog entry is made. I have just done the same thing here in work.

How to restart an exe when it is exits in windows 10?

I have a process in windows which i am running in startup. Now i need to make it if somehow that process get killed or stopped i need to restart it again in Windows 10?
Is there any way. Process is a HTTP server which if somehow stopped in windows i need to restart it. I have tried of writing a power-shell in which I'll check task-list status of process and then if not found I'll restart but that is not a good way. Please suggest some good way to do it.
I have a golang exe; under a particular scenario my process got killed or stopped i need to start it up again automatically. This has to be done imediately after the exe got killed. What is the best way to achieve this?
I will give you a brief rundown. You can enable Audit Process Termination in local group policy of the machine as shown below. In your case, success audits would be enough. Please note that the pic is for Windows 7. It may change with OS.
Now every time a process gets terminated, a success event will be generated and written to the security eventlog.
This will allow you to create a task scheduler that triggers on the generation of this event that calls a script that would run the process again. Simple right?
Well, you might have some trouble setting that task up especially when you want to pass details about the generating event to the script. This should help you get through that.
You can user Task scheduler for this purpose. There is a option of "restart on failure" which can be selected and whenever your process get failed it will restart again.
Reference :- https://social.technet.microsoft.com/Forums/windowsserver/en-US/4545361c-cc1f-4505-a0a1-c2dcc094109a/restarting-scheduled-task-that-has-failed?forum=winserverManagement

unable to generate recorded script in gatling in CLI

When trying to use Gatling CLI mode, the gatling starts successfully and recording is also happening. But the problem is when stopping the recording. As mentioned by the documentation (https://gatling.io/docs/2.3/http/recorder/), it can be stopped either by CTRL-C or by killing the pid available in .gatling-recorder-pid file.
I have used the second approach. Though the recording is stopped successfully, it is unable to create the simulation file. After doing some trial and error the only understanding i have now is that unless CTRL-C is pressed, it can never create a simulation file and killing pid only stops the recorder just before the file creation. But i am unable to simulate the CTRL-C action in windows command prompt from java. Please help. Thanks in advance
The kill command sends a SIGTERM (termination) signal. The SIGINT (interrupt) signal is the one equivalent to Ctrl+C.
kill -SIGINT processPIDHere

Scala println doesn't work while termination in IDE

I'm writing a server that runs in a loop and terminates in case of SIGINT(ctrl-c) or terminate button pressed in the console window of Eclipse IDE. I want it to shutdown gracefully printing out termination logs. But the problem is that println doesn't seem to work while shutdown sequence triggered by pressing the terminate button in Eclipse IDE. Look at the simple code below:
object Test extends App {
println("start")
Runtime.getRuntime().addShutdownHook(new Thread {
override def run = println("shutdown")
})
synchronized { wait }
}
It works well with the command line scala tool. Both messages "start" and "shutdown" are printed when I hit ctrl-c. But the "shutdown" message isn't printed when I run it in Eclipse IDE and hit the terminate button of the console window. It just terminates silently. I've verified that everything else in the shutdown hook runs correctly while termination. It's only println that doesn't work.
Any idea about this? I need to print out termination messages for logging.
Thanks for your help in advance!
Shutdown hooks don't get run by Eclipse when shutting down a process: https://bugs.eclipse.org/bugs/show_bug.cgi?id=38016
That bug report is resolved as Won't fix
I realise that its an old bug and so maybe things have changed since then.
Try logging the shutdown messages to a file instead. Eclipse probably closes its end of the stdout pipe before the program is really terminated.
You can try the shutDownHook which scala provides
sys addShutdownHook(println("shutdown"))