1. Create a new folder where you’d like Sonarr-4K to live. Most use a similar place such as
    C:\ProgramData\Sonarr-4K
  2. Back in Command Prompt, create the new Sonarr-4K service using nssm install Sonarr-4K. A popup window will open where you can type your parameters for the new instance. For this example, we will use the

    following:

Note that Arguments points to the new folder created in step 1. This is crucial, as it keeps all the data files from both instances in

separate locations.

  1. Click Install service. The window should close and the service
    will now be available to run.
  2. Continue to Configuring Sonarr-4k
  1. Create a new File and name it SonarrInstancesChecker.ps1 with the below code.
  2. Edit the script with your actual service names, IP, and ports.
  3. Create a scheduled task to run the script on a repeating schedule.
################################################################################################
### SonarrInstancesChecker.ps1                                                               ###
################################################################################################
### Keeps multiple Sonarr Instances up by checking the port                                  ###
### Please use Sonarr´s Discord or Reddit for support!                                       ###
### https://wiki.servarr.com/sonarr/installation#windows-multi                               ###
################################################################################################
### Version: 1.1                                                                             ###
### Updated: 2020-10-22                                                                      ###
### Author:  reloxx13                                                                        ###
################################################################################################



### SET YOUR CONFIGURATION HERE ###
# Set your host ip and port correctly and use your service or scheduledtask names!

# (string) The type how Sonarr is starting
# "Service" (default) Service process is used
# "ScheduledTask" Task Scheduler is used
$startType = 'Service'

# (bool) Writes the log to C:\Users\YOURUSERNAME\log.txt when enabled
# $false (default)
# $true
$logToFile = $false


$instances = @(
    [pscustomobject]@{   # Instance 1
        Name = 'Sonarr-V3'; # (string) Service or Task name (default: Sonarr-V3)
        IP   = '192.168.178.12'; # (string) Server IP where Sonarr runs (default: 192.168.178.12)
        Port = '7873'; # (string) Server Port where Sonarr runs (default: 7873)
    }
    [pscustomobject]@{   # Instance 2
        Name = 'Sonarr-4K'; # (string) Service or Task name (default: Sonarr-4K)
        IP   = '192.168.178.12'; # (string) Server IP where Sonarr runs (default: 192.168.178.12)
        Port = '7874'; # (string) Server Port where Sonarr runs (default: 7874)
    }
    # If needed you can add more instances here... by uncommenting out the below lines
    # [pscustomobject]@{   # Instance 3
    # Name='Sonarr-3D';    # (string) Service or Task name (default: Sonarr-3D)
    # IP='192.168.178.12'; # (string) Server IP where Sonarr runs (default: 192.168.178.12)
    # Port='7875';         # (string) Server Port where Sonarr runs (default: 7875)
    # }
)


### DONT CHANGE ANYTHING BELOW THIS LINE ###


###
# This function will write to a log file or in console output
###
function Write-Log
{
    #Will write to C:\Users\YOURUSERNAME\log.txt
    
    Param(
        $Message,
        $Path = "$env:USERPROFILE\log.txt" 
    )

    function TS { Get-Date -Format 'hh:mm:ss' }
    
    #Console output
    Write-Output "[$(TS)]$Message"
    
    #File Output
    if ($logToFile)
    {
        "[$(TS)]$Message" | Tee-Object -FilePath $Path -Append | Write-Verbose
    }
}


Write-Log 'START ====================='


$instances | ForEach-Object {
    Write-Log "Check $($_.Name) $($_.IP):$($_.Port)"
    
    $PortOpen = ( Test-NetConnection $_.IP -Port $_.Port -WarningAction SilentlyContinue ).TcpTestSucceeded 
    
    if (!$PortOpen)
    {
        Write-Log "Port $($_.Port) is closed, restart $($startType) $($_.Name)!"

        if ($startType -eq 'Service')
        {
            Get-Service -Name $_.Name | Stop-Service
            Get-Service -Name $_.Name | Start-Service
        }
        elseif ($startType -eq 'ScheduledTask')
        {
            Get-ScheduledTask -TaskName $_.Name | Stop-ScheduledTask
            Get-ScheduledTask -TaskName $_.Name | Start-ScheduledTask
        }
        else
        {
            Write-Log '[ERROR] STARTTYPE UNKNOWN! USE Service or ScheduledTask !'
        }
    }
    else
    {
        Write-Log "Port $($_.Port) is open!"
    }
}

Write-Log 'END ====================='

Below is an example script to create a Sonarr4K instance. The below systemd creation script will use a data directory of /var/lib/sonarr4k/. Ensure the directory exists or modify it as needed.

cat << EOF | sudo tee /etc/systemd/system/sonarr4k.service > /dev/null
[Unit]
Description=Sonarr4k Daemon
After=syslog.target network.target
[Service]
User=sonarr
Group=media
Type=simple

ExecStart=/opt/Sonarr/Sonarr -nobrowser -data=/var/lib/sonarr4k/
TimeoutStopSec=20
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl -q daemon-reload
sudo systemctl enable --now -q sonarr4k