Computer Hope

Other => Other => Topic started by: DaveLembke on April 19, 2018, 04:19:09 PM

Title: Is Taskkill as graceful in ending a program as ( CTRL + C )?
Post by: DaveLembke on April 19, 2018, 04:19:09 PM
I am putting together an automated routine for my one server that has a MySQL database and I have been manually performing dailies on it in which I stop and start a service using CTRL + C to stop it and  starting it is a matter of running the EXE. In order to make this part of a scheduled task I need to pass CTRL + C to that console window -or- kill it some other way gracefully.

I have used Taskkill in the past to kill stuff forceably. Never used it to kill anything gracefully. Was curious if Taskkill /IM to target the application to end is graceful or a very destructive idea in substitution for CTRL + C?

I have Jitbit Macro Recorder that I can use to create an EXE to call that will pass a CTRL + C, but targeting this application has been a pain because its console location on the screen is not always in the same location to click and make focus as well as task manager under applications tab doesnt always have it listed in the same location to have the macro click and select it and use the Switch To button to set focus to that console window to pass the CTRL + C to it.

So looking for a way to do this safely without corrupting or nuking my database.

Title: Re: Is Taskkill as graceful in ending a program as ( CTRL + C )?
Post by: BC_Programmer on April 19, 2018, 06:22:17 PM
With Windows the way to "gracefully" terminate a program would be to send the appropriate exit message to the program's top level Window.

Taskkill will do this- if you open notepad, type some text, and then use TSKILL /IM "Notepad.exe" notepad won't close, instead, it will be as if you clicked the X button, and prompt you to save changes before exiting. compare that to /F, which will not extend such cordialities- it forces the process closed.

With applications/software that run at the console, Ctrl-C is the closest thing. for MySQL in particular, the MySQL process implements and sets what is known as a "ControlCtrlHandler"; this allows it to "see" Control-C signals and it can then say set a flag which tells anything it is currently doing to clean up and exit as soon as possible to close it. (for reference, Control-Break cannot be caught- this is why sometimes Ctrl-C might not do anything in a program, but Ctrl-Break does)

TSKILL doesn't do any of that for a console program. Since it's a console program it doesn't have a top level window so TSKILL sort of always acts as if you passed in /F, I think.

You might be able to use keyboard shortcuts in some way, however. For example pressing a letter in Task Manager will jump to the first process in the list that starts with that letter. if you type quickly, you can drill down. if you type "Microsoft Outlook" quickly enough it will go straight to the first Microsoft Outlook process. Using that you might have the macro type that full name, press the application key and press the accelerator for "Switch To" (I think it is S?) t oswitch to that program before passing in Ctrl-C.
Title: Re: Is Taskkill as graceful in ending a program as ( CTRL + C )?
Post by: DaveLembke on April 19, 2018, 07:27:02 PM
Thanks for the info BC. I never knew that MySQL had that clean up process of CTRL -C. I just been using it for years because somewhere I was taught to use it as the graceful ending of console programs that interact with MySQL databases.

I made some progress in pinning the console window to a set position on my desktop. In the properties of the console shell window I was able to pin it to X/Y coordinates and it gave option to save this position so that each time Windows sees this same program run it will place the console window in that position which is top left. Now that I got that behaving I can target it correctly with the macro. But I will just have to make sure that no other windows are open overtop of that or else it wont work and the macro will perform the CTRL-C action to whatever is the focus of last active window. The good thing here is that as long as I remember to close out of stuff and keep that console window open and not hidden, it should work.

In my travels I was looking to see if there were any code examples of how to take focus of a window by name and pass keystrokes to it but I gave up when I realized I can just sort of cheat by using the macro program to automate all that and that's as easy as saying record and doing the routine with keyboard and mouse recorded of my actions and then end recording and then compile that as an EXE.