paint-brush
How To Set Up Scheduling In GRUBby@OpenSchoolZ

How To Set Up Scheduling In GRUB

by Open School SolutionsApril 2nd, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

<a href="https://en.wikipedia.org/wiki/GNU_GRUB#Version_2_%28GRUB%29" target="_blank">GRUB 2</a> 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.

Company Mentioned

Mention Thumbnail
featured image - How To Set Up Scheduling In GRUB
Open School Solutions HackerNoon profile picture

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-digitif[ $MINUTE -lt 10 ]; then PADDING="0"; else PADDING=""; fiTIME=$HOUR$PADDING$MINUTE


# Start Ubuntu by defaultset default=0




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

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.

Originally published at openschoolsolutions.org on April 2, 2018. Sign up to our newsletter to get access to a FREE PDF with great open source apps for your classroom or follow @OpenSchoolZ on Twitter.