JSI Tip 7127. Freeware RunProcess allows you to delay the start of a process, set a number of repetitions, and limit the execution time.

Jerold Schulman

September 2, 2003

6 Min Read
ITPro Today logo in a gray background | ITPro Today


Download RunProcess.zip.

The RunProcess.txt file contains:

Copyright (C)2002, Frank P. Westlake.LICENSE:RunProcess.exe is FREEWARE. It is freely redistributable and there are no user limits, time limits, or restrictions on the number of copies that may be used or retained; however, the author retains the copyright.DESCRIPTION:Runs a process and waits for it to complete or optionally launches the process and exits. A report may be printed with the process times and other information. The process may be given a delayed start, a number of repetitions to recur, and a time limit. The process may be safely killed if it runs beyond a specified duration. A console process may be given its own window and the window parameters may be specified. The process' base priority may be specified. The process can be logged on to a specified user account. An extended length command line may be handed to the process and the standard streams may be redirected internally. There are more options, see below.ERRORLEVEL:If the process is successfully started the exit code will be that of the client process. If the process fails to start the exit code will be a Windows system exit code that identifies the problem.USAGE:RunProcess [options] ProgramName [ProgramName's parameters]If the file name extension is not specified a search will be conducted using the PATHEXT environment variable. The extension is then located in the registry under HKEY_CLASSES_ROOT and the associated program is run.RunProcess will pass piped or redirected input to the client program, forexample:DIR | RunProcess %ComSpec% /CMORERunProcess %ComSpec% /CMORE commandline.txtEcho See how they run.>>commandline.txtRunProcess /-a:commandline.txt %ComSpec% /cECHO/In the above example the command line will become:%ComSpec% /cECHO/Three blind mice. See how they run.Without the hyphen in the switch the command line would be:%ComSpec% /cECHO/ Three blind mice. See how they run.NOTE: CMD.EXE will not accept extended command line lengths but most other programs will. /c:flagSet the process priority class. Realtime priority may interfere with system processes and should be avoided. A low priority process may fail on a busy system. 'flag' may be one of the following:L or lLOW (or IDLE) priority.N or nNORMAL priority.H or hHIGH priority.R or rREALTIME priority.-Disable priority boosting. Use alone or with one of the above.Example:RunProcess /c:L BackupFiles.cmd /desktop:[[Window station name\]desktop name]The Desktop, or window station and desktop, for the process's window. The default, if "/desktop" alone is specified, is "WinSta0Default". There will rarely ever be a need to usea window station and desktop other than the default. The logondesktop is "WinSta0Winlogon" and using it requires the appropriate privilidges (SYSTEM user has them). Services generally use a noninteractive window station.Examples:RunProcess /desktop:WinSta0WinLogon /fC %SystemRoot%ALERT.EXERunProcess /desktop:WinSta0Default %ComSpec%RunProcess /desktop %ComSpec% /d:DirNameThe working directory for the process. Note that if the directory to be assigned as the working directory is named "esktop", the colon must be included (/d:esktop) or the switch will be interpreted as "/desktop". /f:flagsProcess flags. 'flags' may be one or more of the following, except where noted, in either upper or lower case:CCreate a new console window. Cannot be used with 'D'.DCreate a detached process. Cannot be used with 'C'.GCreate a new process group.PCreate the 16-bit application in a private VDM.SCreate the 16-bit application in the shared VDM.UCreate a Unicode environment.WCreate process in a suspended state. If the process is created with its own process group (/fG) CTRL-C will decrement the suspend count until it reaches 0 and the process resumes. Once the process has resumed CTRL-C will behave normally. CTRL-BREAK will cancel a suspended process. Other programs, such as psSuspend.exe, can also be used to resume the process.Only one of the following (default is B [Batch]):BIf logon as user (/u, below), perform a batch logon.IIf logon as user (/u, below), perform an interactive logon.NIf logon as user (/u, below), perform a network logon.VIf logon as user (/u, below), perform a service logon.Example:RunProcess /f:CIG /u:fwestlak /p:secret BackupFiles.cmd /e:o|nSend the process's STDERR to STDOUT (/e:o) or to NULL (/e:n).Ignored if /F specifies either flag C or flag D. /o:e|nSend the process's STDOUT to STDERR (/o:e) or to NULL (/o:n).Ignored if /F specifies either flag C or flag D. /iIgnore CTRL_LOGOFF_EVENT. This permits RUNPROCESS to be used with SRVANY.EXE as a service that will remain after logoff. /k:timeKill the process if it does not finish in the specified time. If the process is being repeated (see /t) the kill timer will be applied to each repetition. If the client process generates processes of its own (i.e. CMD.EXE) those processes will not be killed unless the switch /F:G is also specified. If not specified otherwise, the time will be read as milliseconds. To indicate hours, minutes, or seconds, immediately follow the time with one of the modifiers: h, m, or s. For Example, the following are equivalent periods of time:RunProcess /k:7200000ms BackupFiles.cmdRunProcess /k:7200000   BackupFiles.cmdRunProcess /k:7200s     BackupFiles.cmdRunProcess /k:120m      BackupFiles.cmdRunProcess /k:2h        BackupFiles.cmd /m:BitMaskSet the process affinity mask. On a multiprocessor system this sets the processors that the process can use. 'BitMask' is a right-justified binary pattern that identifies the processors to be used. Some examples:/m:1Processor 1 only./m:10Processor 2 only./m:11Processors 1 or 2./m:100Processor 3 only. /r[:o|e|c|n|filename]Print a process report to STDOUT (o), STDERR (e), directly to the console window (c), or to a named file. The default is STDERR. To disable all RunProcess output, including PID and wait information but not including errors, use /r:n. The report will be printed after the process has completed, unless the switch /x is included, and will appear similar to the following:------ RunProcess: Process Information ------ProcessID:        232Program:          C:WINNTSystem32cmd.EXECommandLine:      C:WINNTSystem32cmd.EXE /cC:tempttt.cmdRepetition:       1 of 1MinimumOSVer:     4.0PriorityClass:    NORMALCreationTime:     2002-09-17 13:22:37.971ExitTime:         2002-09-17 13:22:38.091ElapsedTime:      120.1728 msKernelTime:       20.0288 msUserTime:         20.0288 msExitCode:         0ErrorMessage:     The operation completed successfully.------ RunProcess: End Process Information---The "ErrorMessage" item is a translation of the exit code to a Windows system message. The process may or may not be using Windows system exit codes so this information may or may not be correct. /u:usernameIf specified, the process will be logged on with the indicated username. The username may be preceded with the domain name. The current user must have the applicable batch, interactive, service, or network priveledge. If none of the flags B, I, N, or S are specified in /f above, B is assumed.Examples:RunProcess /u:fwestlake        /p BackupFiles.cmdRunProcess /u:.fwestlake      /p BackupFiles.cmdRunProcess /u:domainfwestlake /p BackupFiles.cmdTo be able to use this "logon as" feature the user must have the following privileges:Minimum:    "Act as part of the Operating System"      "Logon locally"Batch logon:"Log on as a batch job"Service logon:"Log on as a batch service"Network logon:"Access this computer from network"These privileges can be set with the User Manager (menu item Policies|User Rights). Normally only the SYSTEM user has these privileges. /p[password]A plain text password that is necessary for the username. If this switch is absent, no password is used. If the switch is used alone (/p), the password is read from STDIN with echo disabled. STDIN may be a pipe or redirected input.Examples:RunProcess /u:fwestlake /p:%PASSWORD% BackupFiles.cmdRunProcess /u:"NT Authoritysystem" BackupFiles.cmdECHO/%PASSWORD% | RunProcess /u:fwestlake /p BackupFiles.cmdRunProcess /u:fwestlake /p BackupFiles.cmd Vars.txtECHO/ComSpec=%SystemRoot%System32.CMD.EXE>>Vars.txtECHO/Path=C:WINNT;C:WINNTSystem32>>Vars.txtECHO/PATHEXT=.COM;.EXE;.CMD>>Vars.txtECHO/PROMPT=%ComSpec%$G>>Vars.txtECHO/SystemDrive=C:>>Vars.txtECHO/SystemRoot=C:WINNT>>Vars.txtECHO/TEMP=C:TEMP>>Vars.txtECHO/TMP=C:TMP>>Vars.txtECHO/windir=C:WINNT>>Vars.txtRunProcess /v:Vars.txt BackupFiles.cmd /w:flagsSets window options. 'flags' may be one or more of the following, upper or lower case, followed by a value and optionally separated by commas:XWindow position, pixels from left of screen.YWindow position, pixels from top of screen.CWindow size, pixels from left to right.RWindow size, pixels from top to bottom.LConsoles only, buffer size in lines from top to bottom.WConsoles only, buffer size in characters from left to right.BConsoles only, background color.FConsoles only, foreground color.SWindow state (0=hidden, 1=normal, 2=min, 3=max)Examples:RunProcess /w:R80L25,S2 BackupFiles.cmdRunProcess /w:R80,L25,B1,F11 BackupFiles.cmd /xDo not wait for the process to complete before repeating or exiting.



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like