4

Using the excellent answers from this thread I've been able to schedule booting into different OSes at different times of day.

However, I would like to get the community's advice on how to boot into Ubuntu on weekdays and Windows on weekends, without trial-and-erroring myself into a bricked laptop.

My existing /boot/grub/custom.cfg looks like this:

insmod datehook

if [ $MINUTE -lt 10 ]; then PADDING="0"; else PADDING=""; fi
TIME=$HOUR$PADDING$MINUTE

if [ $TIME -ge 0 -a $TIME -lt 559 ]; then
  set default="Windows Boot Manager (on /dev/nvme0n1p2)"
fi

if [ $TIME -ge 1000 -a $TIME -lt 1659 ]; then
  set default="Ubuntu"
fi

if [ $TIME -ge 1700 -a $TIME -lt 2359 ]; then
  set default="Windows Boot Manager (on /dev/nvme0n1p2)"
fi

Even if someone knows about a sandbox mode which will allow me to trial-and-error my way to an answer that'd be great, but I'd also appreciate some best-practice input from the community.

Thank you!

ignarukih
  • 143

1 Answers1

5

If you want to experiment with grub, you can use the grub command line (hit c when you are in grub menu). You should see the grub prompt :

grub>

If you want to be safe for experimenting, I would suggest using a virtual machine (with VirtualBox for example).

Once in grub command line interface, you can enter the commands like you would in your /boot/grub/custom.cfg.

grub> insmod datehook
grub> date

I haven't found the datehook module documentation ; however, the source code of datehook shows that a $WEEKDAY variable is available.

grub> echo $WEEKDAY

So, you can do

grub> if [ $WEEKDAY = "saturday" -o $WEEKDAY = "sunday"  ]; then
 echo "Setting Windows as default"
 set default="Windows Boot Manager (on /dev/nvme0n1p2)"
else
 echo "Setting Ubuntu as default"
 set default="Ubuntu"
fi

Be aware that $WEEKDAY might be localized, so test before using this code.

If you are not using a QWERTY keyboard, follow these instructions to set your keymap in grub : How to change grub command-line (grub shell) keyboard layout?

Max
  • 574
  • Thanks for this! I've since moved to macOS so have sidestepped the problem, but you've offered some great solutions and thoughts in any case. – ignarukih Jun 29 '20 at 02:12