树莓派开机自动运行_树莓派设置开机启动脚本

0x00 背景需求

由于北邮校内网需要Drcom拨号,每次用curl命令很麻烦,所以开始了这次写开机启动脚本的过程。

0x01 linux启动过程

init 进程读取 /etc/inittab文件中的信息,并进入预设的运行级别。

# The default runlevel.

id:2:initdefault:

Debian中的运行级别

0 – 停机(千万不要把initdefault设置为0 )

1 – 单用户模式(单用户模式,只允许root用户对系统进行维护。)

2 – 多用户,但是没有NFS

3 – 完全多用户模式(字符界面)

4 – 基本不用

5 – X11(图形界面)

6 – 重新启动(千万不要把initdefault设置为6 )

查看系统当前运行级别使用runlevel命令,通常情况下 /etc/rcS.d/目录下的启动脚本首先被执行,然后是/etc/rcN.d/目录,N为/etc/inittab中的initdefault。

/etc/rc2.d中的部分文件如下:

pi@raspberrypi:/etc/rc2.d $ ll

total 12

drwxr-xr-x 2 root root 4096 Sep 25 17:56 .

drwxr-xr-x 111 root root 4096 Sep 25 18:26 ..

lrwxrwxrwx 1 root root 17 May 7 07:03 K01lightdm -> ../init.d/lightdm

lrwxrwxrwx 1 root root 20 May 7 06:38 K05nfs-common -> ../init.d/nfs-common

lrwxrwxrwx 1 root root 17 May 7 06:38 K05rpcbind -> ../init.d/rpcbind

-rw-r--r-- 1 root root 677 Jul 15 2013 README

lrwxrwxrwx 1 root root 18 May 7 06:16 S01bootlogs -> ../init.d/bootlogs

lrwxrwxrwx 1 root root 20 May 7 06:51 S01cgroup-bin -> ../init.d/cgroup-bin

lrwxrwxrwx 1 root root 16 May 7 07:16 S01dhcpcd -> ../init.d/dhcpcd

lrwxrwxrwx 1 root root 17 Sep 22 19:18 S01hostapd -> ../init.d/hostapd

lrwxrwxrwx 1 root root 17 May 7 06:38 S01ifplugd -> ../init.d/ifplugd

lrwxrwxrwx 1 root root 14 May 7 06:16 S01motd -> ../init.d/motd

lrwxrwxrwx 1 root root 17 May 7 06:38 S01rsyslog -> ../init.d/rsyslog

lrwxrwxrwx 1 root root 14 May 7 06:38 S01sudo -> ../init.d/sudo

lrwxrwxrwx 1 root root 22 May 7 06:38 S01triggerhappy -> ../init.d/triggerhappy

lrwxrwxrwx 1 root root 17 Sep 21 10:41 S02apache2 -> ../init.d/apache2

lrwxrwxrwx 1 root root 14 Sep 21 10:41 S03cron -> ../init.d/cron

lrwxrwxrwx 1 root root 14 Sep 21 10:41 S03dbus -> ../init.d/dbus

lrwxrwxrwx 1 root root 24 Sep 21 10:41 S03dphys-swapfile -> ../init.d/dphys-swapfile

lrwxrwxrwx 1 root root 15 Sep 25 17:56 S03drcom -> ../init.d/drcom

lrwxrwxrwx 1 root root 15 Sep 21 10:49 S03mysql -> ../init.d/mysql

lrwxrwxrwx 1 root root 13 Sep 21 10:41 S03ntp -> ../init.d/ntp

lrwxrwxrwx 1 root root 15 Sep 21 10:41 S03rsync -> ../init.d/rsync

lrwxrwxrwx 1 root root 15 Sep 21 14:42 S03snort -> ../init.d/snort

lrwxrwxrwx 1 root root 13 Sep 21 10:41 S03ssh -> ../init.d/ssh

lrwxrwxrwx 1 root root 16 Sep 22 19:21 S03udhcpd -> ../init.d/udhcpd

lrwxrwxrwx 1 root root 22 Sep 21 10:41 S04avahi-daemon -> ../init.d/avahi-daemon

lrwxrwxrwx 1 root root 18 Sep 21 10:41 S05plymouth -> ../init.d/plymouth

lrwxrwxrwx 1 root root 18 Sep 21 10:41 S05rc.local -> ../init.d/rc.local

lrwxrwxrwx 1 root root 19 Sep 21 10:41 S05rmnologin -> ../init.d/rmnologin

有K开头的文件,有S开头的文件,K的意思是Kill,S为Start。K和S后面紧跟着的数字就是关闭和启动的顺序,数字越大关闭或启动顺序越靠后。

将自定义脚本放在/etc/init.d目录下面,就可以使用service yourshellname start|stop当然需要脚本中写好start,stop功能。但是不能开机自启动。

0x03 update-rc.d函数

使用update-rc.d函数将脚本设置为开机自启动。

1)设置脚本启动和关闭依赖$remote_fs,$syslog,并且启动运行级别为2-5,关闭运行级别为0,1,6。

update-rc.d foobar defaults

上面一条命令就会在/etc/rc2.d rc3.d rc4.d rc5.d中创建软链接S03drcom -> ../init.d/drcom,在rc0.d rc1.d rc6.d中创建软链接K01drcom -> ../init.d/drcom。

2)设置脚本启动关闭都为顺序20,并且启动运行级别为2-5,关闭运行级别为0,1,6。

update-rc.d foobar start 20 2 3 4 5 . stop 20 0 1 6 .

同理。

上面两条经过试验,运行级别和顺序需要在shell脚本头部注释中指定。命令行中设置无效。

3)移除连接。

update-rc.d yourshellname remove

0x02 shell脚本

#!/bin/sh

### BEGIN INIT INFO

# Provides: drcom

# Required-Start: $local_fs $remote_fs $network $syslog

# Required-Stop: $local_fs $remote_fs $network $syslog

# Default-Start: 2 3 4 5

# Default-Stop: 0 1 6

# Short-Description: starts the dail drcom

# Description: starts drcom using start-stop-daemon

### END INIT INFO

case $1 in

start)

/usr/bin/curl -d "DDDDD=20131107**&upass=yourpassword&0MKKey=" "http://gw.bupt.edu.cn"

;;

stop)

/usr/bin/curl -b "myusername=20131107**&username=20131107**&smartdot=yourpassword" "http://gw.bupt.edu.cn/F.htm"

;;

*)

echo "Usage: $0 (start|stop)"

;;

esac

exit 0


版权声明:本文为weixin_39934613原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。