# Restart rport through a tunnel

### Problem

You want to restart the rport client, but you are connected via a tunnel (RDP, VNC or SSH). If you just execute a restart command, you will kill the current connection and the restart is also killed halfway. The client will not reconnect.

### Solution

You must restart the client with a small delay from a background process. This is done best from the rport script interface.

#### On Linux

On Linux, execute the following script:

{% code title="rport restart" %}

```bash
if [ "$(id -u)" -ne 0 ];then 
    echo "Not root. Please enable sudo";
    exit 1
fi
if which at >/dev/null 2>&1; then
    echo "$RESTART_CMD" | at now +1 minute
    echo "Restart of rport scheduled via atd."
else
    nohup sh -c "sleep 10;$RESTART_CMD" >/dev/null 2>&1 &
    echo "Restart of rport scheduled via nohup+sleep."
fi
```

{% endcode %}

Make sure, you enable sudo.&#x20;

<figure><img src="https://1574570054-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MekeI9EovpQqbUTQSdM%2Fuploads%2Fd5GJe2kOrrOu0ezsIr8j%2Fimage.png?alt=media&#x26;token=3598e454-4cfd-4fe2-a79b-6b041bbe2316" alt=""><figcaption><p>Restart rport over rport on Linux</p></figcaption></figure>

#### On Windows

On Windows, a few more lines of PowerShell are required to execute a task in the background. Execute the following script to safely restart rport.

{% code title="restart rport" %}

```powershell
function Invoke-Later {
    Param
    (
        [Parameter(Mandatory = $true)]
        [string] $ScriptBlock,
        [Parameter(Mandatory = $false)]
        [int] $Delay = 10,
        [Parameter(Mandatory = $false)]
        [string] $Description = "Background Task"
    )
    $taskName = 'Invoke-Later-' + (Get-Random)
    $taskFile = [System.Environment]::GetEnvironmentVariable('TEMP', 'Machine') + '\' + $taskName + '.ps1'
    $ScriptBlock.Split("`n") | ForEach-Object {
        if ($_)
        {
            $_.Trim() | Out-File -FilePath $taskFile -Append
        }
    }
    "Unregister-ScheduledTask -Taskname $( $taskName ) -Confirm:`$false" | Out-File -FilePath $taskFile -Append
    "Remove-Item `"$( $taskFile )`" -Force" | Out-File -FilePath $taskFile -Append
    $action = New-ScheduledTaskAction -Execute "powershell" -Argument "-ExecutionPolicy bypass -file $( $taskFile )"
    $trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddSeconds($Delay)
    $principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
    $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries
    $task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger -Settings $settings
    Register-ScheduledTask $taskName -InputObject $task
    Write-Output "* Task `"$( $Description )`" [$( $taskFile )] scheduled."
    Write-Output "  It will be executed within $( $Delay ) seconds."
}
Invoke-Later -Description "Restart RPort" -Delay 10 -ScriptBlock {
    Stop-Service rport
    Start-Service rport
}
```

{% endcode %}

Make sure you execute the script with PowerShell.

<figure><img src="https://1574570054-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MekeI9EovpQqbUTQSdM%2Fuploads%2FqRyEprvJJ42IdHdjh8Zj%2Fimage.png?alt=media&#x26;token=e04b5ef5-7b53-4716-9ddc-a8bb477ac02c" alt=""><figcaption><p>Restart rport over rport on Windows</p></figcaption></figure>
