CentOS7中系统服务脚本,有系统system和用户user之分

存放system服务启动项路径:/lib/systemd/system/,系统开机没有登录就能运行的服务,754权限保存

存放user服务器启动项路径:/usr/lib/systemd,相应用户登录后才能运行的服务


systemctl list-units --type=service    显示所有系统添加的服务,简介显示

systemctl status                       显示所有系统添加的服务,显示详细信息

systemctl start/stop/restart/ sshd.service    启动/关闭/重启指定服务

systemctl enable/disable sshd.service    将服务器添加到开机启动或者不开机启动


service httpd start/stop/restart  

启动、关闭、重启服务  

systemctl start/stop/restart/ sshd.service


service httpd status    查看指定服务状态    systemctl status httpd.service


chkconfig --list    查看已启动的服务    systemctl list-units --type=service

chkconfig --level 3 httpd on   启动指定服务,在相应运行级别  systemctl enable httd.service

chkconfig --level 3 httpd off  关闭指定服务,在相应运行级别  systemctl disable httd.service


1.建立服务文件

[html] view plaincopyprint?

  1. vim /lib/systemd/system/nginx.service  

[plain] view plaincopyprint?

  1. [Unit]  

  2. Description=nginx  

  3. After=network.target  

  4.    

  5. [Service]  

  6. Type=forking  

  7. ExecStart=/www/lanmps/init.d/nginx start  

  8. ExecReload=/www/lanmps/init.d/nginx restart  

  9. ExecStop=/www/lanmps/init.d/nginx  stop  

  10. PrivateTmp=true  

  11.    

  12. [Install]  

  13. WantedBy=multi-user.target  

[Unit]:服务的说明

Description:描述服务
After:描述服务类别

[Service]服务运行参数的设置

Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径