For example:
00:00:00 to 06:00:00 -> Slitaz
06:00:01 to 13:00:00 -> Ubuntu
13:00:01 to 19:00:00 -> Fedora
19:00:01 to 23:59:59 -> openSUSE
Can grub change default 'entry' automatically?
For example:
00:00:00 to 06:00:00 -> Slitaz
06:00:01 to 13:00:00 -> Ubuntu
13:00:01 to 19:00:00 -> Fedora
19:00:01 to 23:59:59 -> openSUSE
Can grub change default 'entry' automatically?
First, run
grep -E '(menuentry |submenu )' /boot/grub/grub.cfg
to get a list of your grub menu entries. You should see something like:Here you can see my first menu entry is Ubuntu, followed by a Advanced options for Ubuntu submenu (with four other entries), 2 memory tests and, at last, Windows 7.
If we create a file named
/boot/grub/custom.cfg
, it will be loaded after/boot/grub/grub.cfg
, so we can easily change GRUB's default configuration.I used GRUB's module
datehook
to get the current time./boot/grub/custom.cfg
:The module
datehook
makes available the variables: DAY, HOUR, MINUTE, MONTH, SECOND, WEEKDAY and YEAR, which return the actual date/time values based on hardware clock.Let's take
if [ $TIME -ge 600 -a $TIME -lt 1659 ]; then
as an example. It means: if the current time is greater than or equal to 6AM and less than 4:59PM (16:59) then execute the next command (set default="Windows 7 (loader) (on /dev/sda2)"
), which set thedefault
variable with the Windows 7 menu entry name taken from thatgrep
command above.The last
if
block exemplifies the selection of a submenu entry. In that case "Ubuntu, with Linux 3.16.0-25-generic" lies inside a submenu that is the second entry in the main menu. As an entry position in a menu starts from 0, the menu entry named "Ubuntu" is0
and the "Advanced options for Ubuntu" submenu is1
, that's why I had to add1>
before the entry name:set default="1>Ubuntu, with Linux 3.16.0-25-generic"
.There no need to run
update-grub
.The hardware clock may be unreliable, specially if the battery is dead. Also, enter BIOS setup and check the time. If it is UTC you'll have to change the time range in the script.
For the beginning, run the following command in terminal:
This will return a list of your grub menu entries. I assume that in your case this list is something like this:
Now, for each of these entries you should assign a number in ascending order starting with 0 (for "Slitaz" -
0
, for "Advanced options" -1
and so on). You will use this numbers to set default entry in grub menu.Next, and the last thing, you should edit
/boot/grub/grub.cfg
file as follow:From terminal open in gedit the file using:
Find the line where
default
variable is set; it should look something similar with:Replace the above line with next code:
Save the file and close it.
That's all! Restart your PC and check if it is working.
Source of inspiration: Scripting a Simple Boot Time State Machine in GRUB2.
Other sources:
You have 2 possibilities. First, you can add to every OS a cron script that changes config by clock. However, you will not like what will happen if this script kicks in while OS is shutting down, so beware. A better idea would be to acquire a UEFI motherboard for your PC. There you can install Shellx86 which is a pre-bootloader scripting console. Creating script for what you want in Shellx64 is no harder than scripting in bash. I used it to boot different OS depending on if you have Internet connection.