init

最後更新: 2024-03-11

介紹

init 是系統第一個執行的 Process(pid 1)

ps aux

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0   2176   640 ?        Ss   Mar10   0:00 init [3]
...

它的 config file 是 /etc/inittab

目錄


SysV vs Upstart vs Systemd

 

Upstart init config files: /etc/init/*

SysV init files are still in /etc/init.d/*

OS

  • Fedora 15+: systemctl
  • Ubuntu 12.04+, 14.04: initct

 

 


telinit

 

/sbin/telinit is linked to /sbin/init.  

It takes a one-charactern argument and signals init to perform the appropriate action

q

tell init to re-examine the /etc/inittab file

/var/log/messages

... init: Re-reading inittab

0,1,2,3,4,5 or 6

tell init to switch to the specified run level

s

tell init to switch to single user mode.

 


Ubuntu 14.04 Example

 

Install

apt get install sysv-rc-conf

  • sysv-rc-conf --list : List services
  • sysv-rc-conf service-name on : Start service
  • sysv-rc-conf service-name off : Stop service

Init Script

#!/bin/sh
#
# Startup script for program
#
# chkconfig: 345 85 15     - start or stop process definition within the boot process
# description: Description of program
# processname: process-name
# pidfile: /var/run/process-name.pid

# Source function library.      This creates the operating environment for the process to be started
. /etc/rc.d/init.d/functions

case "$1" in
  start)
        echo -n "Starting  process-name: "
        daemon  process-name                 - Starts only one process of a given name.
        echo
        touch /var/lock/subsys/process-name
        ;;
  stop)
        echo -n "Shutting down process-name: "
        killproc process-name
        echo
        rm -f /var/lock/subsys/process-name
        rm -f /var/run/process-name.pid      - Only if process generates this file
        ;;
  status)
        status process-name
        ;;
  restart)
        $0 stop
        $0 start
        ;;
  reload)
        echo -n "Reloading process-name: "
        killproc process-name -HUP
        echo
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac

exit 0

Doc

  • man upstart-events

 


 

 

Creative Commons license icon Creative Commons license icon