This is the 25th day of my participation in the August Genwen Challenge.More challenges in August

Learn about the ubuntu startup process to solve a small problem. I have studied the startup process of embedded Linux before, but distributions of Linux are not the same as embedded ones, so I have to do some more research and comb through it. This article will only cover the notes I have summarized.

The first thing I am confused about is the runlevel of the system. Ubuntu is a little different from the previous busybox system, so it is not obvious what the runlevel is. At the beginning of the script modification, I did not pay attention to confirm this problem first, and it took some time. To view runlevel, simply type runlevel, for example:

# runlevel
N 2
Copy the code

To find out where the level is set, use the grep command as follows:

# grep -iR runlevel /etc/
/etc/init/rc-sysinit.conf:# Default runlevel, this may be overriden on the kernel command-line
/etc/init/rc-sysinit.conf:env DEFAULT_RUNLEVEL=2
Copy the code

/etc/init. rc-sysinit.conf file:

# Default runlevel, this may be overriden on the kernel command-line env  
DEFAULT_RUNLEVEL=3  
Copy the code

The comment says you can use the startup parameter to change it, but I think it’s better to write a default value like this. I changed it to level 3. On startup, the scripts in the /etc/rc3.d directory are run. Here’s how to add a custom script. Use the existing script as the template to perform the desired operation. Let’s say I add an XScript script. As follows:

# cat /etc/init.d/xscript
#! /bin/sh
### BEGIN INIT INFO
# Provides:         start or reboot,etc...
# Required-Start:
# Required-Stop:
# Default-Start: 2 3
# Default-Stop: 0 1 6
# Short-Description:
# Description:
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /lib/lsb/init-functions

# other things to do before dying...
do_stop () {
        echo "Stop in my script and clean net.rules...."
        echo "" > /etc/udev/rules.d/70-persistent-net.rules
}

case "$1" in
  start)
        echo "Will START Running...."
        cd /home/latelee
        chmod +x my_run.sh
        ./my_run.sh
        ;;
  restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
  stop)
        do_stop
        ;;
  *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac
Copy the code

This script defines the operations to be performed during startup and restart. At first, I linked files to Xscript directly in the runlevel directory I wanted to run, but later found that I could do without. The following is added to each runlevel with the update-rc.d command, where 99 specifies the order of execution and the /etc/rx.d directory scripts start with S and K scripts, indicating execution on startup and shutdown (reboot) respectively. Scripts with small numbers are started first, and since this script is user-level, it is placed last. Result:

# update-rc.d xscript defaults 99 update-rc.d: warning: default start runlevel arguments (2 3 4 5) do not match xscript Default-Start values (2 3) Adding system startup for /etc/init.d/xscript ... /etc/rc0.d/K99xscript -.. /init.d/xscript /etc/rc1.d/K99xscript -.. /init.d/xscript /etc/rc6.d/K99xscript -.. /init.d/xscript /etc/rc2.d/S99xscript -.. /init.d/xscript /etc/rc3.d/S99xscript -.. /init.d/xscript /etc/rc4.d/S99xscript -.. /init.d/xscript /etc/rc5.d/S99xscript -.. /init.d/xscriptCopy the code

Restart the system:

root@localhost:~# reboot

Broadcast message from root@localhost
        (/dev/ttyS2) at 17:09 ...

The system is going down for reboot NOW!
root@localhost:~# wait-for-state stop/waiting
*
Stop in my script and clean net.rules....
* Asking all remaining processes to terminate...                        [ OK ]
* All processes ended within 1 seconds...                               [ OK ]
Copy the code

You can see Stop in my script and clean net.rules…. Print, after reboot, has executed the command we wrote ourselves.