Skip to content
systemcoding.dev
Embedded Linux

Respawn a process forever with ::respawn: in inittab

Add a ::respawn: line to BusyBox /etc/inittab so init restarts a daemon every time it exits, with no extra supervisor installed.

1 min read
Summarize in AI

When a daemon must always be running and the image has no systemd, BusyBox init can supervise it. An inittab line has the form id:runlevels:action:process, and BusyBox ignores the runlevels field, which is why the usual lines begin with ::. The respawn action starts the process and starts it again whenever it exits.

Text
::respawn:/usr/sbin/sensord --foreground

The process must stay in the foreground. A daemon that forks and exits looks to init like a process that died, so init starts another copy, and then another. Pass the program's no-daemonize flag, or wrap it so the wrapper itself stays alive.

Shell
ps | grep sensord
kill $(pidof sensord); sleep 1; pidof sensord
Text
  212 root     /usr/sbin/sensord --foreground
  247

The second command prints a new pid, since init replaced the process we killed (the numbers will differ). The BusyBox init documentation lists the actions in the source header comment. BusyBox does not document any back-off, so a program that exits immediately on a bad config will be restarted in a tight loop; put a short sleep in a wrapper script if that is a risk. Init rereads inittab on kill -HUP 1.

Share

More snippets

Systems Programming

Get the page size with sysconf(_SC_PAGESIZE)

Read the memory page size at run time with sysconf(_SC_PAGESIZE) instead of hard-coding 4096, which is wrong on arm64 kernels built with 16K or 64K pages.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.