Is it possible to set the current Linux runlevel programmatically?

286 Views Asked by At

Without shelling out or calling exec, is there a way to request a runlevel change programmatically?

1

There are 1 best solutions below

1
user6556709 On

Sys-V init opens a fifo at /run/initctl . The command you can send is "described" in initreq.h (most probably not installed on your system but can be found in the sources of init).

So all you have to do is to open /run/initctl and write the request into it.

struct init_request request= {.magic = INIT_MAGIC, 
                              .cmd = INIT_CMD_START, 
                              .runlevel= [your_run_level], 
                              .sleeptime = 5  //should sleep 5s between term and kill
                             };