WinBatch® Technical Support Forum

Archived Boards => WinBatch Script Exchange => Topic started by: paljets on February 05, 2015, 11:25:51 AM

Title: Script to detect running process
Post by: paljets on February 05, 2015, 11:25:51 AM
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?
Title: Re: Script to detect running process
Post by: DAG_P6 on February 13, 2015, 01:06:27 AM
The Process Extender probably has everything you need to do so.
Title: Re: Script to detect running process
Post by: 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.
Title: Re: Script to detect running process
Post by: ChuckC on February 20, 2015, 08:24:33 AM
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.
Title: Re: Script to detect running process
Post by: td on February 20, 2015, 01:18:01 PM
It would appear that whether or not the user id is important is contingent on several factors that OP did not specify.
Title: Re: Script to detect running process
Post by: ChuckC on February 23, 2015, 06:58:27 AM
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.
Title: Re: Script to detect running process
Post by: td on February 23, 2015, 10:20:13 AM
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.