17

You can put "panic=N" on the kernel command line to make the system reboot N seconds after a panic.

But is there a config option to specify this (other than the default kernel command line option) before even boot loader comes into a play? Some kernel option may be?

poige
  • 6,231
Shawn J. Goff
  • 46,081
  • Through the kernel watchdog subsystem, perhaps? – Gilles 'SO- stop being evil' Jan 20 '12 at 17:05
  • config file is boot (grub) configuration file itself, since it is a parameter invoked at the boot time and grub cannot be expected to read from some other config file while the filesystem is not mounted. – Nikhil Mulley Jan 20 '12 at 17:20
  • I presume you'll want to reboot to a different kernel? That's going to require some cooperation from the bootloader, and at that point, you'll surely be able to pass command line arguments. Some bootloaders can be set up to reboot to a different kernel if a boot fails (by having a userland program indicate to the bootloader that the boot succeeded). – Gilles 'SO- stop being evil' Jan 21 '12 at 14:49
  • Yes, I'll be booting either to a different kernel, or giving the kernel a different rootfs path. There is a variable in the bootloader's config that gets changed when userspace boots successfully; if it doesn't change, it knows to try something different. I was hoping for something more than just the kernel command line for this because the bootloader's config can be modified by the Linux userspace. If there's a problem, it's likely that userspace started writing, then the device powered off; and there is a fallback default config for that; I'm still looking for the most solid I can get this. – Shawn J. Goff Jan 21 '12 at 15:00
  • You might want to boot to the recovery kernel (or a complete recovery system) by default and use a oneshot reboot to the development kernel explicitly. – Pavel Šimerda Apr 22 '20 at 11:32

9 Answers9

20

From man proc:

/proc/sys/kernel/panic

This file gives read/write access to the kernel variable panic_timeout. If this is zero, the kernel will loop on a panic; if nonzero it indicates that the kernel should autoreboot after this number of seconds. When you use the software watchdog device driver, the recommended setting is 60.

jpalecek
  • 333
  • I'm looking for a kernel config option here, not something from userspace. Specifically, if for some reason, it can't mount the root filesystem (or for some other reason, userspace never comes up), I need it to reboot. – Shawn J. Goff Jan 20 '12 at 16:36
  • @ShawnJ.Goff If you're also concerned about userspace not coming up, then you're asking the wrong question (you want to reboot even if the kernel doesn't panic). And the answer is a form of watchdog by definition; you need to activate the watchdog subsystem (triggering a reboot if /dev/watchdog hasn't been touched in a while). See Documentation/watchdog/watchdog-api.txt. – Gilles 'SO- stop being evil' Jan 21 '12 at 14:47
  • 1
    Yes, I do have a watchdog. I'm just putting as many safety nets in place as possible. – Shawn J. Goff Jan 21 '12 at 14:49
  • Any way to shutdown instead of reboot? – Ciro Santilli OurBigBook.com Apr 20 '18 at 19:39
  • @ShawnJ.Goff boot loader supports passing down this parameter long before userspace is booted. Anyways, see my answer: https://unix.stackexchange.com/a/517364/6622 – poige May 06 '19 at 09:58
6

The config file is boot (grub) configuration file itself, since it is a parameter invoked at the boot time and grub cannot be expected to read from some other config file while the filesystem is not mounted.

However, that being a initialized setting, the runtime can also be modified through sysctl. So, essentially updating /etc/sysctl.conf with parameter kernel.panic = 3 is a configuration update.

  • 1
    grub is not on all systems; the one I'm working with is not using grub. My bootloader's config is stored in flash. – Shawn J. Goff Jan 20 '12 at 17:29
  • yeah, still.. can it mount the filesystem and read the kernel config file? if yes, then there you go. Because its the boot loader which passes the kernel parameters to the kernel while loading the kernel. – Nikhil Mulley Jan 20 '12 at 17:42
  • if you have a sysctl.d structure without sysctl.conf, put kernel.panic = 3 inside a new file within that folder, e.g. /etc/sysctl.d/99-panic.conf (# echo kernel.panic = 3 > /etc/sysctl.d/99-panic.conf) – EkriirkE Dec 13 '23 at 11:22
3

kernel.panic is a sysctl. There are many ways to configure these, for example through sysctl.d.

Tobu
  • 6,593
3

In Linux Kernel, (I've seen in 3 and above) there's option in the .config. CONFIG_PANIC_TIMEOUT is the parameter and defaults to 0. In these versions of Linux kernel, Lekensteyn's answer will also work. But that variable is taking from the .config only.

int panic_timeout = CONFIG_PANIC_TIMEOUT;
RatDon
  • 281
2

There does not seem to be such a config option. The default timeout is 0 which according to http://www.mjmwired.net/kernel/Documentation/kernel-parameters.txt#1898 is "wait forever".

The option is defined in kernel/panic.c, you can write a patch that sets the initial value to something different.

To hardcode a reboot after 3 seconds, change:

int panic_timeout;

to:

int panic_timeout = 3;
Lekensteyn
  • 20,830
  • 1
    It's poor advice to tinker with sources when there's special mechanism: https://unix.stackexchange.com/a/517364/6622 – poige May 06 '19 at 09:55
  • @poige Maybe it isn't! You are only tinkering with a pretty much standalone part of the code, not affecting other parts. In some build systems it might be much trickier to set the command line than make a small change to the sources that you are editing anyway. If you mess with the boot args, the machine might not boot. – Pavel Šimerda Apr 22 '20 at 11:30
  • There seems to be a CONFIG_PANIC_TIMEOUT available for you to configure the thing! – Pavel Šimerda Apr 22 '20 at 11:35
1

Linux kernel supports CONFIG_CMDLINE_BOOL

Allow for specifying boot arguments to the kernel at build time. On some systems (e.g. embedded ones), it is necessary or convenient to provide some or all of the kernel boot arguments with the kernel itself (that is, to not rely on the boot loader to provide them.)

There're some examples even on this "portal", for e. g.

https://superuser.com/questions/778826/config-cmdline-override-set-but-hardcoded-vga-boot-parameter-ignored

poige
  • 6,231
0

first conclude information in other answers. the value is defined in kernel/panic.c and rw through sysctl. it can be passed as a boot commanline.

what's more i'm about to say, boot commandline can be set default value during compilation.

0

You can use this patch, which adds a config option.

mgalgs
  • 167
-2

After seeing a lot of answers that don't really answer the question, I add my own:

You can press e when GRUB displays the boot menu to enter Edit Mode. There you see the usual kernel boot parameters, and you can remove, edit, or add parameters. When done, Press Cntrl+X to boot the edited entry.

I think that is what was being asked for.

For today's systems the line using linuxefi is probably the line you want to edit.

U. Windl
  • 1,411
  • I don't understand the down-voting; specifically as the question in contradicting itself: "But is there a config option to specify this (other than the default kernel command line option) before even boot loader comes into a play? Some kernel option may be?" The panic=N is a kernel parameter, so configuring it before any bootloader makes no sense as the bootloader just passes it along to the kernel. And actually configuring it before the bootloader would mean you'll have to configure it in firmware. Specifically the edit made be "poige" change the original semantics of the question. – U. Windl Dec 27 '23 at 07:07
  • I retract my downvote because, yes, the question is not as clear as I had thought. "Before the bootloader" is the issue. Most people just assumed the questioner meant "outside of the bootloader", as you can see from the upvoted answers. However, from the original poster's comments it seems that that was not what they meant. I don't know if your answer would have helped them, but I do not think so as it seems they want it to be automatic, as a failsafe. The question is unclear. – hackerb9 Feb 02 '24 at 17:06
  • Oof. Hovering over the downvote button it said "Click to undo". Unfortunately, that is not what it did. StackExchange now says my vote is "locked in" as a downvote unless the answer is edited. – hackerb9 Feb 02 '24 at 17:09