/// <summary> /// Kills a process by its PID. /// </summary> /// <param name="fileSystem">The file system.</param> /// <param name="pid">The PID of the process.</param> public static void KillByPId(IFileSystem fileSystem, int pid) { var process = new Process(); var platform = PopulateKillProcess(process, pid, Environment.OSVersion); if (!fileSystem.CheckIfFileExists(process.StartInfo.FileName)) { logger.Error("KillByPId(): Unable to find kill command"); var message = string.Format( CultureInfo.CurrentCulture, "Kill command {0} not found on {1} OS. PID:{2}", process.StartInfo.FileName, platform, pid); throw new CruiseControlException(message); } // Execute the actual kill command logger.Info("KillByPId(): running kill command on {0}", pid); logger.Debug( "KillByPId(): command = [{0}], args = [{1}]", process.StartInfo.FileName, process.StartInfo.Arguments); process.Start(); process.WaitForExit(); process.Close(); }
/// <summary> /// Creates the process. /// </summary> /// <param name="fileSystem">The file system.</param> /// <returns> /// The new <see cref="Process"/>. /// </returns> public Process CreateProcess(IFileSystem fileSystem) { var executableInWorkingDirectory = Path.Combine(WorkingDirectory ?? string.Empty, FileName); if (fileSystem.CheckIfFileExists(executableInWorkingDirectory)) { startInfo.FileName = executableInWorkingDirectory; } // if WorkingDirectory is filled in, check that it exists if (!string.IsNullOrEmpty(WorkingDirectory) && !fileSystem.CheckIfDirectoryExists(WorkingDirectory)) { throw new DirectoryNotFoundException("Directory does not exist: " + WorkingDirectory); } logger.Debug("Creating process for '{0}'", this.FileName); var process = new Process { StartInfo = startInfo }; return(process); }
/// <summary> /// Creates the process. /// </summary> /// <param name="fileSystem">The file system.</param> /// <returns> /// The new <see cref="Process"/>. /// </returns> public Process CreateProcess(IFileSystem fileSystem) { var executableInWorkingDirectory = Path.Combine(WorkingDirectory ?? string.Empty, FileName); if (fileSystem.CheckIfFileExists(executableInWorkingDirectory)) { startInfo.FileName = executableInWorkingDirectory; } // if WorkingDirectory is filled in, check that it exists if (!string.IsNullOrEmpty(WorkingDirectory) && !fileSystem.CheckIfDirectoryExists(WorkingDirectory)) { throw new DirectoryNotFoundException("Directory does not exist: " + WorkingDirectory); } logger.Debug("Creating process for '{0}'", this.FileName); var process = new Process { StartInfo = startInfo }; return process; }