Linux学习笔记(3)linux服务管理与启停(开机自启与自定义服务)

一、LINUX 系统服务管理

1、RHEL/OEL 6.X及之前

  service命令用于对系统服务进行管理,比如启动(start)、停止(stop)、重启(restart)、查看状态(status)等。

  chkconfig用于查看、设置服务器的运行级别。ntsysv用于直观方便的设置各个服务是否自动启动。

  service命令本身是一个shell脚本,它在/etc/init.d/目录查找指定的服务器脚本,然后调用该服务脚本来完成任务。

2、RHEL/OEL 7.X之后

  systemctl是新的服务管理器命令,该命令是用来替代service和chkconfig的;

  systemctl是一个systemd工具,主要负责控制systemd系统的服务管理器。

  启用服务就是在当前的 "runlevel" 的配置文件目录 /etc/systemd/system/multi-user.target.wants/ 里,

  建立 /usr/lib/systemd/system 里面对应服务配置文件的软链接。禁用服务就是删除此软链接。

3、指令对照表

旧指令指的是redhat 6.X之前,新指令指定是红帽7.X之后

任务 旧指令(redhat 6.x及之前) 新指令(redhat 7.X及之后)
使某服务自动启动 chkconfig --level 3 httpd on systemctl enable httpd service
使某服务不自动启动 chkconfig --level 3 httpd off systemctl disable httpd service
检查服务状态 service httpd status

systemctl status httpd.service(详细信息)

systemctl is-active test.service(仅显示是否活跃)

加入自定义服务 chkconfig --add test systemctl load test.service
删除服务 chkconfig --del test 停掉应用,删除对应配置文件
显示所有已启动的服务 chkconfig --list systemctl list-units --type = service
启动某服务 service httpd start systemctl start httpd.service
停止某服务 service httpd stop systemctl stop httpd.service
以字符界面启动 /etc/inittab   ctrl+alt+F3 systemctl set-default multi-user.target

 

 

 

 

 

 

 

 

 

 

 

 

 

 

重启某服务      service httpd restart      systemctl restart httpd.service

查看某服务的自启信息  chkconfig --list httpd

 ----------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------

4、自定义封装成服务,开机自启

【4.1】CentOS6自定义系统服务

    6比较简单,直接把启动的 sh 命令脚本文件放到 /etc/init.d/ 下即可。
    然后使用例如:chkconfig --level 2345 https on

【4.2】CentOS7自定义系统服务
CentOS7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,需要开机不登陆就能运行的程序,存在系统服务里,
即:/usr/lib/systemd/system目录下.
CentOS7的每一个服务以.service结尾,一般会分为3部分:[Unit]、[Service]和[Install] 


[Unit]部分主要是对这个服务的说明,内容包括Description和After,Description 用于描述服务,After用于描述服务类别

[Service]部分是服务的关键,是服务的一些具体运行参数的设置.
Type=forking是后台运行的形式,
User=users是设置服务运行的用户,
Group=users是设置服务运行的用户组,
PIDFile为存放PID的文件路径,
ExecStart为服务的具体运行命令,
ExecReload为重启命令,
ExecStop为停止命令,
PrivateTmp=True表示给服务分配独立的临时空间
#注意:[Service]部分的启动、重启、停止命令全部要求使用绝对路径,使用相对路径则会报错!

[Install]部分是服务安装的相关设置,可设置为多用户的
首先,使用systemctl start [ 服务名(也是文件名) ] 可测试服务是否可以成功运行, 如果不能运行则可以使用 systemctl status [ 服务名(也是文件名) ]查看错误信息和其他服务信息,然后根据报错进行修改,直到可以start,如果不放心还可以测试restart和stop命令。 接着,只要使用systemctl enable xxxxx就可以将所编写的服务添加至开机启动即可。


实例:服务用于开机运行tomcat项目:
#vim /usr/lib/systemd/system/tomcat.service
[Unit]
Description=java tomcat project
After=tomcat.service
  
[Service]
Type=forking
User=users
Group=users
PIDFile=/usr/local/tomcat/tomcat.pid
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecReload=
ExecStop=/usr/local/tomcat/bin/shutdown.sh
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target
添加可执行权限:
  chmod 754 /usr/lib/systemd/system/tomcat.service
设置为开机自启动:
  #systemctl enable tomcat.service
 常用指令(以tomcat服务为例):
启动某服务 
  systemctl start   tomcat.service
 
停止某服务
  systemctl stop   tomcat.service
 
重启某服务
  service tomcat   restart
  systemctl restart   tomcat.service
 
使某服务自动启动(如tomcat服务)
 
  systemctl enable   tomcat.service
 
使某服务不自动启动
  systemctl disable   tomcat.service
 
检查服务状态 
  systemctl   status tomcat.service (服务详细信息)
  systemctl   is-active tomcat.service(仅显示是否Active)
 
显示所有已启动的服务
  systemctl   list-units --type=service

【CentOS7 实用技巧】 

systemctl daemon-reload #重载系统服务(写好自定义服务后,如果要立马可用,必须使用这个重载)
systemctl list-unit-files #查看所有开机自启的服务

 

5、最佳实践》mysql封装成自定义服务【el7】

[Unit]
Description=Mysql
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/mysql/data/XXX.pid
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false

[Install]
WantedBy=multi-user.target
systemctl daemon-reload #重载系统服务(写好自定义服务后,如果要立马可用,必须使用这个重载)
systemctl list-unit-files #查看所有开机自启的服务

6.最佳实践》es封装成自定义服务【el6】

cat << eof >/etc/init.d/es_exporter
#!/bin/bash
# chkconfig: 2345 10 90
# description: es's exporter
touch /var/log/es_exporter.log
chown prometheus.prometheus /var/log/es_exporter.log
es_path_config=`ps -ef|grep elastic|grep "Des.path.conf"|sed 's# #\n#g'|grep "Des.path.conf"|awk -F'=' '{print $2}'`/
configfile=elasticsearch.yml
ip=`cat ${es_path_config}${configfile}|grep network.host|awk -F":" '{print $2}'|sed 's/[[:space:]]//g'`
port=`cat ${es_path_config}${configfile}|grep http.port|awk -F":" '{print $2}'|sed 's/[[:space:]]//g'`
su prometheus -s /bin/bash -c "/usr/local/elasticsearch_exporter/elasticsearch_exporter --es.uri=http://${ip}:${port} &"  >> /var/log/es_exporter.log
eof
    chown prometheus.prometheus /etc/init.d/es_exporter
    chmod +x /etc/init.d/es_exporter
    chkconfig --add es_exporter
    chkconfig --level 3 es_exporter on
    service es_exporter start

二、开机自动启动脚本

1./etc/rc.local

最简单粗暴的方式直接在脚本/etc/rc.d/rc.local(和/etc/rc.local是同一个文件,软链)末尾添加自己的脚本
然后,增加脚本执行权限

chmod +x /etc/rc.d/rc.local

2.crontab

crontab -e
@reboot /home/user/test.sh

3. /etc/profile.d/

每次登录自动执行
  也可以设置每次登录自动执行脚本,在/etc/profile.d/目录下新建sh脚本,
  /etc/profile会遍历/etc/profile.d/*.sh

另外,几个脚本的区别:
(1) /etc/profile: 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. 并从/etc/profile.d目录的配置文件中搜集shell的设置。

(2) /etc/bashrc: 为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取(即每次新开一个终端,都会执行bashrc)。

(3) ~/.bash_profile: 每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次。默认情况下,设置一些环境变量,执行用户的.bashrc文件。

(4) ~/.bashrc: 该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该该文件被读取。

(5) ~/.bash_logout: 当每次退出系统(退出bash shell)时,执行该文件. 另外,/etc/profile中设定的变量(全局)的可以作用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承 /etc/profile中的变量,他们是”父子”关系。

(6) ~/.bash_profile: 是交互式、login 方式进入 bash 运行的~/.bashrc 是交互式 non-login 方式进入 bash 运行的通常二者设置大致相同,所以通常前者会调用后者。

 

posted @ 2018-10-30 22:36  郭大侠1  阅读(1634)  评论(0编辑  收藏  举报