I want to make a simple script so that the user can easy change the runlevels. The script should prompt the user to choose the new runlevel, run the /sbin/init program and receive a confirmation message.

Any ideas ?

P.S if the runlevel is changed i want to receive a confirmation message. Help please


You can try simply,

Create file named myscript with following content:

echo -n "Enter Run level":
read rl;
sudo init $rl;

Where echo is used to print what to input, read is used to get the input and store into variable rl and sudo init $rl; to execute command for changing run level.

For more control/verification over input, you can use loop such as while and case.

You've to give execution permission:sudo chmod +x myscript so that it can be executed.

Then you can run script like:

$ ./myscript
$ Enter Run level : 3


as @RuiFRibeiro suggested, the command to change runlevel is telinit:

       telinit - change system runlevel

       telinit [OPTION]...  RUNLEVEL

       telinit may be used to change the system runlevel.

So, You should use telinit instead init in script.

And to check run level you can use who -r or runlevel command which output previous and current runlevel.

$ who -r | awk '{print $1,$2}'
run-level 3
$ runlevel
2 3
  • @RuiFRibeiro I've also pointed that in answer. thanks – Pandya Dec 21 '15 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.