运行级别
许多程序需要开机启动。它们在Windows叫做"服务"(service),在Linux就叫做"守护进程"(daemon)。
init进程的一大任务,就是去运行这些开机启动的程序。
但是,不同的场合需要启动不同的程序,比如用作服务器时,需要启动Apache,用作桌面就不需要。
Linux允许为不同的场合,分配不同的开机启动程序,这就叫做"运行级别"(runlevel)。也就是说,启动时根据"运行级别",确定要运行哪些程序。
Linux系统有7个运行级别(runlevel):
- 运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动
- 运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆
- 运行级别2:多用户状态(没有NFS)
- 运行级别3:完全的多用户状态(有NFS),登陆后进入控制台命令行模式
- 运行级别4:系统未使用,保留
- 运行级别5:X11控制台,登陆后进入图形GUI模式
- 运行级别6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动
比如说,我写了一个helloworld.c程序,编译后的可执行程序helloworld如何开机自启
1、首先将应用程序放到/usr/lib/目录下 注意修改访问权限
2、在/etc/init.d中写执行脚本
#! /bin/sh
# init.d script for daemon
set -e
case "$1" in
start)
echo -n "Starting helloworld daemon: "
start-stop-daemon -S -b -a /usr/bin/helloworld
echo "done"
;;
stop)
echo -n "Stopping helloworld daemon: "
start-stop-daemon -K -n helloworld
echo "done"
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: helloworld daemon { start | stop | restart }" >&2
exit 1
;;
esac
exit 0
3、首先通过 cat /etc/inittab 查看系统启动的运行级别。
如果启动级别为5 则软连接在rc5.d里面
cd /etc/rc.d/rc5.d
ln -s ../init.d/helloworld S56helloworld
S5Pv210实战:
1、进入:vi /etc/init.d/rcS文件中,输入下面脚本:
脚本含义:开启/etc/init.d/ S开头的脚本
#!/bin/sh
# Start all init scripts in /etc/init.d
# executing them in numerical order.
#
for i in /etc/init.d/S??* ;do
# Ignore dangling symlinks (if any).
[ ! -f "$i" ] && continue
case "$i" in
*.sh)
# Source shell script for speed.
(
trap - INT QUIT TSTP
set start
. $i
)
;;
*)
# No sh extension, so fork subprocess.
$i start
;;
esac
done
2、在/etc/init.d目录下新建一个S开头的文件,在里面输入下面的脚本
使用方法:【1】、export添加自己应用程序所需的库文件所在目录
【2】、在下面标红处写上需要启动的应用程序
#!/bin/sh
#
# Start the qttool....
#
#Source configuration files from /etc/profile.d
export LD_PRELOAD=/lib/preloadable_libiconv.so
##tslib库环境变量配置#############################################################################
export TSLIB_ROOT=/usr/local/tslib
export TSLIB_TSDEVICE=/dev/input/event1
export TSLIB_CALIBFILE=/etc/pointercal
export TSLIB_CONFFILE=$TSLIB_ROOT/etc/ts.conf
export TSLIB_PLUGINDIR=$TSLIB_ROOT/lib/ts
export TSLIB_FBDEVICE=/dev/fb0
export QTDIR=/usr/local/QT5.6.2
export QT_QPA_FONTDIR=$QTDIR/lib/fonts
export QT_QPA_PLATFORM_PLUGIN_PATH=$QTDIR/plugins/
export QT_QPA_PLATFORM=linuxfb:fb=/dev/fb0:size=1024x600:mmSize=1024x600:offset=0x0:tty=/dev/tty1
export LD_LIBRARY_PATH=$TSLIB_ROOT/lib:/lib:/usr/lib:$QTDIR/lib:$QTEDIR/lib:$LD_LIBRAR
case "$1" in
start)
echo "Starting qttool..."
/root/uidemo18 -qws &
;;
stop)
echo -n "Stopping qttool..."
;;
restart|reload)
"$0" stop
"$0" start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit $?
开机成功开启程序