Start and restart Windows self-hosted runner automatically
Asked by fernando_arias:

Hi  

I've successfully set up a self-hosted runner on a Windows Server 2019 VM. How would you set it up so that it starts on boot and restarts on failure?


Answers:
author: fernando_arias
body: Ok, this is what I did. Assuming we have the agent in d:\\bb...

Use **procmon** (<https://commons.apache.org/proper/commons-daemon/procrun.html>):

Download for windows: *commons-daemon-1.3.2-bin-windows.zip* from: <https://downloads.apache.org/commons/daemon/binaries/windows/>

Then copy these files (I use the x64 version):

```
Copy-Item amd64/prunsrv.exe d:\bb\bin\
Copy-Item prunmgr.exe d:\bb\bin\
New-Item -ItemType Directory -Path d:\bb\logs
```

<br />

Create an *install.ps1* file in d:\\bb\\bin

```
param (
    [string]$accountUuid = $(throw "-accountUuid is required."),
    [string]$repositoryUuid = "",
    [string]$runnerUuid = $(throw "-runnerUuid is required."),
    [string]$OAuthClientId = $(throw "-OAuthClientId is required."),
    [string]$OAuthClientSecret = $(throw "-OAuthClientSecret is required."),
    [string]$workingDirectory = $(throw "-workingDirectory is required."),
    [string]$serviceUser = $(throw "-serviceUser is required."),
    [string]$servicePassword = $(throw "-servicePassword is required."),
    [string]$runnerEnvironment = "PRODUCTION",
    [string]$scheduledStateUpdateInitialDelaySeconds = "0",    
    [string]$scheduledStateUpdatePeriodSeconds = "30",
    [string]$cleanupPreviousFolders = "false",
    [string]$logPath = "../logs"
)

if (-not (Get-Command -name "java" -CommandType Application -errorAction SilentlyContinue))
{
    Write-Error "Java not installed"
    break
}

$argList = @("install",
            "BitBucketRunner",
            "--Install=D:\bb\bin\prunsrv.exe",
            "--Startup=auto",
            "--Classpath=runner.jar",
            "--StartClass=com.atlassian.pipelines.runner.core.ApplicationImpl",
            "--StartMethod=main",
            "--StopClass=com.atlassian.pipelines.runner.core.ApplicationImpl",
            "--StopMethod=main",
            '--DisplayName="Bitbucket Runner"',
            '--Description="Bitbucket Runner Service"',
            "--StartMode=jvm",
            "--StopMode=jvm",
            "--Jvm=auto",
            "--ServiceUser=$serviceUser",
            "--ServicePassword=$servicePassword",
            "++JvmOptions=-Dbitbucket.pipelines.runner.account.uuid=$accountUuid",
            "++JvmOptions=-Dbitbucket.pipelines.runner.repository.uuid=$repositoryUuid",
            "++JvmOptions=-Dbitbucket.pipelines.runner.uuid=$runnerUuid",
            "++JvmOptions=-Dbitbucket.pipelines.runner.environment=$runnerEnvironment",
            "++JvmOptions=-Dbitbucket.pipelines.runner.oauth.client.id=$OAuthClientId",
            "++JvmOptions=-Dbitbucket.pipelines.runner.oauth.client.secret=$OAuthClientSecret",
            "++JvmOptions=-Dbitbucket.pipelines.runner.directory.working=$workingDirectory",
            "++JvmOptions=-Dbitbucket.pipelines.runner.runtime=windows-powershell",
            "++JvmOptions=-Dbitbucket.pipelines.runner.scheduled.state.update.initial.delay.seconds=$scheduledStateUpdateInitialDelaySeconds",
            "++JvmOptions=-Dbitbucket.pipelines.runner.scheduled.state.update.period.seconds=$scheduledStateUpdatePeriodSeconds",
            "++JvmOptions=-Dbitbucket.pipelines.runner.cleanup.previous.folders=$cleanupPreviousFolders"
            "++JvmOptions=-Dfile.encoding=UTF-8",
            "++JvmOptions=-Dsun.jnu.encoding=UTF-8",
            "--JvmMs=256",
            "--JvmMx=1024",
            "--JvmSs=4000",
            "--LogPath=$logPath",
            "--LogPrefix=bitbucket-agent",
            "--StdOutput=$logPath\stdout.log",
            "--StdError=$logPath\stderr.log")

$params = @{
    FilePath = "prunsrv"
    ArgumentList = $argList
}

$p = Start-Process @params -NoNewWindow -Wait
```

<br />

Create *uninstall.ps1* in D:\\bb\\bin

```
$argList = @("delete",
            "BitBucketRunner")

$params = @{
    FilePath = "prunsrv"
    ArgumentList = $argList
}

$p = Start-Process @params -NoNewWindow -Wait
```

<br />

CD into D:\\bb\\bin and run

```
.\install.ps1 -accountUuid '{*****}' -runnerUuid '{*****}' -OAuthClientId ***** -OAuthClientSecret ***** -workingDirectory '..\temp' -serviceUser DOMAIN\user -servicePassword ***
```

comments: author: sebastien_ezfocus
body: I can't get my service to start after following this installation procedure. procmon requires an Oracle JDK properly installed in the registry but Bitbucket runner only works with OpenJDK. How did you do it? Thanks.


author: fernando_arias
body: I used the Eclipse Adoptium Java version and then

1. Added this JAVA_HOME to environment variables:

C:\\Program Files\\Eclipse Adoptium\\jdk-11.0.16.101-hotspot

2. Added to PATH:

C:\\Program Files\\Eclipse Adoptium\\jdk-11.0.16.101-hotspot\\bin


author: sebastien_ezfocus
body: Thank you very much!


author: Fabio Racobaldo _Herzum_
body: Hi [@fernando_arias](/t5/user/viewprofilepage/user-id/4960946) ,

welcome to the Atlassian community!

You need to install a service. Please take a look to the following article <https://confluence.atlassian.com/bitbucketserver/start-and-stop-bitbucket-776640144.html>

Hope this helps,

Fabio

comments: author: fernando_arias
body: Thanks, Fabio

Looks like the answer is for running Bitbucket as a service, but I'm talking about a Bitbucket Windows runner.


Tags: bitbucket-cloud

bitbucket-pipelines

cloud

windows