【shell实战之编写服务启动脚本-12】


EHEL6:/etc/init.d 或 /etc/rc.d/init.d
EHEL7:/usr/lib/systemd/system/

1.EHEL6编写服务脚本

a.脚本具有执行权限
b.要有以下两行内容:

# chkconfig: 2345 10 90      在什么级别启动和关闭的顺序
# description: Activates/Deactivates

其中-表示所有级别,启动链接文件为/etc/rc.d/rc[0-6].d/…,关闭链接文件为/etc/rc.d/rc[0-6].d/…
7个运行级别:0-6 (cat /etc/inittab)
0 关机
1 单用户模式
2 多用户模式,没有NFS
3 命令行
4
5 图形化界面
6 重启
注:/etc/inittab配置文件中可永久改为命令行或者图形界面(查看运行级别:runlevel)
c.case调用函数
d.添加成系统服务:chkconfig add nginx
e.开机自启动:chkconfig nginx on
查看:chkconfig --list nginx

例子:源码安装nginx,基于service脚本

RPM包下载地址:http://nginx.org/packages/rhel/
源码安装nginx:http://nginx.org/download/
1)解压
[root@www ~]# tar xf nginx-1.10.0.tar.gz -C /usr/local/src/
2)配置
c和c++的编译环境:[root@www nginx-1.10.0]# yum install gcc gcc-c++ make -y
[root@www ~]# cd /usr/local/src/nginx-1.10.0/
安装依赖包:yum install pcre-devel zlib-devel -y
[root@www nginx-1.10.0]# ./configure --prefix=/usr/local/nginx
3)编译
make
4)安装
make install
编写脚本:
1、复制模板
[root@www ~]# scp 192.168.150.107:/etc/init.d/nginx .

2、修改脚本
nginx= N G I N X − / u s r / l o c a l / n g i n x / s b i n / n g i n x c o n f f i l e = {NGINX-/usr/local/nginx/sbin/nginx} conffile= NGINX/usr/local/nginx/sbin/nginxconffile={CONFFILE-/usr/local/nginx/conf/nginx.conf}

3、修改配置文件pid: /var/run/nginx.pid
[root@www ~]# grep pid /usr/local/nginx/conf/nginx.conf
#pid logs/nginx.pid;
pid /var/run/nginx.pid;

4、复制到/etc/init.d,增加执行权限
[root@www ~]# chmod +x /etc/init.d/nginx

5、添加系统服务,设置开机自启动
[root@www ~]# chkconfig --add nginx
[root@www ~]# chkconfig nginx on
[root@www ~]# chkconfig --list nginx

6、测试脚本 :service nginx start
curl 192.168.131.109

2.EHEL7编写服务脚本

每一个服务文件都以.service结尾,文件内容主要分为三个部分:
[Unit]#单元,主要用于对服务的简要说明
Description:描述
After:指在哪个服务后面启动,一般指网络服务后面启动-network.target

[service]#服务,用于配置具体的服务启动关闭等相关参数信息
Type:forking 是后台运行的形式
PIDFile:是服务的pid文件路径。开启后,该服务配置文件必须配置pid文件路径。
ExecStart:启动命令
ExecReload:重启服务指令
ExecStop:停止服务指令
PrivateTmp:是否为服务分配独立的临时空间

其中TYPE还有下面这些值:
simple(默认值):ExecStart字段启动的进程为主进程
forking:ExecStart字段将以fork()方式启动,此时父进程将会退出,子进程将成为主进程
oneshot:类似于simple,但只执行一次,Systemd 会等它执行完,才启动其他服务
dbus:类似于simple,但会等待 D-Bus 信号后启动
notify:类似于simple,启动结束后会发出通知信号,然后 Systemd 再启动其他服务
idle:类似于simple,但是要等到其他任务都执行完,才会启动该服务。一种使用场合是为让该服务的输出,不与其他服务的输出相混合
[Install] #是服务安装的相关设置,可设置为多用户的

例子:编写源码安装系统服务启动脚本
前提准备:一台192.168.131.107上进行rpm包安装nginx,一台192.168.131.109进行源码安装nginx。
1.192.168.131.107上rpm包安装nginx:

wget -c http://nginx.org/packages/rhel/7Server/x86_64/RPMS/nginx-1.10.0-1.el7.ngx.x86_64.rpm 

2.192.168.131.109上源码安装nginx,并把上一台机器的服务文件传到这台机器:
a.拷贝文件为了之后的改编

scp 192.168.131.107:/usr/lib/systemd/system/nginx.service  /usr/lib/systemd/system/

b.源码地址:http://nginx.org/download/nginx-1.10.0.tar.gz
解压之前先安装一些软件:yum install -y gcc gcc-c++ make
解压:tar -xf nginx-1.10.0.tar.gz -C /usr/local/
切换到解压后的目录:cd /usr/local/nginx-1.10.0
配置:./configure --prefix=/usr/local/nginx #指定安装地址

若配置时会出现如下错误:
#./configure --prefix……检查编辑环境时出现:
checking for APR... no
configure: error: APR not found . Please read the documentation

解决:
下载所需软件包:

wget http://archive.apache.org/dist/apr/apr-1.4.5.tar.gz  
wget http://archive.apache.org/dist/apr/apr-util-1.3.12.tar.gz 

对这些软件包进行编译安装:
a:解决apr not found问题>>>>>>

[root@xt test]# tar -zxf apr-1.4.5.tar.gz  
[root@xt test]# cd  apr-1.4.5  
[root@xt apr-1.4.5]# ./configure --prefix=/usr/local/apr  
[root@xt apr-1.4.5]# make && make install  

b:解决APR-util not found问题>>>>

[root@xt test]# tar -zxf apr-util-1.3.12.tar.gz  
[root@xt test]# cd apr-util-1.3.12  
[root@xt apr-util-1.3.12]# ./configure --prefix=/usr/local/apr-util -with-apr=/usr/local/apr/bin/apr-1-config  
[root@xt apr-util-1.3.12]# make && make install 

再使用:./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/
解决源码安装遇到apr not found等问题
编译:make
安装:make install

3.对传过来的nginx.service进行改编:在指定的安装地址/usr/local/nginx/目录下找到命令启动文件路径和配置文件路径,对nginx.service进行修改

4.测试:查看端口是否开启,首页是否可以访问
修改过服务文件,就需要重新加载:systemctl daemon-reload
启动服务:systemctl start nginx
开机自启动:systemctl enable nginx

[root@localhost system]# ps -ef | grep httpd
root       2857      1  0 12:26 ?        00:00:00 /usr/local/httpd/bin/http -DFOREGROUND
daemon     2858   2857  0 12:26 ?        00:00:00 /usr/local/httpd/bin/http -DFOREGROUND
daemon     2859   2857  0 12:26 ?        00:00:00 /usr/local/httpd/bin/http -DFOREGROUND
daemon     2860   2857  0 12:26 ?        00:00:00 /usr/local/httpd/bin/http -DFOREGROUND
root       2943   1248  0 12:26 pts/0    00:00:00 grep --color=auto httpd
[root@localhost system]# curl 192.168.131.109
<html><body><h1>It works!</h1></body></html>

问题:centos7编写自己的服务,运行systemctl后卡住了(即shell阻塞了)。当我执行systemctl命令后shell阻塞在那里,没有像平时执行命令那样自动结束(只能自己按Ctrl+C强制结束)问题如下:

[root@localhost system]# systemctl start httpd
^C

强制结束后,查看程序发现目标程序启动是成功的:systemctl status httpd
解决:导致此问题的原因是:[service]类型选择有问题, 不应该选forking类型;类型改为Type=simple(或删除Type=forking这句),问题便得到解决。
Type=notify 与 Type=simple 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 libsystemd-daemon.so 提供。
Type=simple (默认值) systemd认为该服务将立即启动。服务进程不会 fork 。
从上可以看到,当类型为forking时,systemd会认为所运行当该服务本身是守护进程即本身会fork,且只有父进程退出后systemd才会退出,但由于参考例子并不是守护进程,故systemd一直处于阻塞等待状态,默认的simple无等待这一环节。
解决shell阻塞:编写自己的服务,运行systemctl后卡住了

3.练习:centos7 源码安装httpd 2.4,编写基于service启动脚本

前提准备:一台192.168.131.107上进行rpm包安装Apache,一台192.168.131.109进行源码安装Apache。(注意:rpm包软件的版本要和源码安装的软件版本一致)

步骤:
1. 在192.168.131.107上rpm包安装Apache:yum install –y httpd
在这里插入图片描述
2.192.168.131.109上源码安装nginx,并把上一台机器的服务文件传到这台机器:
1)拷贝文件为了之后的改编
在这里插入图片描述
2)源码安装Apache
在这里插入图片描述
解压之前先安装一些软件:yum install -y gcc gcc-c++ make
解压:tar -xf httpd-2.4.25.tar.gz -C /usr/local/
切换到解压后的目录:cd /usr/local/ httpd-2.4.25
配置:./configure --prefix=/usr/local/httpd #指定安装地址

在这里插入图片描述
在这里插入图片描述

编译:make
安装:make install

3.对传过来的httpd.service进行改编:在指定的安装地址/usr/local/httpd/目录下找到命令启动文件路径,对/usr/lib/system/system/httpd.service进行修改
在这里插入图片描述

注:[service]里的Type=simple否则将出现运行systemctl后卡住了(即shell阻塞了)的现象

4.启动服务:
修改过服务文件,就需要重新加载:systemctl daemon-reload
启动服务:systemctl start httpd
开机自启动:systemctl enable httpd

在这里插入图片描述
5.测试:查看端口是否开启,首页是否可以访问
ps -ef | grep httpd

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

4.centos利用Apache自带的脚本编写服务启动脚本,并且将服务添加到系统服务里

在192.168.131.109机器上源码安装Apache,将其安装在/usr/local/httpd
则Apache自带脚本的位置:

[root@localhost system]# ll /usr/local/httpd/bin/apachectl 
-rwxr-xr-x 1 root root 3431 711 22:19 /usr/local/httpd/bin/apachectl

1)首先将该脚本拷贝到/etc/init.d/httpd

[root@localhost ~]#cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@localhost ~]# ll /etc/init.d
总用量 44
-rw-r--r--. 1 root root 18281 329 2019 functions
-rwxr-xr-x  1 root root  3492 712 09:52 httpd
-rwxr-xr-x. 1 root root  4569 329 2019 netconsole
-rwxr-xr-x. 1 root root  7923 329 2019 network
-rw-r--r--. 1 root root  1160 88 2019 README

2)改进脚本:在脚本里添加chkconfig 和description这两行

[root@localhost ~]#vim /etc/initd.d/httpd
#!/bin/sh
#chkconfig:35 85 15 
#description:Activates/Deactivates httpd
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Apache control script designed to allow an easy command line interface
# to controlling Apache.  Written by Marc Slemko, 1997/08/23
# 
# The exit codes returned are:
#   XXX this doc is no longer correct now that the interesting
#   XXX functions are handled by httpd
#	0 - operation completed successfully
#	1 - 
#	2 - usage error
#	3 - httpd could not be started
#	4 - httpd could not be stopped
#	5 - httpd could not be started during a restart
#	6 - httpd could not be restarted during a restart
#	7 - httpd could not be restarted during a graceful restart
#	8 - configuration syntax error
#
# When multiple arguments are given, only the error from the _last_
# one is reported.  Run "apachectl help" for usage info
#
ACMD="$1"
ARGV="$@"
#
# |||||||||||||||||||| START CONFIGURATION SECTION  ||||||||||||||||||||
# --------------------                              --------------------
# 
# the path to your httpd binary, including options if necessary
HTTPD='/usr/local/httpd/bin/httpd'
#
# pick up any necessary environment variables
if test -f /usr/local/httpd/bin/envvars; then
  . /usr/local/httpd/bin/envvars
fi
#
# a command that outputs a formatted text version of the HTML at the
# url given on the command line.  Designed for lynx, however other
# programs may work.  
LYNX="lynx -dump"
#
# the URL to your server's mod_status status page.  If you do not
# have one, then status and fullstatus will not work.
STATUSURL="http://localhost:80/server-status"
#
# Set this variable to a command that increases the maximum
# number of file descriptors allowed per child process. This is
# critical for configurations that use many file descriptors,
# such as mass vhosting, or a multithreaded server.
ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`"
# --------------------                              --------------------
# ||||||||||||||||||||   END CONFIGURATION SECTION  ||||||||||||||||||||

# Set the maximum number of file descriptors allowed per child process.
if [ "x$ULIMIT_MAX_FILES" != "x" ] ; then
    $ULIMIT_MAX_FILES
fi

ERROR=0
if [ "x$ARGV" = "x" ] ; then 
    ARGV="-h"
fi

case $ACMD in
start|stop|restart|graceful|graceful-stop)
    $HTTPD -k $ARGV
    ERROR=$?
    ;;
startssl|sslstart|start-SSL)
    echo The startssl option is no longer supported.
    echo Please edit httpd.conf to include the SSL configuration settings
    echo and then use "apachectl start".
    ERROR=2
    ;;
configtest)
    $HTTPD -t
    ERROR=$?
    ;;
status)
    $LYNX $STATUSURL | awk ' /process$/ { print; exit } { print } '
    ;;
fullstatus)
    $LYNX $STATUSURL
    ;;
*)
    $HTTPD "$@"
    ERROR=$?
esac

exit $ERROR

3)将Apache服务添加成系统服务,并设置开机自启

[root@localhost ~]# chkconfig --add httpd
[root@localhost ~]# chkconfig httpd on
[root@localhost ~]# chkconfig --list httpd
[root@localhost init.d]# chkconfig --list httpd

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

httpd          	0:1:2:3:4:5:6:

#2345都是开
4)测试:使用service httpd start 开启服务,检测服务端口是否开启,首页是否可以访问

[root@localhost init.d]# service httpd start
[root@localhost system]# ps -ef | grep httpd
root       2857      1  0 12:26 ?        00:00:00 /usr/local/httpd/bin/http -DFOREGROUND
daemon     2858   2857  0 12:26 ?        00:00:00 /usr/local/httpd/bin/http -DFOREGROUND
daemon     2859   2857  0 12:26 ?        00:00:00 /usr/local/httpd/bin/http -DFOREGROUND
daemon     2860   2857  0 12:26 ?        00:00:00 /usr/local/httpd/bin/http -DFOREGROUND
root       2943   1248  0 12:26 pts/0    00:00:00 grep --color=auto httpd
[root@localhost system]# curl 192.168.131.109
<html><body><h1>It works!</h1></body></html>

在这里插入图片描述

[root@localhost init.d]# service httpd stop
[root@localhost init.d]# curl 192.168.131.109
curl: (7) Failed connect to 192.168.131.109:80; 拒绝连接
[root@localhost init.d]#  ps -ef | grep httpd
root       5494   3892  0 19:30 pts/2    00:00:00 grep --color=auto httpd
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑着蜗牛追汤圆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值