Can root kill init process (the process with pid 1)? What would be its consequences?
By default, no, that's not allowed. Under Linux (from
man 2 kill):
The only signals that can be sent to process ID 1, the init process, are those for which init has explicitly installed signal handlers. This is done to assure the system is not brought down accidentally.
Pid 1 (init) can decide to allow itself to be killed, in which case the "kill" is basically a request for it to shut itself down. This is one possible way to implement the
halt command, though I'm not aware of any
init that does that.
On a Mac, killing
launchd (its init analogue) with signal 15 (SIGTERM) will immediately reboot the system, without bothering to shut down running programs cleanly. Killing it with the uncatchable signal 9 (SIGKILL) does nothing, showing that Mac's
kill() semantics are the same as Linux's in this respect.
At the moment, I don't have a Linux box handy that I'm willing to experiment with, so the question of what Linux's And with
init does with a SIGTERM will have to wait.
init replacement projects like Upstart and Systemd being popular these days, the answer could be variable.
UPDATE: On Linux,
init explicitly ignores SIGTERM, so it does nothing. @jsbillings has information on what Upstart and Systemd do.
The SysV init ignores SIGKILL or SIGTERM signals. The only signal that causes a change in state is SIGPWR as far as I can tell, which schedules a power-related shutdown.
It appears that Upstart and Systemd also do not respond to SIGKILL, and from my test, it appears that a SIGTERM causes upstart and systemd to re-exec.
I'm not sure what the other answerers are running but I'm pretty sure you can't kill -9 (SIGKILL) or kill -15 (SIGTERM) init (pid 1). Most likely, if you were able to, you'd get a kernel panic because init unexpectedly exited with a non-zero exit code, which would be less than ideal. It doesn't shut down your computer, or cause it to reboot.
Technically yes, root can issue a SIGKILL to init. init, however, differs from most, nearly all in fact, other processes in that it is permitted to trap and ignore the signal.
You can, loosely, kill init by issuing a
kill -TERM 1 which would be analogous to issuing a
shutdown in that init will pass the signal to all children, essentially all other processes, before honoring the signal itself.
Please note: performing this command will shutdown your system.
For flavor; one type of other process that can "ignore" a SIGKILL is one in uninterruptible sleep, such as one waiting for i/o. Such a process could be found by issuing a
ps axo stat,comm where processes with a status 'D' are uninterruptible.
You can restart the
init process. This is useful for making changes to
inittab without having to reboot.
kill -HUP 1
Well, root can kill init process on Linux:
strace -p 1 -o OUT & kill -9 1
kill -9 1 doesn't work:
-bash-4.3# trace-cmd start -e signal_deliver -f 'common_pid == 1' -e signal_generate -f 'pid == 1' -bash-4.3# echo "My first attempt" >/sys/kernel/debug/tracing/trace_marker -bash-4.3# kill -9 1 -bash-4.3# trace-cmd show # there is no signal_deliver-event ... bash-164  .N.. 29.302996: tracing_mark_write: My first attempt bash-164  d... 29.312586: signal_generate: sig=9 errno=0 code=0 comm=systemd pid=1 grp=1 res=1
So, let's run
-bash-4.3# echo 1 >/proc/sys/kernel/ftrace_dump_on_oops -bash-4.3# strace -p 1 -o OUT &  179 strace: Process 1 attached -bash-4.3# echo "My second attempt" >/sys/kernel/debug/tracing/trace_marker -bash-4.3# kill -9 1 bash-4.3# [ 134.943439] Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000009 [ 134.943439] [ 134.943439] CPU: 0 PID: 1 Comm: systemd Not tainted 4.7.2-201.fc24.x86_64 #1 [ 134.943439] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.9.1-1.fc24 04/01/2014 [ 134.943439] 0000000000000086 00000000610ec632 ffff88001ea43c10 ffffffff813d941f [ 134.943439] ffffffff81a290c0 ffff88001ea43ca8 ffff88001ea43c98 ffffffff811b2cb6 [ 134.943439] ffffffff00000010 ffff88001ea43ca8 ffff88001ea43c40 00000000610ec632 [ 134.943439] Call Trace: [ 134.943439] [<ffffffff813d941f>] dump_stack+0x63/0x84 [ 134.943439] [<ffffffff811b2cb6>] panic+0xde/0x22a [ 134.943439] [<ffffffff810a40ac>] do_exit+0xb6c/0xb70 [ 134.943439] [<ffffffff810a4137>] do_group_exit+0x47/0xb0 [ 134.943439] [<ffffffff810af3ed>] get_signal+0x28d/0x630 [ 134.943439] [<ffffffff81025f57>] do_signal+0x37/0x6c0 [ 134.943439] [<ffffffff8100325b>] ? do_audit_syscall_entry+0x4b/0x70 [ 134.943439] [<ffffffff810ca250>] ? wake_up_q+0x70/0x70 [ 134.943439] [<ffffffff8100330c>] exit_to_usermode_loop+0x8c/0xd0 [ 134.943439] [<ffffffff81003df3>] do_syscall_64+0x103/0x110 [ 134.943439] [<ffffffff817eb921>] entry_SYSCALL64_slow_path+0x25/0x25 [ 134.943439] Dumping ftrace buffer: [ 134.943439] --------------------------------- [ 134.943439] bash-154 0.... 10592888us : tracing_mark_write: My first attempt [ 134.943439] bash-154 0d... 17328079us : signal_generate: sig=9 errno=0 code=0 comm=systemd pid=1 grp=1 res=1 [ 134.943439] bash-154 0.... 80772500us : tracing_mark_write: My second attempt [ 134.943439] bash-154 0dN.. 85426791us : signal_generate: sig=9 errno=0 code=0 comm=systemd pid=1 grp=1 res=0 [ 134.943439] systemd-1 0d... 85437478us : signal_deliver: sig=9 errno=0 code=0 sa_handler=0 sa_flags=0 [ 134.943439] --------------------------------- [ 134.943439] Kernel Offset: disabled [ 134.943439] ---[ end Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000009 [ 134.943439]