0
votes

Some time a go (over a year I think) ive heard from a guy that it is possible, to automatically turn your VM in Microsoft azure at certain hour and turn it on also at certain hour. So I thought, I could use this mechanism to turn my Owncloud server off night as I dont use it anyways and It could reduce the billing. But now when I am in the Microsoft Azure panel, I cannot find this option anywhere... Could someone please point me out some info about this feature? I would really aprichiate all help.

4

4 Answers

0
votes

It is possible, but it requires Azure cmdlet script job.

Please check the link for step by step introduction: http://clemmblog.azurewebsites.net/using-azure-automation-start-und-stop-virtual-machines-schedule/

As a workaround, if your task does not require to be in a VM, you can consider Azure Scheduler for simple job. It works pretty reliable. https://msdn.microsoft.com/en-us/library/azure/dn495651.aspx

0
votes

You can achieve it following the next steps:

  • Log in into Azure Management Portal
  • Create a new Runbook: Add -> App Services -> Automation -> Runbook ->From Gallery. Find and select the template "Azure Automation Workflow to Schedule stopping of all Azure Virtual Machines"
  • Enter some data. Create a new automation account when asked for
  • Create a new Windows PowerShell credential based on an existing user with coadministrator privileges
  • Authoring the Runbook editing the credential name and Azure subscription name
  • Test & Publish the Runbook
  • Add a new schedule and configure it based on your needs

The same way that we use it to turn off the VMs automatically we can do it to turn it on. The detailed steps to turn it on/off are described here.

0
votes

There are several options:

  1. Write a simple azure power shell script and automate it to run at specified time.
  2. If you need your virtual machine only to run when you need it for remote session - try http://usevm.com1. It is a website that allows you to easily manage VMs in Azure including switching them off. Application does not support automatic switching yet, but there are some plans for that.

1. I am working for usevm.com

0
votes

I too ran into the problem of turning off Linux VMs automatically: I didn't want it to be on a schedule, but rather I wanted the VMs to deallocate when I'm not using them. To effectuate this I wrote a script that will do it for me. The script is below, and I also wrote a blog about setting it up and using it.

The script essentially checks if the VM has been up at least 10 minutes, and if the last SSH connection was closed at least 15 minutes ago (although you can adjust the timing). If these conditions are met, it will deallocate the VM. The script requires it have access to an Azure service principal for managing Resource Manager VMs in Azure and should be configured to run in a cron job every minute or so.

#!/bin/bash

### SCRIPT CONFIGURATION PARAMETERS
SPNAME="<spNameGUID>"
TENANT="<tenantGUID>"
PASSWORD=`cat pass.txt`

VMNAME=`cat /etc/hostname`
RESOURCEGROUP="${VMNAME}"

SSHTIMEOUT=15
MINSYSUPTIMEMS=600000
TEMPDIR=/tmp/autoshutdown
TEMPFILE="${TEMPDIR}/timetest"
### END SCRIPT CONFIGURATION PARAMETERS

UPTIME=`awk '{print $1*1000}' /proc/uptime`
NUMSSHCONNS=`ps auxwww | grep sshd: | grep -v grep | wc -l`

function dologin {
    azure login -u "${SPNAME}" -p `cat pass.txt` --service-principal --tenant "$TENANT"
}

function dodeallocate {
    echo "Deallocating VM..."
    dologin
    azure vm deallocate $RESOURCEGROUP $VMNAME
    exit
}

while test $# -gt 0
do
    case "$1" in
        --test)
            echo "Testing azure login..."
            dologin
            exit $?
            ;;
        --force)
            echo "Shutting down vm without testing parameters..."
            dodeallocate
            exit $?
            ;;
    esac
    shift
done

# If there are SSH connections, write the tempfile and exit
if [ $NUMSSHCONNS -gt 0 ]; then
    mkdir -p $TEMPDIR
    touch $TEMPFILE
    exit 0
fi

# Check the system uptime, and the time of the last SSH connection
if [ $UPTIME -gt $MINSYSUPTIMEMS ] && test `find ${TEMPFILE} -mmin +${SSHTIMEOUT}`; then
    dodeallocate
    exit $?
fi