0

I am experiencing issues with the wg-easy VPN service, as the VPS containers seem to go down frequently. I have set up a cron job to reboot every 2 hours, but it doesn't seem to help much.

I am also interested in setting up an auto reboot using a Powershell script on Automation Accounts, but I am not sure if my runbook script is correct. Here is the script I have written:

$resourceGroupName = "xxx-resource-group"
$vmNames = @(
    "xxx-virtual-machine",
    "xxx_virtual_machine",
    "xxx-virtual-machine"
)

$trigger = New-JobTrigger -Once -At (Get-Date).AddMinutes(2) -RepetitionInterval (New-TimeSpan -Hours 2) -RepetitionDuration ([TimeSpan]::MaxValue)

Register-ScheduledJob -ScriptBlock {
    Connect-AzAccount -Identity

    foreach ($vmName in $vmNames) {
        Restart-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
    }
} -Trigger $trigger

I would appreciate any help with troubleshooting the wg-easy VPN service issues and with verifying if my Powershell script is correct.

Thank you.

enter image description here

2 Answers 2

1

Setting up an auto reboot using a PowerShell script on Automation Accounts, but I am not sure if my runbook script is correct:

Your script looks good to me and below are the improvised versions of the script to make it work efficiently.

$resourceGroupName = "xxx"
$vmName = "latestj"

$trigger = New-JobTrigger -Once -At (Get-Date).AddMinutes(2) -RepetitionInterval (New-TimeSpan -Hours 2) -RepetitionDuration ([TimeSpan]::MaxValue)

Register-ScheduledJob -ScriptBlock {
    param (
        [string]$resourceGroupName,
        [string[]]$vmName
    )

    Connect-AzAccount -Identity
    Restart-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
} -Trigger $trigger -ArgumentList $resourceGroupName, $vmName

You can also use try catch blocks to check and handle errors.

enter image description here

$resourceGroupName = "xxx"
$vmNames = @(
    xxxx
)
$trigger = New-JobTrigger -Once -At (Get-Date).AddMinutes(2) -RepetitionInterval (New-TimeSpan -Hours 2) -RepetitionDuration ([TimeSpan]::MaxValue)
Register-ScheduledJob -ScriptBlock {
    Connect-AzAccount -Identity

    foreach ($vmName in $vmNames) {
        $vmdetails = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName -ErrorAction SilentlyContinue
        if ($vmdetails) {
            $Status = $vmdetails.ProvisioningState
            if ($Status -eq "Succeeded") {
                Restart-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
                Write-Host "Restarted"
            } else {
                Write-Host "Not in a running state"
            }
        } else {
            Write-Host "VM $vmName not found"
        }
    }
} -Trigger $trigger

Coming to the,

Issue with wg-easy VPN service:

Although using a cron job to reboot every 2 hours makes sure the service stays online, it's not the most feasible approach. It interrupts connections and poses the risk of data loss.

  • Check wg-easy VPN network configurations and also verify you are using the latest version of it.
  • Also check wg-easy logs and monitor the VPN usage that might cause these conflicts.
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks so much. I've just tried your approach, but the test shows a few errors :) System.Management.Automation.ParameterBindingValidationException: Cannot validate argument on parameter 'ArgumentList'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that does not contain any null values and then try the command again. ---> System.Management.Automation.ValidationMetadataException: The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that ....
Have you checked the values you are passing is correct? Resource group name and VMnames! @organicnz
Yeah, the script looks correct I copy pasted it. The script passed the test without any issues when I removed the rebooting part from the script :)
0

I wasn't able to solve the issue through the Powershell script and decided to make the docker.sh that reboots the VPN container. The cronjob initiates it.

crontab -e

# Ansible: a job to run the docker.sh script every few hours
0 */12 * * * /home/user/dev/scripts/docker.sh


docker.sh

#!/bin/bash

## VPN Startup Script
cd /home/user/dev/vpn && docker-compose up -d --force-recreate --remove-orphans


# Run Docker Compose pull in all subdirectories

for dir in "/home/user/dev"/*/; do
    [ -d "$dir" ] && cd "$dir" && docker compose pull --quiet

    # List all Docker images
    docker images -a

    # List Docker containers
    docker ps -a --format "{{.Names}}"

    # Delete unused Docker images
    for image in $(docker images -a -q); do
        if ! echo $(docker ps -a --format "{{.Names}}") | grep -q $(echo $image | awk -F ":" '{print $1}'); then
            echo "Deleting unused image: $image"
            docker rmi $image
        fi
    done

    cd "/home/user/dev"
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.