Last active
October 10, 2017 21:04
-
-
Save Emzi0767/fde65142038146e519916945a465cfc4 to your computer and use it in GitHub Desktop.
Your daemon script is bad; here, hold this.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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