Alexi Demetriou · Nov 20, 2018

Run batch script from Task Manager

Good morning,

Background to task: Shut down Ensemble from a task in Task Manager.

I have a batch file that shuts down Ensemble when run as administrator. I can confirm this works, as it brings up the shutdown/restart prompt for Ensemble, however I now need to plug this into Ensemble's Task Scheduler so it can be run on-demand.

At present I am trying to use: do ^execute: %windir%\system32\cmd.exe Z:\Scripts\shutdown.lnk

This is to run the batch file from the shortcut, which runs it as admin. I get a syntax error, which I am not surprised to see, so could I have some advice on how to code this better? Alternatively, if anyone has any better ideas to shutdown Ensemble from Task Manager I am open to suggestions.

Many thanks and kind regards, Alexi Demetriou

1 1,198
Discussion (19)3
Log in or sign up to continue

you may try what works in pure Caché


Hi Robert, Thank you for updating this with your suggestion. I have put this directly into the Task Manager as a Legacy task but I still appear to get a syntax error. Am I running this incorrectly?

Many thanks and kind regards, Alexi Demetriou

Remove ZNamespace "%SYS"from the ExecuteCode line. You may want to do it for either of two reasons:

  • if you scheduled a new task using Task manager's defaults for startup namespace as "%SYS", it is not needed
  • this construction in syntactically wrong;  zn "%SYS" or new $namespace set $namespace="%SYS" should be used instead.

Good afternoon Dmitry, after running the suggestion Alexey made and then testing yours as well it makes more sense to have the task finish successfully as opposed to disabling itself. I will run this past you as well, but do you know of a way for this to be run under a batch file instead? This was the original requirement.

Many thanks and kind regards, Alexi Demetriou

Dmitry and Ed,

I'm just curious: why do you recommend the OP to use an entry point which is provided mostly for recovering from errors? In most cases we do *want* the user  code to be executed, don't we?

job INT^SHUTDOWN ; is the right code (corrected by Dmitry)

batch file means, that you want to do it outside of Ensemble. In this case you should use ccontrol command or iris if you use IRIS.

ccontrol stop <ensemble> quietly

ccontrol start <ensemble>

where you should replace <ensemble> with your instance name.

if you work on windows, you can find ccontrol tool in bin directory of installed Ensemble.

you can find more details about using ccontrol in the documentation

I have put that in the batch file, and confirm it works, but the original requirement is still to run the batch file from Ensemble, even if it ultimately loops back into itself. I am keeping the task for shutting down Ensemble, but still wonder if it's possible to run the batch file.

look at $zf function in the documentation, it gives a possibility to execute a command in OS.

so, the same Legacy task and your code should be 

set res=$zf(-2, "c:\path\to\your\shutdown_ensemble.bat")

Hi Dmitry, I apologise for the late reply. I have tried this in the Task Scheduler but when it runs it does not execute the batch file. I have the following:

set res=$zf(-2, "Z:\Scripts\shutdown.bat")

Many thanks and kind regards, Alexi Demetriou

Is your Z: drive a mapped one? If so, it might not be available to the Windows user that the InterSystems background processes run as. Is your Ensemble instance running as LocalSystem (see Windows Services)?

Hi John, it turns out that my code was slightly incorrect. Rather than changing directory, I needed to define the directory to run the code immediately. This was resolved very recently. I will update the ticket accordingly.

Good afternoon Alexey, thank you for that update. This has indeed shut down Ensemble now. Is there a way it can be used inside of a batch file that can be called from Task Manager though? Whilst this does work, the original spec was to run it from a batch file.

Many thanks and kind regards, Alexi Demetriou

Hi Alexi, 

It sounds like you are wanted to:

  • run the task in Caché Task Manager which does external call to some *.bat/.cmd file (so, calling out Caché)
  • this .bat file should run some code inside Caché (so, calling in Caché back again)

This scenario is possible (despite some security caveats), but (pardon me) - just rises unneeded difficulties. If your task can be solved easier than it initially seemed to be, why not take the easier (and more secure) way?

Hi Alexey,

I can run this past my superior who requested this. To me, it makes sense to keep things in the Task Manager, but I suppose the only issue is when one does not have access to Ensemble's portal.

Kind regards, Alexi Demetriou

If so, I'd just put an icon which points to 'ccontrol stop ...' command file on the Work Table accessible to all users, or (to keep it secured better) on individual Work Tables of those ones who permitted to initiate the shutdown.

Fixed, and also changed from DO to JOB, if we do not want to manually restore this task after every run.


would shut down the instance without running user code. Docs.

Better to use command JOB INT^SHUTDOWN instead of DO. When you use DO, the task will not be finished and will be marked as suspended, and you will have to resume it manually.

As per Dmitry's comment, the correct code was set res=$zf(-2, "Z:\Scripts\shutdown.bat") to run the batch script. In order for this to work, you need to directly reference the folder you run the script from. In my case it was start /W Drive:\Folder\ccontrol stop NAMESPACE.

Be cautious when using this.