public void Create(string serviceName, string binPath, ServiceCreateOptions serviceCreateOptions = null) { string createServiceString = $"/c sc create \"{serviceName}\" binPath= \"{binPath}\" "; if (!string.IsNullOrEmpty(serviceCreateOptions?.DisplayName?.Trim())) { createServiceString = $"{createServiceString} DisplayName= \"{serviceCreateOptions.DisplayName}\""; } var serviceMode = "delayed-auto"; createServiceString = $"{createServiceString} start= \"{serviceMode}\""; var output = this.terminal.Execute(new ProcessInput { ShellName = "cmd", Arguments = createServiceString }); if (output.ExitCode != 0) { throw new SystemManagerException(string.Join(Environment.NewLine, output.StandardOutput)); } if (serviceCreateOptions?.Restart == true) { var restartTimeout = ""; for (int i = 0; i < 3; i++) { restartTimeout += $"restart/{serviceCreateOptions.RestartSeconds * 1000}/"; } restartTimeout = restartTimeout.Remove(restartTimeout.Length - 1, 1); string serviceRestart = $"/c sc failure \"{serviceName}\" reset= 60 actions= \"{restartTimeout}\" "; output = this.terminal.Execute(new ProcessInput { ShellName = "cmd", Arguments = serviceRestart }); if (output.ExitCode != 0) { throw new SystemManagerException(string.Join(Environment.NewLine, output.StandardOutput)); } } }
public void Create(string serviceName, string binPath, ServiceCreateOptions serviceCreateOptions = null) { try { var description = string.IsNullOrEmpty(serviceCreateOptions?.Description?.Trim()) ? serviceName : serviceCreateOptions.Description; var wantedBy = "multi-user.target"; var restartSeconds = serviceCreateOptions == null ? 5 : serviceCreateOptions.RestartSeconds; var restart = RestartConstants.Always; //https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-managing_services_with_systemd-unit_files var serviceFile = $"[Unit]{Environment.NewLine}"; serviceFile += $"Description=\"{description}\"{Environment.NewLine}"; serviceFile += $"[Service]{Environment.NewLine}"; serviceFile += $"ExecStart={binPath}{Environment.NewLine}"; if (!string.IsNullOrEmpty(serviceCreateOptions?.WorkingDirectory)) { serviceFile += $"WorkingDirectory={serviceCreateOptions?.WorkingDirectory}{Environment.NewLine}"; } if (serviceCreateOptions.Restart) { serviceFile += $"Restart={restart}{Environment.NewLine}"; serviceFile += $"RestartSec={restartSeconds}{Environment.NewLine}"; } serviceFile += $"[Install]{Environment.NewLine}"; serviceFile += $"WantedBy={wantedBy}{Environment.NewLine}"; var outputFilePath = $"/etc/systemd/system/{serviceName}{(serviceName.Contains(".service") ? "" : ".service")}"; using (var f = File.Open(outputFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.None)) using (var w = new StreamWriter(f)) { w.Write(serviceFile); w.Flush(); } Enable(serviceName); ReloadDaemon(); } catch (IOException ioe) { throw new SystemManagerException("Cannot make service", ioe); } }
public void Create(string serviceName, string binPath, ServiceCreateOptions serviceCreateOptions) { serviceManagerFactory .Create(systemInformation.GetOSInfo().Platform) .Create(serviceName, binPath, serviceCreateOptions); }