神的尾巴

全栈工程师、独立开发者

0%

聊聊Linux-Service

之前使用LNMP安装后,添加了lnmp的Service,研究下为什么要使用Service,和Service是怎么工作的。

运行级别-RunLevel

  • 运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动;
  • 运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆;
  • 运行级别2:多用户状态(没有NFS);
  • 运行级别3:完全的多用户状态(有NFS),登陆后进入控制台命令行模式;
  • 运行级别4:系统未使用,保留;
  • 运行级别5:X11控制台,登陆后进入图形GUI模式;
  • 运行级别6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动。

runlevel可以查看当前runlevel,init n可以进入指定runlevel。

不同运行级别下的Service

在/etc/rc.d目录下,有名为rc0~6.d的目录,里面对应着到脚本文件的软链接。

第一个字符为S或K,S为开启(传入start参数),K为结束(传入stop参数),中间两位数字是执行顺序,最后是服务的名称。

例如,这是我rc5.d下的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
lrwxrwxrwx   1 root root    16 8月  15 05:09 S01apport -> ../init.d/apport*
lrwxrwxrwx 1 root root 15 11月 19 14:19 S01mysql -> ../init.d/mysql*
lrwxrwxrwx 1 root root 20 8月 14 22:58 S01php7.0-fpm -> ../init.d/php7.0-fpm*
lrwxrwxrwx 1 root root 17 11月 19 15:40 S01php-fpm -> ../init.d/php-fpm*
lrwxrwxrwx 1 root root 17 8月 15 05:09 S01rsyslog -> ../init.d/rsyslog*
lrwxrwxrwx 1 root root 20 8月 14 21:29 S01ubuntu-fan -> ../init.d/ubuntu-fan*
lrwxrwxrwx 1 root root 15 8月 15 05:09 S01uuidd -> ../init.d/uuidd*
lrwxrwxrwx 1 root root 20 8月 14 23:13 S01virtualbox -> ../init.d/virtualbox*
lrwxrwxrwx 1 root root 15 8月 15 05:09 S02acpid -> ../init.d/acpid*
lrwxrwxrwx 1 root root 17 8月 15 05:09 S02anacron -> ../init.d/anacron*
lrwxrwxrwx 1 root root 24 8月 14 21:29 S02cgroupfs-mount -> ../init.d/cgroupfs-mount*
lrwxrwxrwx 1 root root 14 8月 15 05:09 S02cron -> ../init.d/cron*
lrwxrwxrwx 1 root root 14 8月 15 05:09 S02dbus -> ../init.d/dbus*
lrwxrwxrwx 1 root root 20 8月 15 05:09 S02irqbalance -> ../init.d/irqbalance*
lrwxrwxrwx 1 root root 20 8月 15 05:09 S02kerneloops -> ../init.d/kerneloops*
...

例子:Docker的service script,提供start,stop,restart,status等方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/sh
set -e

### BEGIN INIT INFO
# Provides: docker
# Required-Start: $syslog $remote_fs
# Required-Stop: $syslog $remote_fs
# Should-Start: cgroupfs-mount cgroup-lite
# Should-Stop: cgroupfs-mount cgroup-lite
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Create lightweight, portable, self-sufficient containers.
# Description:
# Docker is an open-source project to easily create lightweight, portable,
# self-sufficient containers from any application. The same container that a
# developer builds and tests on a laptop can run at scale, in production, on
# VMs, bare metal, OpenStack clusters, public clouds and more.
### END INIT INFO

export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

BASE=docker

...

case "$1" in
start)
check_init

fail_unless_root

devicemapper_umount

touch "$DOCKER_LOGFILE"
chgrp docker "$DOCKER_LOGFILE"

ulimit -n 1048576
if [ "$BASH" ]; then
ulimit -u 1048576
else
ulimit -p 1048576
fi

log_begin_msg "Starting $DOCKER_DESC: $BASE"
start-stop-daemon --start --background \
--no-close \
--exec "$DOCKER" \
--pidfile "$DOCKER_SSD_PIDFILE" \
--make-pidfile \
-- \
daemon -p "$DOCKER_PIDFILE" \
$DOCKER_OPTS \
>> "$DOCKER_LOGFILE" 2>&1
log_end_msg $?
;;

stop)
check_init
fail_unless_root
log_begin_msg "Stopping $DOCKER_DESC: $BASE"
start-stop-daemon --stop --pidfile "$DOCKER_SSD_PIDFILE" --retry 10
log_end_msg $?
;;

restart)
check_init
fail_unless_root
docker_pid=`cat "$DOCKER_SSD_PIDFILE" 2>/dev/null`
[ -n "$docker_pid" ] \
&& ps -p $docker_pid > /dev/null 2>&1 \
&& $0 stop
$0 start
;;

force-reload)
check_init
fail_unless_root
$0 restart
;;

status)
check_init
status_of_proc -p "$DOCKER_SSD_PIDFILE" "$DOCKER" "$DOCKER_DESC"
;;

*)
echo "Usage: service docker {start|stop|restart|status}"
exit 1
;;
esac
觉得对你有帮助的话,请我喝杯咖啡吧~.