Script: Power/Switch on server by schedule


Recommended Posts

You need to enable RTC wake up in your BIOS. After that use the User Scripts Plugin to create a script and let it execute hourly.

 

Script:

#!/bin/bash

# #####################################
# ######### Settings ##################
# #####################################

times=(
  20:00
  08:00
)

# #####################################
# ######### Script ####################
# #####################################

# sort times
readarray -t times < <(for a in "${times[@]}"; do echo "$a"; done | sort)

# delete wakealarm
echo 0 > /sys/class/rtc/rtc0/wakealarm

# loop through times
for time in "${times[@]}"; do

  # obtain current and wakealarm timestamps
  now=$(date +%s)
  fut=$(date -d $time +%s)

  # does the wakealarm happen today?
  if [[ now -ge fut ]]; then
    fut=''
    continue
  fi

  # set wakealarm for today
  echo $fut > /sys/class/rtc/rtc0/wakealarm
  logger -t wakealarm "set wakealarm to "$(date -d "@$fut")
  exit

done

# set wakealarm for tomorrow
if [[ -z $fut ]]; then
  fut=$(date -d "tomorrow ${times[0]}" +%s)
  echo $fut > /sys/class/rtc/rtc0/wakealarm
  logger -t wakealarm "set wakealarm to "$(date -d "@$fut")
fi

 

 

 

The poweroff should be done in a separate script after your task has been finished or maybe on a specific time by executing a separate script with a specific crontab schedule. That command is simple:

shutdown

 

Link to comment
  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.