Skip to content

Instantly share code, notes, and snippets.

@Emzi0767
Last active October 10, 2017 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Emzi0767/fde65142038146e519916945a465cfc4 to your computer and use it in GitHub Desktop.
Save Emzi0767/fde65142038146e519916945a465cfc4 to your computer and use it in GitHub Desktop.
Your daemon script is bad; here, hold this.
# daemonize.ps1
#
# Daemonizes your Discord bot.
#
# Arguments:
# -Executable]
# Path to bot's executable.
#
# -Arguments
# Optional arguments for the bot.
#
# -NetCore
# Switches to .NET Core mode (i.e. executing via dotnet).
#
# Usage:
# .\daemonize.ps1 MyBot.exe
# .\daemonize.ps1 MyBot.dll -NetCore
# .\daemonize.ps1 MyBot.exe -Arguments --token="your.token.sucks"
# .\daemonize.ps1 MyBot.dll arguments for "the bot" -NetCore
# .\daemonize.ps1 -Executable MyBot.dll -NetCore
param
(
[parameter(Mandatory = $true)]
[string] $Executable,
[parameter(Mandatory = $false, ValueFromRemainingArguments = $true)]
[string] $Arguments,
[switch] $NetCore
)
if (-not Test-Path "$Executable")
{
Write-Host "Specified file ($Executable) does not exist."
$host.SetShouldExit(1)
Exit 1
}
$run = 1
while ($run)
{
if (-not $NetCore)
{
& "$Executable" $Arguments | Out-Host
}
else
{
& dotnet "$Executable" $Arguments | Out-Host
}
if ($LastExitCode -ne 0)
{
Write-Host "Bot crashed, restarting in 3s..."
Start-Sleep 5
}
else
{
Write-Host "Bot shut down, exiting..."
$run = 0
}
}
Exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment