Script to detect running process

Started by paljets, February 05, 2015, 11:25:51 AM

Previous topic - Next topic

paljets

So I need to write a script that does the following:

Check every 60 seconds to see if a process under the user's user-id is running.  If it is NOT running, issue a ts disconnect

the script needs to check every 60 seconds till the process is no longer found, then issue the disconnect..

Anyone help?

DAG_P6

The Process Extender probably has everything you need to do so.
David A. Gray
You are more important than any technology.

td

Here is a link to an example that uses the Process Extender and WMI to obtain the user name associated with a process.

http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WIL~Extenders/Process~ID+Get~Username~from~a~Process.txt

The Terminal Service extender's wtsDisonnectSession function can be used to disconnect a session.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

ChuckC

Also of interest... Take a look at the sample script for the Terminal Services extender, as it shows how to use the extender to enumerate all of the processes running within a specified [e.g. the current] TS session.   For purposes of simply determining if a process is running, it will pull double duty and nix out the need for using the Process Extender.  Since it is the session within which the process runs, and not the actual username under which the process runs, which is important, the TS extender can provide all the required information w/o having to resort to using a combination of the Process extender and WMI.


Quote from: td on February 13, 2015, 06:44:12 AM
Here is a link to an example that uses the Process Extender and WMI to obtain the user name associated with a process.

http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WIL~Extenders/Process~ID+Get~Username~from~a~Process.txt

The Terminal Service extender's wtsDisonnectSession function can be used to disconnect a session.

td

It would appear that whether or not the user id is important is contingent on several factors that OP did not specify.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

ChuckC

Agreed, but I was looking at it from the point of view that the O.P. might not be aware of the significance that a computer might have more than a single TS session active on it when attempting to basically "terminate the TS session if a certain process isn't running."  My statements were alluding towards the generalization of the methodology for a TS server, and to make it neutral w/respect to the user account that the specified process is running under, such as a case where the process was launched using "run as user" with alternate credentials having been supplied.

In its general form, the final script could rely solely on the TS extender and run as a native NT service that is capable of monitoring all active TS sessions on a TS server, or it could run in a user's session and perform the same functionality just for the users's TS session.

td

There certainly many different approaches. A personal preference would be to use WinBatch CLR hosting, since the FCL (dotNet Framework) provides relatively easy to use classes that might eliminate the need for extenders or WMI.  But not everyone is comfortable using CLR hosting.

If the OP could start a background process in each session of interest at start-up, a simple application existence checker script might be all that is needed.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade