RunAsService - mdaneri/Pode GitHub Wiki
Pode provides built-in functions to easily manage services across platforms (Windows, Linux, macOS). These functions allow you to register, start, stop, suspend, resume, query, and unregister Pode services in a cross-platform way.
The Register-PodeService function creates the necessary service files and configurations for your system.
Register-PodeService -Name "HelloService" -Description "Example Pode Service" -ParameterString "-Verbose" -StartThis registers a service named "HelloService" and starts it immediately after registration. The service runs your Pode script with the specified parameters.
The Register-PodeService function offers several parameters to customize your service registration:
-
-Name(string): The name of the service to register. Mandatory. -
-Description(string): A brief description of the service. Defaults to"This is a Pode service.". -
-DisplayName(string): The display name for the service (Windows only). Defaults to"Pode Service($Name)". -
-StartupType(string): Specifies the startup type of the service ('Automatic'or'Manual'). Defaults to'Automatic'. -
-ParameterString(string): Additional parameters to pass to the worker script when the service is run. Defaults to an empty string. -
-LogServicePodeHost(switch): Enables logging for the Pode service host. -
-ShutdownWaitTimeMs(int): Maximum time in milliseconds to wait for the service to shut down gracefully before forcing termination. Defaults to30,000 ms. -
-StartMaxRetryCount(int): Maximum number of retries to start the PowerShell process before giving up. Defaults to3. -
-StartRetryDelayMs(int): Delay in milliseconds between retry attempts to start the PowerShell process. Defaults to5,000 ms. -
-WindowsUser(string): Specifies the username under which the service will run. Defaults to the current user (Windows only). -
-LinuxUser(string): Specifies the username under which the service will run. Defaults to the current user (Linux Only). -
-Agent(switch): Create an Agent instead of a Daemon in MacOS (MacOS Only). -
-Start(switch): Starts the service immediately after registration. -
-Password(securestring): A secure password for the service account (Windows only). If omitted, the service account will be'NT AUTHORITY\SYSTEM'. -
-SecurityDescriptorSddl(string): A security descriptor in SDDL format specifying the permissions for the service (Windows only). -
-SettingsPath(string): Directory to store the service configuration file (<name>_svcsettings.json). Defaults to a directory under the script path. -
-LogPath(string): Path for the service log files. Defaults to a directory under the script path.
You can start a registered service using the Start-PodeService function.
Start-PodeService -Name "HelloService"This returns $true if the service starts successfully, $false otherwise.
To stop a running service, use the Stop-PodeService function.
Stop-PodeService -Name "HelloService"This returns $true if the service stops successfully, $false otherwise.
Suspend a running service (Windows only) with the Suspend-PodeService function.
Suspend-PodeService -Name "HelloService"This pauses the service, returning $true if successful.
Resume a suspended service (Windows only) using the Resume-PodeService function.
Resume-PodeService -Name "HelloService"This resumes the service, returning $true if successful.
To check the status of a service, use the Get-PodeService function.
Get-PodeService -Name "HelloService"This returns a hashtable with the service details:
Name Value
---- -----
Status Running
Pid 17576
Name HelloService
Sudo TrueRestart a running service using the Restart-PodeService function.
Restart-PodeService -Name "HelloService"This stops and starts the service, returning $true if successful.
When you no longer need a service, unregister it with the Unregister-PodeService function.
Unregister-PodeService -Name "HelloService" -ForceThis forcefully stops and removes the service, returning $true if successful.
If the Pode functions are unavailable or you prefer manual management, you can use traditional methods to configure Pode as a service.
To use NSSM for Pode as a Windows service:
-
Install NSSM using Chocolatey:
choco install nssm -y -
Configure the service:
$exe = (Get-Command pwsh.exe).Source $name = 'Pode Web Server' $file = 'C:\Pode\Server.ps1' $arg = "-ExecutionPolicy Bypass -NoProfile -Command `"$($file)`"" nssm install $name $exe $arg nssm start $name
-
Stop or remove the service:
nssm stop $name nssm remove $name confirm
To configure Pode as a Linux service:
-
Create a service file:
sudo vim /etc/systemd/system/pode-server.service
-
Add the following configuration:
[Unit] Description=Pode Web Server After=network.target [Service] ExecStart=/usr/bin/pwsh -c /usr/src/pode/server.ps1 -nop -ep Bypass Restart=always [Install] WantedBy=multi-user.target Alias=pode-server.service
-
Start and stop the service:
sudo systemctl start pode-server sudo systemctl stop pode-server
For privileged ports, consider:
-
Reverse Proxy: Use Nginx to forward traffic from port 443 to an unprivileged port.
-
iptables Redirection: Redirect port 443 to an unprivileged port:
sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 8080
-
setcap Command: Grant PowerShell permission to bind privileged ports:
sudo setcap 'cap_net_bind_service=+ep' $(which pwsh)
-
Authbind: Configure Authbind to allow binding to privileged ports:
authbind --deep pwsh yourscript.ps1