centos之服务配置 与 服务开机启动

论坛 期权论坛 脚本     
匿名技术用户   2021-1-2 22:26   173   0

http://blog.csdn.net/gbenson/article/details/51083817

CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,需要开机不登陆就能运行的程序,存在系统服务里,即:/usr/lib/systemd/system目录下

每一个服务以.service结尾,一般会分为3部分:[Unit]、[Service]和[Install],我写的这个服务用于开机运行tomcat项目:

[plain] view plain copy在CODE上查看代码片派生到我的代码片
  1. vim /usr/lib/systemd/system/tomcat.service
[plain] view plain copy在CODE上查看代码片派生到我的代码片
  1. [Unit]
  2. Description=tomcatapi
  3. After=network.target
  4. [Service]
  5. Type=forking
  6. PIDFile=/usr/local/tomcat/tomcat.pid
  7. ExecStart=/usr/local/tomcat/bin/startup.sh
  8. ExecReload=
  9. ExecStop=/usr/local/tomcat/bin/shutdown.sh
  10. PrivateTmp=true
  11. [Install]
  12. WantedBy=multi-user.target

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

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

[Install]部分是服务安装的相关设置,可设置为多用户的

服务脚本按照上面编写完成后,以754的权限保存在/usr/lib/systemd/system目录下,这时就可以利用systemctl进行测试了

最后用以下命令将服务加入开机启动即可:

[html] view plain copy在CODE上查看代码片派生到我的代码片
  1. systemctl enable tomcat

任务

旧指令 新指令
使某服务自动启动 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 httpd.service (仅显示是否 Active)
显示所有已启动的服务 chkconfig --list systemctl list-units --type=service
启动某服务 service httpd start systemctl start httpd.service
停止某服务 service httpd stop systemctl stop httpd.service
重启某服务 service httpd restart systemctl restart httpd.service

启动nginx服务

systemctl start nginx.service

设置开机自启动

systemctl enable nginx.service

停止开机自启动

systemctl disable nginx.service

查看服务当前状态

systemctl status nginx.service

重新启动服务

systemctl restart nginx.service

查看所有已启动的服务

systemctl list-units --type=service
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP