Do you know how to gracefully shut down a program using task scheduler in Win10?

It is fairly straight forward to start a program at a given time in Windows using a batch file run by Task Scheduler.
However, as far as I can tell, there is no way to properly shut down a running program using Task Scheduler.
It is easy just to kill a program but that can lead to problems.
My googling suggests it is easy to do this in Linux but it cannot be done in Windows.
As a last resort I am asking here.
Does anyone know a practical way of doing this?
Thank you.
Ian

I’m not sure it’s as easy in Linux as you think. You can kill a process from the command line (and probably GUI but I don’t use Linux GUI) using the kill/pkill/killall commands but it’s not always that easy if the parent process has spawned a bunch if sub-processes. You generally need to find the process ID (a number which can be up to 6-7 digits long) which might not be associated with the application name you expect and then use a kill/pkill/killall command (or more than one) to get rid of the process(es).

By contrast in Windows I’d use Task Manager to find the process, right click it and use ‘End Process’. Task Manager usually knows about the sub-processes owned by the parent and will kill them too on demand.

In both cases this type of termination can be pretty catastrophic to the application depending on what it was doing when you killed it.

Hi, do you want to just close the progam or is it prior to windows rebooting?

Either way, you could use something like Mini Mouse Macro, you basically record what you do on the screen and the macro will reproduce it, it will take some to get used to and you may need to fine tweak the macro but it does work and you can set that to run using scheduler

Thanks Chris and Bashy. I think I just have to accept that it cannot be done natively in Win 10.
This seems strange to me but so does a lot of our world.
I will have a look at Mini Mouse Macro.
Thanks again,
Ian

I’m not sure what you mean? You can do it natively using the built in Task Manager functionality. I’ve used quite a few operating systems over the years and don’t remember any of them having the feature you want to use so it’s not really a Windows issue. Scheduled/batch jobs aren’t intended to have human intervention so any interaction with then, e.g. killing them, is going to use lesser used features of the operating system.

Chris,
I want to be able to shut down a running program at a given time using task scheduler or something equivalent. I want to be able to close it down properly as it would be if I were to click on the exit command. I am aware that programs can be “killed” using a batch file run by task scheduler but I want data to be saved etc as if it had been shut down manually.
As far as I can tell this cannot be done in Windows.
I will investigate the work around suggested by Bashy which seems to be a way of recording mouse actions and replaying them later.
I hope I have been clearer this time.
Ian

Ahhh! I see what you’re trying to do now. I probably misread what you said initially!

I don’t remember any operating system I’ve used having such a feature so it’s not really a Windows specific problem. If I were to do it in Linux then I’d be writing a specific script to do it. You could do the same in Windows using PowerShell or something similar.

there used to be a way, maybe still is
basicly you record a macro function
i.e to click a menu item in a program
ie your desktop mouse movements and clicks are recorded
to be repeated, like a play back

e.g

That’s kind of what Bashy is suggesting. It’s effectively a custom built script to do what you want. I think you can create a PowerShell script by doing something similar, but I always wrote them from scratch.

I’d also add that I don’t think it’s possible to kill something gracefully unless it’s written to allow that to happen.

By definition scheduled tasks don’t present a user interface to interact with them so I think the best that can be done is to kill them. In Linux it’s possible to send specific signals to a background task, e.g. SIGHUP, SIGTERM, SIGQUIT, SIGKILL and SIGINT, that can signal to the task what to do, but the task has to be written to accept the signal and decide what to do with it considering the current state of the app.

When I used to set my lappy to auto reboot, programs like WD and WXSIM used to close without any issues at all, then they would start again with nothing lost. Perhaps it depends on the program. Isn’t there a graceful restart and a forceful restart or is that just Linux, I believe graceful closes all open pid’s, did I get that right?

SIGTERM is the Linux signal sent to running programs when the system wants them to shut down. It is up to each program to decide what to do when it receives that signal. The program can shut down gracefully, just do nothing, i.e. die, or even refuse to close. I guess if it refuses to close the next signal is likely to be SIGKILL. However, not all Linux software handles signals (of any type) so you can’t guarantee that a SIGTERM will do anything. I know a number of my own programs have no signal processing in them.

Windows is similar. There are methods by which Windows can notify a program that it wishes to close down, e.g. in PowerShell

Get-Process MyProgram | Foreach-Object { $_.CloseMainWindow() | Out-Null }

. Just like in Linux a program can choose what to do when it receives the CloseMainWindow() message. It might have no code to handle the message, or it might gracefully shut itself down, or it might prompt the user for more info, e.g. “You’ve got open files do you want to save them first”, or it might choose to refuse to close. If it refuses to close (or pops up another window with a question) you could do

Get-Process Myprogram |   Foreach-Object { $_.CloseMainWindow() | Out-Null } | stop-process –force

to force it to close if it didn’t do it for itself.

In Linux you would use the ‘kill’ command to kill a process. ‘kill’ can be issued with various options, e.g. ‘kill -9’ sends a SIGKILL signal.

Going back to the original request…in Windows (10 and 11, not sure about earlier versions) you can use ‘taskkill’ which by default will try sending a graceful shutdown. Alternatively ‘taskkill -f’ is for a ‘forced shutdown’.

Thanks Chris, I was thinking mostly with regards to CPANEL and how that works, I think creating a batch file with the taskkill should do what the op requires, this is what I had in mine when I used to have the lappy reboot automatically

C:\windows\system32\shutdown -r -f -t 01

Hi Bashy,
Yes I am aware that I could kill the program with a batch file and task scheduler using the taskkill command.
But this is my whole point. Doing that can lead to file corruption etc. The program I want to close has pretty constant disk activity so the risk of file corruption with using taskkill is unacceptable.
It just seems strange to me that there is not some sort of general windows command that tells a program to close normally and safely.
Something like “Do whatever you do when someone clicks on exit”.
Thanks to everyone for their input.
Ian

using windows task manager to kill a task is brutal
programs are not able to intercept that to save files on exit etc

Taskkill can ask programs to shut down gracefully but that can only happen if the program has been written to listen to such requests. If the program hasn’t been written that way then there is nothing that taskkill or Windows can do to make it shut down gracefully. All it can do in this case is terminate the program brutally and you have to hope that things are ok.

If my doesnt work then MMM (or the like) is your only other option that i can think of.