<kill processName="peskyLittleProcess" />

For a second time now, I’ve found myself needing to kill processes in my automated build scripts, so that was enough for me to automate it and wrap it up in a custom NAnt task.  The first time I needed it, I just embedded the C# code inside a NAnt script block, but using it as “real” NAnt task is much nicer.  And, perhaps I overlooked it, but I did not see a task in the set of NAnt or NAntContrib tasks to do this kind of simple operation and Google didn’t turn up anything either.  So I just banged it out real quick…

Here’s how it can be used:

   1: <loadtasks assembly="pathToToolsFolderjoeyDotNet.Commons.dll" />
   2: <kill processName="w3wp" />

 

And for those that are interested, here’s the code for the task, although it’s really quite simple: 

   1: /// <summary>
   2: /// Looks for processes with specified name and kills them
   3: /// </summary>
   4: [TaskName("kill")]
   5: public class KillTask : Task
   6: {
   7:     private string processName;
   8:  
   9:     [TaskAttribute("processName", Required = true), StringValidator(AllowEmpty = false)]
  10:     public string ProcessName
  11:     {
  12:         get { return processName; }
  13:         set { processName = value; }
  14:     }
  15:  
  16:     protected override void ExecuteTask()
  17:     {
  18:         Log(Level.Info, "Looking for processes named {0}...", processName);
  19:         foreach (Process process in Process.GetProcessesByName(processName))
  20:         {
  21:             Log(Level.Info, "Found process named {0}, killing...", processName);
  22:             process.Kill();
  23:         }
  24:     }
  25: }

 

Disclaimer:  I take no responsibility for what happens if try to pass in a process name like “System” or “winlogon” or “explorer”.   So please use wisely…  :)

As usual, you can grab the source code yourself from the repository.

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

This entry was posted in build, nant, tools. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Comments are closed.