Detect if app is running under Task Scheduler

Aug 25, 2010 at 11:14 PM

Newbie here - My apologies if this is not the appropriate section, or if the issue is addressed somewhere here already. I searched, but could not find it.

Just wondering if there are any recommended mechanisms (in an app, eg, in C code), to detect if the app is running under the Task Scheduler. Best (in my case) would be something totally automatic, but I've searched a lot, and have not found any.

I think the most appropriate I've see so far are:

  - Command-line parameter

  - Set a special Env-Var before running the app under the Scheduler, and delete the var just afterwards.

Thank you for any references or suggestions.

  - Mike.

Aug 26, 2010 at 2:12 PM

The parent process for apps launched by the Task Scheduler is "svchost.exe" so that may help. The other option is to create a very small program in C or C++ that lauches the executable and waits for its completion. When you see your process running, you can check its parent process and if its the little program you wrote, you have your answer. I also like the command line parameter idea.

Aug 26, 2010 at 2:35 PM

Thank you.

Re "svchost.exe": Do you happen to know if that parent is NOT present for manually-activated tasks. If so, it looks like a perfect soln for me. Thank you.

The C wrapper is also very nice. It relies on the installer activating that fellow for the TS runs, and not otherwise, which may be acceptable.

In my case, the command-line option is not perfect, because the app runs a bunch of stuff (log-files, hardware environment, etc), before it parses the command-line, and it would be nice to have the "Task-Scheduler" status known from the VERY start - so that ALL console outputs and inputs, delays, etc, would be suppressed in the TS environment.

Apart from the SVCHOST parent check, a suitable approach might be:

  - Run the app from a BAT-file/script

  - Have a simple command-line option on that BAT file, maybe "/NOUI". When the app is installed under TS, that parm is specified.

  - If /NOUI is present, run your C wrapper (and then the app), or set a simple Env-var (eg, SET NOUI=Y)

  - In the app itself, detect the Wrapper, or the Env-Var.

It ain't "automatic", but it ain't bad!

  - Mike