Start and stop a service from app?

Has anyone shelled out to start / stop services on windows, mac, linux etc?
I want to update a database but need to make sure the server is stopped before modifying its database.

On windows, you can use the SC command, but it requires elevated permissions.

I wrote this method to create a Windows Service, start it and open the firewall to the application. You could easily add a stop command.
I would love to know how to do the equivalent on macOS and Linux.

[code]Protected Function getCreateWindowsServiceWAD(appFile As FolderItem, serviceName As String = “”, startService As Boolean = False , autoStart As Boolean = True, failureRestart As Boolean = True, failureRestartResetSeconds As Integer = 120, firewallIn As Boolean = True, firewallOut As Boolean = True) as String
Dim tempString As String 'Used by Windows
Dim tempShell As New Shell 'Used by Windows
Dim returnResults As String

if appfile = nil or not appFile.Exists then Return “Application not found”

if serviceName = “” then serviceName = appfile.NameWithoutExtensionMBS
serviceName = ReplaceAll(serviceName, " ", “”) 'in case there are spaces in the file name!

#if TargetWin32 then
'stop any old Windows Service
'tempString = "sc stop " + serviceName
'tempShell.Execute(tempString)
'if tempShell.ErrorCode <> 0 then returnResults = returnResults + tempShell.Result + EndOfLine

''delete any old Windows Service
'tempString = "sc delete " + serviceName
'tempShell.Execute(tempString)
'if tempShell.ErrorCode <> 0 then returnResults = returnResults + tempShell.Result + EndOfLine

'create the new Service
tempString = "sc create " + serviceName + " binpath= " + chr(34) + appfile.NativePath + chr(34) + if(autoStart, " start= auto", "")
tempShell.Execute(tempString)
if tempShell.ErrorCode <> 0 then returnResults = returnResults + tempShell.Result + EndOfLine

'set the Service to restart twice after two seconds then if it still fails then wait for failureRestartResetSeconds seconds
if failureRestart then
  tempString = "sc failure " + serviceName + " actions= restart/2000/restart/2000// reset= " + str(failureRestartResetSeconds)
  tempShell.Execute(tempString)
  if tempShell.ErrorCode <> 0 then returnResults = returnResults + tempShell.Result + EndOfLine
end if

if startService then
  tempString = "sc start " + serviceName
  tempShell.Execute(tempString)
  if tempShell.ErrorCode <> 0 then returnResults = returnResults + tempShell.Result + EndOfLine
end if

'add the application to the Windows Firewall for incoming traffic
if firewallIn then
  tempString = "netsh advfirewall firewall add rule name=" + chr(34) + serviceName + chr(34) + " dir=in action=allow program=" + chr(34) + appfile.NativePath + chr(34) + " enable=yes "
  tempShell.Execute(tempString)
  if tempShell.ErrorCode <> 0 then returnResults = returnResults + tempShell.Result + EndOfLine
end if

'add the application to the Windows Firewall for outgoing traffic
if firewallIn then
  tempString = "netsh advfirewall firewall add rule name=" + chr(34) + serviceName + chr(34) + " dir=out action=allow program=" + chr(34) + appfile.NativePath + chr(34) + " enable=yes "
  tempShell.Execute(tempString)
  if tempShell.ErrorCode <> 0 then returnResults = returnResults + tempShell.Result + EndOfLine
end if

#endif

if returnResults = “” then
Return “No error”
else
Return returnResults
end if

Exception err
commonOS.doHandleExceptionWAD(err, CurrentMethodName)

End Function
[/code]

Wow! Thank you!

The second

if firewallIn then …

should be

if firewallOut then …

Sorry.

Roger that David. Thanks.