One of the most important states is SERVICE_RUNNING. When the SCM reports this state, the script performs the actions for which it was designed. For example, if the script was designed to ping a server every minute, during the SERVICE_RUNNING state, the script issues a ping every minute.
The Win32::Daemon Perl Extension
A few tools are available to create a Win32 Perl service, such as the srvany.exe freeware utility and ActiveState's PerlSvc utility. However, srvany.exe has limitations, and PerlSvc requires the purchase of ActiveState's Perl Dev Kit. An alternative is the Win32::Daemon extension (http://www.roth.net/perl/daemon) and the Win32::Daemon::Simple extension (http://jenda.krynicky.cz), which depends on Win32::Daemon. These extensions provide full Win32 service functionality from a simple Perl script.
You can install Win32::Daemon by running the Perl Package Manager (PPM) from a command line:
PPM install http://www.roth.net/perl/
packages/win32-daemon.ppd
Creating a Process-Monitoring Service
In the Code Library on the Windows Scripting Solutions Web site (http://www.winscriptingsolutions.com), you'll find ProcMon.pl, a script that creates a process-monitoring service. You can use this script to prevent specific processes from running. For example, I configured the script to prevent users from running the registry editors (regedit.exe and regedt32.exe) and the FTP client. You can also use the script to terminate a process after the process has run for a specific amount of time. For example, I configured the script to terminate any Telnet session that runs longer than 2 minutes. This termination prevents users from staying logged on to a Telnet client for long periods of time.
The script's %PROC_LIST hash lists the applications to kill and when to kill them, as the code in Listing 1 shows. Each hash key lists the application's filename (e.g., telnet.exe), which must be in all lowercase letters. The value associated with each hash key specifies how many seconds the application is allowed to run before being terminated. Notice that all but one of the applications have a value of 0 seconds. This value causes the script to terminate those applications just after the script notices they're running.
Next, the script sets the service configurations. Listing 2 shows this block of code. The code first sets the default configuration settings, then calls the Configure() subroutine to check for user-specified parameters, some of which might override the default settings. Depending on the configuration options you specify when you launch ProcMon.pl, the code either installs the script as a service, stops the script from running as a service, or displays the script's syntax and exits. Figure 1 shows the configuration options that you can use when launching ProcMon.pl.
After setting the service configurations, the script creates the log file. The default log's filename is the name of the script with the .log extension. So, if you leave the script's name as ProcMon.pl, the resulting default log file will be ProcMon.log. The log file will reside in the same directory as the script. The code at callout A in Listing 2 configures this default location. However, you can override the default by using the -l parameter when you launch the script.
Prev. page
1
[2]
3
4
next page