Сервис, не полностью остановленный после ServiceController. Остановитесь ()

Команда, которую вы используете, будет запускать zip для каждого файла отдельно, попробуйте это:

find . -name  -print | zip newZipFile.zip -@

-@ говорит zip читать файлы из ввода. Из man zip (1) ,

-@ списки файлов. Если список файлов указан как -@ [Не в MacOS], zip берет список входных файлов из стандартного ввода, а не из командной строки.

6
задан Dave 19 May 2009 в 20:59
поделиться

4 ответа

ServiceController.WaitForStopped () / WaitForStatus () вернется, когда реализация службы заявит, что она остановлена. Это не обязательно означает, что процесс освободил все свои ресурсы и завершился. Я видел, как это делают и другие базы данных, кроме SQL Server.

Если вы действительно хотите убедиться, что база данных полностью и действительно остановлена, вам придется взаимодействовать с самой базой данных, получить идентификатор процесса и подождать для его выхода, дождитесь снятия блокировок с файлов, ...

3
ответ дан 8 December 2019 в 13:02
поделиться

Я видел это раньше, когда я останавливал службу, которая была зависимой от другой службы, а вторая служба удерживала ресурсы, я даже не знал, что она использует. Как вы думаете, это может быть так? Я знаю, что SQL имеет довольно много разных компонентов, но я не проверял, связаны ли с ним несколько служб.

Удачи!

0
ответ дан 8 December 2019 в 13:02
поделиться

Попробуйте использовать Environment.Exit (1);

-5
ответ дан 8 December 2019 в 13:02
поделиться

Службы Windows являются слоем поверх процессы; Чтобы быть службой, приложение должно подключиться к диспетчеру управления службами и объявить, какие службы доступны. Это соединение обрабатывается в библиотеке ADVAPI32.DLL. Как только это соединение установлено, библиотека поддерживает поток, ожидающий команд от диспетчера управления службами, который затем может запускать и останавливать службы произвольно. Я не верю, что процесс должен завершаться после завершения последней службы в нем. Хотя это обычно и происходит, окончание связи с диспетчером управления службами, которое происходит после того, как последняя служба переходит в состояние «остановлено», может произойти значительно раньше, чем процесс фактически завершится, освобождение каких-либо ресурсов, которые он еще не выпустил явным образом.

Windows Service API включает функциональные возможности, позволяющие получить идентификатор процесса для процесса, в котором размещена служба. В одном процессе может размещаться множество служб, поэтому процесс может фактически не завершиться, когда интересующая вас служба завершит работу, но вы должны быть в безопасности с SQL Server. К сожалению, .NET Framework не предоставляет эту функциональность. Однако он предоставляет дескриптор службы, которую он использует для внутренних вызовов API, и вы можете использовать его для выполнения собственных вызовов API. Затем с помощью P / Invoke вы можете получить идентификатор процесса процесса службы Windows, и оттуда, при условии, что у вас есть необходимое разрешение, вы можете открыть дескриптор процесса, который можно использовать, чтобы дождаться его завершения. выход.

Примерно так:

[DllImport("advapi32")]
static extern bool QueryServiceStatusEx(IntPtr hService, int InfoLevel, ref SERVICE_STATUS_PROCESS lpBuffer, int cbBufSize, out int pcbBytesNeeded);

const int SC_STATUS_PROCESS_INFO = 0;

[StructLayout(LayoutKind.Sequential)]
struct SERVICE_STATUS_PROCESS
{
  public int dwServiceType;
  public int dwCurrentState;
  public int dwControlsAccepted;
  public int dwWin32ExitCode;
  public int dwServiceSpecificExitCode;
  public int dwCheckPoint;
  public int dwWaitHint;
  public int dwProcessId;
  public int dwServiceFlags;
}

const int SERVICE_WIN32_OWN_PROCESS = 0x00000010;

const int SERVICE_RUNS_IN_SYSTEM_PROCESS = 0x00000001;

public static void StopServiceAndWaitForExit(string serviceName)
{
  using (ServiceController controller = new ServiceController(serviceName))
  {
    SERVICE_STATUS_PROCESS ssp = new SERVICE_STATUS_PROCESS();
    int ignored;

    // Obtain information about the service, and specifically its hosting process,
    // from the Service Control Manager.
    if (!QueryServiceStatusEx(controller.ServiceHandle.DangerousGetHandle(), SC_STATUS_PROCESS_INFO, ref ssp, Marshal.SizeOf(ssp), out ignored))
      throw new Exception("Couldn't obtain service process information.");

    // A few quick sanity checks that what the caller wants is *possible*.
    if (ssp.dwServiceType != SERVICE_WIN32_OWN_PROCESS)
      throw new Exception("Can't wait for the service's hosting process to exit because there may be multiple services in the process (dwServiceType is not SERVICE_WIN32_OWN_PROCESS");

    if ((ssp.dwServiceFlags & SERVICE_RUNS_IN_SYSTEM_PROCESS) != 0)
      throw new Exception("Can't wait for the service's hosting process to exit because the hosting process is a critical system process that will not exit (SERVICE_RUNS_IN_SYSTEM_PROCESS flag set)");

    if (ssp.dwProcessId == 0)
      throw new Exception("Can't wait for the service's hosting process to exit because the process ID is not known.");

    // Note: It is possible for the next line to throw an ArgumentException if the
    // Service Control Manager's information is out-of-date (e.g. due to the process
    // having *just* been terminated in Task Manager) and the process does not really
    // exist. This is a race condition. The exception is the desirable result in this
    // case.
    using (Process process = Process.GetProcessById(ssp.dwProcessId))
    {
      // EDIT: There is no need for waiting in a separate thread, because MSDN says "The handles are valid until closed, even after the process or thread they represent has been terminated." ( http://msdn.microsoft.com/en-us/library/windows/desktop/ms684868%28v=vs.85%29.aspx ), so to keep things in the same thread, the process HANDLE should be opened from the process id before the service is stopped, and the Wait should be done after that.

      // Response to EDIT: What you report is true, but the problem is that the handle isn't actually opened by Process.GetProcessById. It's only opened within the .WaitForExit method, which won't return until the wait is complete. Thus, if we try the wait on the current therad, we can't actually do anything until it's done, and if we defer the check until after the process has completed, it won't be possible to obtain a handle to it any more.

      // The actual wait, using process.WaitForExit, opens a handle with the SYNCHRONIZE
      // permission only and closes the handle before returning. As long as that handle
      // is open, the process can be monitored for termination, but if the process exits
      // before the handle is opened, it is no longer possible to open a handle to the
      // original process and, worse, though it exists only as a technicality, there is
      // a race condition in that another process could pop up with the same process ID.
      // As such, we definitely want the handle to be opened before we ask the service
      // to close, but since the handle's lifetime is only that of the call to WaitForExit
      // and while WaitForExit is blocking the thread we can't make calls into the SCM,
      // it would appear to be necessary to perform the wait on a separate thread.
      ProcessWaitForExitData threadData = new ProcessWaitForExitData();

      threadData.Process = process;

      Thread processWaitForExitThread = new Thread(ProcessWaitForExitThreadProc);

      processWaitForExitThread.IsBackground = Thread.CurrentThread.IsBackground;
      processWaitForExitThread.Start(threadData);

      // Now we ask the service to exit.
      controller.Stop();

      // Instead of waiting until the *service* is in the "stopped" state, here we
      // wait for its hosting process to go away. Of course, it's really that other
      // thread waiting for the process to go away, and then we wait for the thread
      // to go away.
      lock (threadData.Sync)
        while (!threadData.HasExited)
          Monitor.Wait(threadData.Sync);
    }
  }
}

class ProcessWaitForExitData
{
  public Process Process;
  public volatile bool HasExited;
  public object Sync = new object();
}

static void ProcessWaitForExitThreadProc(object state)
{
  ProcessWaitForExitData threadData = (ProcessWaitForExitData)state;

  try
  {
    threadData.Process.WaitForExit();
  }
  catch {}
  finally
  {
    lock (threadData.Sync)
    {
      threadData.HasExited = true;
      Monitor.PulseAll(threadData.Sync);
    }
  }
}
19
ответ дан 8 December 2019 в 13:02
поделиться