2

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

redcaddy
  • 225

1 Answers1

1

You can try simply,

Create file named myscript with following content:

#!/bin/bash
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

EDIT:

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

NAME
       telinit - change system runlevel

SYNOPSIS
       telinit [OPTION]...  RUNLEVEL

DESCRIPTION
       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
Pandya
  • 24,618