Linux start init в script

All about Linux init script

What is an init script, what is the use of it, how to write an init script? Lets begin to know the answers.

Declaimer: Use it at your own risk. Writing this topic for learning and knowledge sharing purpose only. You must own root privileges (and authority) on the system where you are trying or applying this topic.

What is an init script?

Keeping it simple, init script is just a shell script which always starts with shebang. It is used to start an application on boot time and stop on shutdown. It can also be used to restart and to send various other instructions to the application.

So how to write this init script?

Writing sample init script

So its a shell script, a simple case esac shell script. Lets take following example:

#!/bin/sh # # Sample init script version 1 # Created by Rahul Panwar # # description: Describe the purpose of this shell script like every good documented shell script. # chkconfig: 2345 80 20 # start function will be executed on startup start() < echo start # start application command ># stop function will be executed on shutdown stop() < echo stop # stop application command >case "$1" in start) start ;; stop) stop ;; *) echo "invalid argument" exit 1 esac

This is an example init shell script, which is using start, stop as argument. Executing this script with start argument will call start function and stop will call stop function, other than those arguments will give error “invalid argument” and exit with error status 1.

Explaining and enhancing sample init script

Now you have a sample script which is starting at boot time. So its time to understand it part by part and enhance it for some real use.

Start a init script like a simple shell script

Like every shell script, it is also starting with shebang #!/bin/sh and some comments about the script.

#!/bin/sh # # Sample init script version 1 # Created by Rahul Panwar

Comment but no comment, chkconfig in comment, to set start and stop in init script

In comment line of init script, chkconfig means when to start the service and when to stop and in which runlevel.

# description: Describe the purpose of this shell script like every good documented shell script. # chkconfig: 2345 80 20 

2345 after chkconfig: is showing the runlevels 2, 3, 4 and 5 where this init script will be started and other than that, like runlevels 0, 1 and 6, where it will be stopped.

Читайте также:  Mts in mp4 linux

After runlevel, 80 denotes the start sequence and 20 denotes the stop sequence.

So Don’t consider chkconfig as a comment in init script, set the start sequence and stop sequence as per the requirement.

NOTE: If same start and stop sequence is using in other scripts, init will sort them, to execute, by using there names.

Functions creation is helpful in init script

Functions in init script or in any shell script, makes it easy to read and understand. It helps to avoid code repetition.

For example, restart function can be added, which will use stop and start functions to restart the application.

# restart function will be executed for restarting the application restart()

Other important functions can also be added to script by using import method.

# start function will be executed on startup . /etc/init.d/functions

/etc/init.d/functions script contains several useful functions for init scripts like daemon (used to daemonize the application), killproc (To kill the running process id), status (to get the current status of running application or process id), etc.

So lets enhance our start and stop functions, by using some useful functions from /etc/init.d/functions script.

myname=`basename $0` helloapp=/usr/local/bin/sayhello RETVAL=0 # start function will be executed on startup start() < echo -n "Starting $myname: " # start application command daemon $helloapp RETVAL=$? [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$myname echo return $RETVAL ># stop function will be executed on shutdown stop() < echo -n "Stopping $myname: " # stop application command killproc $helloapp RETVAL=$? [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$myname echo return $RETVAL >

NOTE: Lock file creation in start and remove in stop is very important to write a perfect init script, to know more about it check my old post Use of Subsystem Lock Files in Init Scripts.

case esac to accept the init script arguments

Last but very essential part of init script is case esac. It also make init scripts easy to understand the allowed arguments and add more arguments.

case "$1" in start) start ;; stop) stop ;; restart) restart ;; status) status $helloapp ;; *) echo "Usage: 

So very easily two new cases, restart and status, are added to init script, which will allow start, stop, restart and status as init script arguments. Other than these arguments, it will goes to default section (*), where it will just show the usage to init script and exit.

Читайте также:  Linux сбросить настройки панели

Finalize and Execute the init script

Sample init script is explained and enhanced step by step, so lets collaborate all and prepare the final script.

#!/bin/sh # # Sample init script version 2 # Created by Rahul Panwar # # description: Run helloapp application as Linux service. # chkconfig: 2345 80 20 # start function will be executed on startup . /etc/init.d/functions myname=`basename $0` helloapp=/usr/local/bin/sayhello RETVAL=0 # start function will be executed on startup start() < echo -n "Starting $myname: " # start application command daemon $helloapp RETVAL=$? [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$myname echo return $RETVAL ># stop function will be executed on shutdown stop() < echo -n "Stopping $myname: " # stop application command killproc $helloapp RETVAL=$? [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$myname echo return $RETVAL ># restart function will be executed for restarting the application restart() < stop start >case "$1" in start) start ;; stop) stop ;; restart) restart ;; status) status $helloapp ;; *) echo "Usage: $myname " exit 1 ;; esac exit $?

Adding sample init script to start at boot

This init script can be added to start at boot time, by using following steps:

  • Save this init script to /etc/init.d/ directory with a name let say sample-init, where all Linux init scripts stores.
  • Don’t forget to make it executable, by using Linux chmod command.
[root@rahul-pc ~]# chmod +x /etc/init.d/sample-init
[root@rahul-pc ~]# chkconfig --add sample-init
  • /etc/rc0.d/K20sample-init
  • /etc/rc1.d/K20sample-init
  • /etc/rc2.d/S80sample-init
  • /etc/rc3.d/S80sample-init
  • /etc/rc4.d/S80sample-init
  • /etc/rc5.d/S80sample-init
  • /etc/rc6.d/K20sample-init

These softlinks will be executed during boot at system runlevel. Softlink starts with S80 will execute with start argument (with 80 priority) and softlink starts with K20 will execute with stop argument (with 20 priority) on respective runlevel.

    Verify the added init script.

[root@rahul-pc ~]# chkconfig --list sample-init sample-init 0:off 1:off 2:on 3:on 4:on 5:on 6:off

After 4th step, it start showing 2:on, 3:on, 4:on, 5:on, means it is ready to start on boot at runlevel 2, 3, 4 and 5. All the priority and runlevel setting is the effect of chkconfig in comment section, described in above sectiosn.

You can now reboot your system and check its running.

Deleting sample init script boot from system boot

Following chkconfig command will be used to delete the sample-init script from system boot.

[root@rahul-pc ~]# chkconfig --del sample-init

All the softlinks created in add will be removed, that will stop it to execute on boot.

Use of service command with init script

service command can be used to pass the argument to init script, as follows:

[root@rahul-pc ~]# service sample-init start Starting sample-init: [ OK ]
[root@rahul-pc ~]# service sample-init status Subsysten sample-init is active with pid 1234
[root@rahul-pc ~]# service sample-init restart Stopping sample-init: [ OK ] Starting sample-init: [ OK ]
[root@rahul-pc ~]# service sample-init stop Stopping sample-init: [ OK ]

Using any other argument, it will show usage:

[root@rahul-pc ~]# service sample-init help Usage: sample-init

Источник

Can the init process be a shell script in Linux?

The only thing that is missing is /init, the executable in the root of the initramfs that is executed by the kernel once it is loaded. Because sys-apps/busybox includes a fully functional shell, this means you can write your /init binary as a simple shell script (instead of making it a complicated application written in Assembler or C that you have to compile).

and gives an example of init as a shell script that starts with #!/bin/busybox sh So far, I was under the impression that init is the main process that is launched and that all the other user space process are eventually children of init. However, in the given example, the first process is actually bin/busybox/ sh from which later init is spawned. Is this a correct interpertation? If I were, for example, have a available interpreter available at that point, I could write init as a Python script etc.?

2 Answers 2

init is not "spawned" (as a child process), but rather exec 'd like this:

# Boot the real thing. exec switch_root /mnt/root /sbin/init 

exec replaces the entire process in place. The final init is still the first process (pid 1), even though it was preceded with those in the Initramfs.

The Initramfs /init , which is a Busybox shell script with pid 1, exec s to Busybox switch_root (so now switch_root is pid 1); this program changes your mount points so /mnt/root will be the new / .

switch_root then again exec s to /sbin/init of your real root filesystem; thereby it makes your real init system the first process with pid 1, which in turn may spawn any number of child processes.

Certainly it could just as well be done with a Python script, if you somehow managed to bake Python into your Initramfs. Although if you don't plan to include busybox anyway, you would have to painstakingly reimplement some of its functionality (like switch_root , and everything else you would usually do with a simple command).

However, it does not work on kernels that do not allow script binaries ( CONFIG_BINFMT_SCRIPT=y ), or rather in such a case you'd have to start the interpreter directly and make it load your script somehow.

Источник

Оцените статью
Adblock
detector