How To Set Up Scheduling In GRUB

Published by Stephan on

GRUB 2 has outsourced many functions into modules. This makes it possible to load only the required functions and thus smaller core images are possible. You can also use these modules to extend GRUB, e.g. to implement scheduling in GRUB. This makes it possible to start an particular operating system at a certain time.

You get a list of all available modules with

$ ls /boot/grub/i386-pc/

Configuring Scheduling in GRUB

For the scheduling in GRUB you need the module datehook. This allows access to the following variables in GRUB:

  • DAY
  • HOUR
  • MINUTE
  • SECOND
  • MONTH
  • WEEKDAY
  • YEAR

All these values are based on the time and date set in the BIOS (usually the mainboard hardware clock). You have to keep that in mind when setting up the scheduling in GRUB.

With the following command you get a list with all menu items of the GRUB menu:

$ grep -E '^menuentry|^submenu' /boot/grub/grub.cfg | cut -d '"' -f2 | cut -d "'" -f2

The first entry has the number 0, the second the number 1 and so on.

Next open the file /boot/grub/grub.cfg and add the following lines relative to the beginning of the file:

insmod datehook
# Add an extra zero for minute 0-9, so MINUTE is always double-digit
if[ $MINUTE -lt 10 ]; then PADDING="0"; else PADDING="""; fi
TIME=$HOUR$PADDING$MINUTE

# Start Ubuntu by default
set default=0

# Start Windows 10 from 5:00 p.m. to 11:00 p.m.
if[ $TIME -ge 1700 -a $TIME -lt 2300 ]; then
set default=3
fi

At the beginning, an extra zero is inserted for minutes smaller than 10, so that the number of minutes is always two digits. Then we construct a new variable TIME, which has the current time in the format HHMM. These can now be used to implement scheduling in GRUB.

Ubuntu should be started by default, but in the evening Windows 10 takes precedence. It may be necessary to adjust the times, since the time zone UTC is often set up in the BIOS.

Conclusion

The changes above are only valid until the next time update-grub is executed. To keep the changes in GRUB permanently, you should create a template with this code in /etc/grub.d/.

We use this option to better manage our computers at school. Ubuntu is started by default during the day, but during the night we let Linbo boot so that the computers can be updated or provided with a new image, for example.

Categories: HowTo

Stephan

I'm a teacher and IT system administrator in an international school. I love open source software and I used it over a decade in my private and work life. My passion is to solve problems with open source software!

1 Comment

How can I prevent myself from booting a certain distro in some particular hours? · July 24, 2022 at 1:52 pm

[…] it a look on this ubuntu thread for some more practical hints or to this post for a simple panorama. Hint relays on the datehook module and the config file […]

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *