Announcement

Collapse
No announcement yet.

Wait for a process to start, and then perform action?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Wait for a process to start, and then perform action?

    Morning all,
    We have a program that was written in house that we are having problems with on our multi-core machines. The developers are working on correcting the problem in the code, but in the meantime, we need to set the affinity of this process to a single processor on the system.

    Unfortunately, this is something that needs to take place at every reboot.

    What i am looking to do, is setup a script that i can schedule to run at startup (using the windows task scheduler) that will look for the running process called ampservice.exe, if the process is not running yet, then it should sleep for 10 seconds, and try again. Once it sees that process running in the task list, it needs to run another process to set the processor affinity.

    I would like to have the output of this script sent to me in an email when it runs. I have portions of the script that i think should work, but i don't know how to put it all together.

    Here is what i have. As you can see, i don't know how to look for a running process, and if it isn't running, to loop through it until it sees it. Also, how do i redirect any and all output to a text file, or to the body of an email that it will email to me?

    Code:
    '==========================================================================
    '
    ' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 4.0
    '
    ' NAME: Restart AMP Proxy Service
    '
    ' AUTHOR: Aaron Perrault, EVault Inc.
    ' DATE  : 8/14/2007
    '
    ' COMMENT: This script will set the affinity of the AmpProxy service to a single processor.  
    '			It needs to be run every time that the AmpProxy service is restarted, and is 
    '			currently scheduled in the Windows Task Scheduler to run at startup.  
    '			It uses the Process.exe utility that can be downloaded from 
    '			http://www.beyondlogic.org/solutions/processutil/processutil.htm
    '			This utility should be extracted and copied to the %systemroot%\system32 
    '			directory on the problem server
    '
    '==========================================================================
    
    Option Explicit
    Dim objShell
    Dim strService
    Dim strComputerName
    Set objShell = CreateObject("WScript.Shell")
    Set strComputerName = objShell.ExpandEnvironmentStrings ("%COMPUTERNAME%")
    strService = "AmpProxy.exe"
    
    'Set the affinity to 1 processor using the Process command
    objShell.Run "process.exe -a " & strService & " 01"
    Here is the code that i have to send an email
    Code:
    Function sendEmail()
    	Set objNetwork = CreateObject("Wscript.Network") 
    	strComputer = objNetwork.ComputerName 
    	Set objEmail = CreateObject("CDO.Message") 
    	objEmail.From = strComputer & "@evault.com" 
    	objEmail.To = "EMAIL ADDRESS"
    	objEmail.Subject = "Processor Affinity Set" 
    	objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
    	objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "Hermes" 
    	objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
    	objEmail.Configuration.Fields.Update 
    	objEmail.Send
    end Function
    Any help would be greatly appreciated.

    app

  • #2
    Re: Wait for a process to start, and then perform action?

    Examples of Wait until process is started;
    Code:
    strProcName = "calc.exe"  '<----replace with "ampservice.exe"
    
    strComputer ="."
    Set objWMIService = GetObject _
        ("winmgmts:{impersonationLevel=impersonate," _
        & "authenticationLevel=pktPrivacy}!//" _
        & strComputer & "\root\cimv2")
    
    Do until IsStarted = True
    
      Wscript.sleep 5000 'pause 5 seconds
    
      Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process Where Name = '" & strProcName & "'")
      For Each objProcess in colProcesses
        IsStarted = True
        exit For
      Next
    
    Loop
    
    wscript.sleep 2000 'pause 2 seconds extra?
    
    msgBox "go ahead " & strProcName & " is started now"
    or something like this;
    Code:
    strProcName = "calc.exe"  '<----replace with "ampservice.exe"
    
    strComputer ="."
    Set objWMIService = GetObject _
        ("winmgmts:{impersonationLevel=impersonate," _
        & "authenticationLevel=pktPrivacy}!//" _
        & strComputer & "\root\cimv2")
    
    Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process Where Name = '" & strProcName & "'")
    For Each objProcess in colProcesses
       IsStarted = True
       exit For
     Next
    
    If Not IsStarted Then
      wmiQuery = "Select * From Win32_ProcessStartTrace Where " _
                 & "ProcessName = '" & strProcName & "'"
      Set colProcesses = objWMIService.ExecNotificationQuery(wmiQuery)
       Do until IsStarted = True
         Set objProcess = colProcesses.NextEvent
         IsStarted = True
       Loop
    End If
    
    wscript.sleep 2000 'pause 2 seconds extra?
    
    msgBox "go ahead " & strProcName & " is started now"
    Where does the output from the script come from?

    send email examples: http://www.paulsadowski.com/WSH/cdo.htm


    \Rems
    Last edited by Rems; 14th August 2007, 22:09.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Wait for a process to start, and then perform action?

      Rems,
      Thanks for the help. That looks like it will work. The process.exe outputs data to the screen when it runs, and i would like to capture that data and put it in the email. Ideally it would just put the results in the body of the email, but if it can put it in a text file and send me that, it would be great.

      Thanks

      app

      Comment


      • #4
        Re: Wait for a process to start, and then perform action?

        Originally posted by apperrault View Post
        The process.exe outputs data to the screen when it runs, and i would like to capture that data
        check the .Run method and the .Exec method here:
        http://forums.petri.com/showthread.php?t=17595

        Code:
        strService = "AmpProxy.exe"
        
        strCommand = "process.exe -a " & strService & " 01"
        
        Set objShell = WScript.CreateObject ("WScript.shell")
        Set oExec = objShell.Exec(strCommand)
        ' or else-> Set oExec = objShell.Exec("%comspec% /c " & strCommand)
        
        Do While oExec.Status = 0
           WScript.Sleep 100
        Loop 'Do until job is finished
        
        sOutput = oExec.stdOut.ReadAll
        
        sOutput = now() & vbNewLine & sOutput  '<--adding date as first line
        
        
        msgBox sOutput
        
        
        '*** Example of  ***
        'output to txt-file 
        strOutputFile = "c:\output.txt"
        
        On Error Resume Next
        With CreateObject("Scripting.FileSystemObject")
          Set oFile = .CreateTextFile(strOutputFile, True)  'overwrite=true
        End With
          oFile.WriteLine( sOutput )
          oFile.close
        Set oFile = Nothing
        
        objShell.Run "wordpad.exe " & strOutputFile, 1, false
        Set objShell = Nothing
        
        'more;
        'output to HTML: http://forums.petri.com/showthread.php?t=17595
        'output to email.TextBody: http://www.paulsadowski.com/WSH/cdo.htm
        \Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Wait for a process to start, and then perform action?

          Is the OS Windows 2003 Enterprise or DataCenter?
          if yes, you should use WSRM instead of changing the AmpProxy.exe's affinity with process.exe.

          WSRM FAQ's: http://technet.microsoft.com/en-us/w.../bb405954.aspx
          WSRM Download: http://technet.microsoft.com/en-us/w.../bb405952.aspx

          Martin77.

          Comment

          Working...
          X