EDA365电子论坛网

标题: linux 将自己的服务添加到系统service服务 [打印本页]

作者: mytomorrow    时间: 2021-3-16 10:02
标题: linux 将自己的服务添加到系统service服务

  l& l" \4 l( f  f8 ]+ [前言
- H) h4 M; N& x8 O# f! L8 a我们在linux上要启动一个程序得时候, 往往都是要写一堆路径, 找到要启动得服务程序, 再用 ./*** 启动服务. 那么我们有没有快速启动方法吗, 答案是肯定得! p) w- E1 s3 j+ B) Z$ R

" J; V; Y3 {9 Y2 Z3 s" yservice 介绍6 S4 L, j  \2 P! }0 c6 P& Y
官方介绍(英文): https://linux.die.net/man/8/service$ e2 b' J; T" S; ?  s; G$ q
( j0 s" c# j. `! o! a
简单说一下service运行过程. 以iptables为例: service iptables start
9 \8 ~0 Z) R" E8 N8 o( R% h: g( h! V; d3 w  Y( Z+ h
首先,sevice 会去/etc/init.d下寻找iptables脚本, start是iptables脚本里的一个参数(你可以去查看networking这个脚本支持的参数)
7 W5 S7 V6 O$ Z# {* ?, _. c2 `$ t然后告诉系统运行iptables这个脚本,剩下的事情就交给iptables脚本去坐了,事实就是这么简单。% ~8 J& M2 P5 ]3 r! r, Q
至此,你们应该知道如何添加一个service命令了吧' F5 A5 n: l; I% p5 c6 b
7 I  h0 G8 n4 @+ Z) q) X
编写一个脚本,然后把它放在/etc/init.d这个目录下,再用service + 脚本名字 运行即可。如果是要开机自动启动那就得用chkconfig命令了
; c! R& p" g2 y0 S& B1 `" @3 i4 n9 Q) n" {3 V7 l' \0 e
注意:
5 b' l4 _% g5 Y: B( U$ U6 y8 p4 p, jA、service这个命令往往是即时生效,不用开关机,但是重启后服务会回到默认状态。
  |5 K4 i4 d% a3 z- P5 C' p1 t- N: s9 _6 m3 v2 P& T
B、在init.d里面得脚本是没有后缀名的9 K, d5 ^( c. E8 @4 J6 j5 j6 ?
. y! M' d3 W- b1 ~

! ~  v" N: a2 W) x
9 a. I! Z2 ~4 K$ _2 W: Y& X设置开机自动启动' S7 _( z5 a# G6 I- }
chkconfig --add test
2 s4 \& S0 i* C7 achkconfig test on/off    //重启后永久生效/ y1 O- x' J/ \( w5 B1 H
上面的不生效:则使用下面得方法) j$ L$ _5 l5 N0 o

, @; n+ R( S5 U+ }% H+ Y通过update-rc.d 命名设置开机自启动( W  O7 h4 K( ]: ~1 D
9 a- f* i! r% Z! e+ }: X5 V9 u
cd /etc/init.d
2 j  `* p) ~6 U. }1 W( l# Bsudo update-rc.d test defaults 95) k2 V( q" ?" |. |! K% m
注:其中数字95是脚本启动的顺序号,按照自己的需要相应修改即可。在你有多个启动脚本,而它们之间又有先后启动的依赖关系时你就知道这个数字的具体作用了。该命令的输出信息参考如下:
8 g1 W9 Q* f$ z1 v
6 v3 }$ c0 A$ J4 q' v8 eupdate-rc.d: warning: /etc/init.d/test missing LSB information3 b3 _$ i4 K) W  `8 q2 c; q' w8 U
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
3 Z6 {( U7 y9 U) C: s: V' n- t' eAdding system startup for /etc/init.d/test ...: l4 q& G( Z# s, |$ B. s" R4 u4 o
/etc/rc0.d/K95test -> ../init.d/test
6 f' J# r( d3 V% ]/etc/rc1.d/K95test -> ../init.d/test
5 ^' U6 U- n& _* [' @/etc/rc6.d/K95test -> ../init.d/test+ v" i5 Q: p+ ^9 ?) ^- W2 h2 [
/etc/rc2.d/S95test -> ../init.d/test
0 A4 _( Y7 X  g# m. G# \/etc/rc3.d/S95test -> ../init.d/test, J  _6 H- h+ M8 b9 |- y* `
/etc/rc4.d/S95test -> ../init.d/test8 S8 O/ t* ?  g, s5 G+ m- _; l; @
/etc/rc5.d/S95test -> ../init.d/test
* G9 w0 [6 J& @$ ^: H: l. X2 E卸载启动脚本的方法:( W: ^$ S& Q5 r3 u4 l4 D
2 F! y( |8 i" n/ q  j2 g
cd /etc/init.d3 J$ d) |: c& e$ I
sudo update-rc.d -f test remove
' {. u# x8 a7 y& r命令输出的信息参考如下:
3 X' {: W4 h- y  ^4 Y: ^. d; ]$ {4 y$ J2 j; d
Removing any system startup links for /etc/init.d/test ...
8 h# E& l3 }. p9 [/ S* D! ?- W( O1 O/etc/rc0.d/K95test& a9 ?0 `# ^0 A% }& Q; Q
/etc/rc1.d/K95test
1 m0 {- l+ }. O. z/etc/rc2.d/S95test4 ~. L  E7 Z$ @, N; P0 n
/etc/rc3.d/S95test
3 I2 g( z- @" U( f/etc/rc4.d/S95test
  ]. ~$ N3 ]$ R# \; }8 q/etc/rc5.d/S95test
- r! j1 e8 |# W  J2 L/etc/rc6.d/K95test( R' P( v( |- ^

3 K7 \# w- f" |( i, E  l  J
6 d& D% z0 i7 I/ K常见错误:5 W, N1 V/ m* v9 z0 f
1. service启动任务时提示: program-service: unrecognized service) ]6 o7 H) |1 V  z5 |* r1 d
' ]" R0 r1 ?! T! J# Y( ~# E
  这是因为我们还没有更改 执行脚本得 的权限为可执行。chmod +x /etc/init.d/serviceName 后重新执行上一个命令' M- _8 f4 U" y0 D8 N! a
+ n" W8 b* |  B" h+ A& {
2. 添加到开机启动任务时提示: service *** does not support chkconfig
! L) a3 E, s; `% N) y
+ e+ N9 H% k  ?0 g[root@redis01 test]# chkconfig --add test
  J5 u8 x! n: `: Oservice test does not support chkconfig, Y9 _( m& J) }1 G: ]. I; l2 e/ Z
解决方法:(在/etc/init.d/test添加两行注释)
' c6 G0 o" J0 m- x1 J5 \. [' w) h#!/bin/sh
+ D9 l" E4 W' u8 ]7 M#添加的两行注释内容如下:+ _# c' Q8 e0 d
# chkconfig:   2345 90 10
) k- d/ B9 C% R: P$ z7 S$ n+ Q* g# description:  test is a persistent key-value database
5 n1 Z& q6 j) V1 [5 u# 注释的意思是,test服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是104 j) l% j8 n# x( [( F# f$ h
[root@redis01 test]# chkconfig --add test0 E8 a* b4 M% ^. _
[root@redis01 test]# echo $?
( t( P5 P; |  m/ p0
' G6 n# Q8 t. {0 F" M, Z9 g1 b[root@redis01 test]# chkconfig --list | grep test
2 m6 }1 Y% H0 Z3 D6 itest          0:off1:off2:on3:on4:on5:on6:off
6 ~. W) m' u& w; O7 `  在编辑其它类似服务时,也可能出现这种情况,解决方法基本类似
8 B, N6 ?4 b1 R$ w* n9 w5 \. F1 T" S$ k
5 H& \# T7 D( h4 T  R- u/ y/ u

8 U; U+ J7 r! a$ P# Q" z. h+ | 参考:   1 e& v3 Y$ k# O: I5 `# y

- F; ?) V* w/ s$ M6 a" O2 t
  J" ?3 ~# ?8 f4 v解决“service XXX does not support chkconfig”的问题
6 V# h- y8 i, b( v! A; y$ M3 NProfessor哥关注 0人评论9933人阅读2014-11-19 20:55:25) K! b; O$ ~6 U5 d3 t( q

. n, ?. C  r- h4 ^为了便于管理linux服务器上的服务,很多人都喜欢用service nginx start这样的方式来管理,也就是系统服务吧
( l* ]8 h* r, u. l2 k这样可以设置自动启动
; J" M) n! v. Z1 `% Z
8 T; R2 _/ A6 d" D5 jchkconfig --level 35 nginx on0 ~; S' R) x9 B8 f( x; ~( x1 T
service nginx start|stop|status|reload
8 k5 K+ H/ @6 s7 a: e) Y
$ Z- Y; k, w% E编译安装的软件服务脚本多位于/usr/local/sbin  目录下,例如
5 u! }1 Y& j% O1 U% x( c( ~( \% b- c8 K  V/ B' n/ [2 S1 x
cp /usr/local/sbin/rc.radiusd /etc/init.d/radius
9 E6 q  C# q4 e. e , b/ @) Q* {, b- x* L1 F
1 F% U& d" w  P: ?2 I$ N
3 C; b% w# w% g5 ?' q7 P
等命令
5 R" Q* [+ b2 m$ o  Q5 ~( Q1 P) `# r4 G1 Y9 c
那么很多时候我们在制作完脚本后吧他拷贝到/etc/init.d后用chkconfig --add nginx的时候出现诸如下面的错误
9 @& n. n( Y7 k7 ]; z* L
8 H) m+ o( M3 l" V8 iservice xxxx does not support chkconfig
; ~  X; G5 c" W, T . \' c. m0 |) ~' v* z8 Y% [
那解决方法如下:
  q" Y# Q! n8 _4 u5 \7 F在脚本的开头添加下面两行即可:. R! F) Y& l& b* U% E
; U5 h/ Y. o- N0 r7 ?% H
#chkconfig: - 85 15) a& B0 O1 ?* ]- w
#description: nginx is a World Wide Web server. It is used to serve
- @  ^+ x# _& M9 j# f5 K1 u6 [4 X# m
7 y+ t. L; h( Q( o6 A井号不要去掉。
1 A7 o9 y- C, J# b2 x& C7 }6 N) N" h& b( X6 I
: J) b6 V; T: T* p. `# t
  L* Q" q) @) d& I6 v
服务不支持 chkconfig,另外还有一个非常重要的原因7 B% k* W- m) Z+ k
就是可执行权限7 N. r/ }& u* v* M' F0 t

* u) n; m% o+ J- _. R5 g* R! xchmod +x /etc/init.d/php-fpm
) `5 e2 M' K' d2 e! ~" j5 Y
作者: senlenced3    时间: 2021-3-16 10:59
可执行权限是指什么




欢迎光临 EDA365电子论坛网 (https://bbs.eda365.com/) Powered by Discuz! X3.2