傅令江的光影色彩世界
CentOS 7.x设置自定义开机启动,添加自定义系统服务
2017-10-25 令狐江

Centos 系统服务脚本目录:

[plain] view plain copy
 print?
  1. /usr/lib/systemd/    
有系统(system)和用户(user)之分,

如需要开机没有登陆情况下就能运行的程序,存在系统服务(system)里,即:

[plain] view plain copy
 print?
  1. lib/systemd/system/    
反之,用户登录后才能运行的程序,存在用户(user)里

服务以.service结尾。

这边以nginx开机运行为例 IT

1.建立服务文件

[plain] view plain copy
 print?
  1. vim /lib/systemd/system/nginx.service   
[plain] view plain copy
 print?
  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]的启动、重启、停止命令全部要求使用绝对路径

[Install]服务安装的相关设置,可设置为多用户 Linux学习,http:// linux.it.net.cn

2.保存目录

以754的权限保存在目录:

[plain] view plain copy
 print?
  1. /lib/systemd/system  

3.设置开机自启动

[plain] view plain copy
 print?
  1. systemctl enable nginx.service  

4.其他命令

任务 旧指令 新指令
使某服务自动启动 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服务 I
[plain] view plain copy
 print?
  1. systemctl start nginx.service  

设置开机自启动

[plain] view plain copy
 print?
  1. systemctl enable nginx.service  
停止开机自启动

[plain] view plain copy
 print?
  1. systemctl disable nginx.service  
查看服务当前状态

[plain] view plain copy
 print?
  1. systemctl status nginx.service  
重新启动服务

[plain] view plain copy
 print?
  1. systemctl restart nginx.service   
查看所有已启动的服务
[plain] view plain copy
 print?
  1. systemctl list-units --type=service  

 

 

 

systemctl  -- 系统服务管理器

systemctl 是系统服务管理器命令,它实际上将 service 和 chkconfig 这两个命令组合到一起。

直接运行命令可以列出所有正在运行的服务,输出列表具有更详细的信息,比如:


[root@beyes   command]# systemctl
... ...
sendmail.service          loaded active exited        LSB: start and stop sendmail
sshd.service              loaded active running       LSB: Start up the OpenSSH server daemon
udev.service              loaded active running       udev Kernel Device Manager
... ...



这里,还有一个 systemd-cgls 命令可以以树状的形式列出正在运行的进程信息。

如果要启动 httpd 服务,那么运行下面命令:

[root@beyes   command]# systemctl start httpd.service


注意,上面的 httpd 后面的 .service 是不能少的。

同理,停止服务和重启服务可以分别如下运行命令:

# systemctl stop httpd.service       #停止服务
# systemctl restart httpd.service              #重启服务



如果我们要查看服务的运行状态,那么如下运行:

[root@beyes   command]# systemctl status httpd.service
httpd.service - LSB: start and stop Apache HTTP Server
      Loaded: loaded (/etc/rc.d/init.d/httpd)
      Active: active (running) since Wed, 22 Feb 2012 10:37:30 +0800; 2s ago
     Process: 2573 ExecStop=/etc/rc.d/init.d/httpd stop (code=exited, status=0/SUCCESS)
     Process: 2589 ExecStart=/etc/rc.d/init.d/httpd start (code=exited, status=0/SUCCESS)
    Main PID: 2594 (httpd)
      CGroup: name=systemd:/system/httpd.service
          ├ 2594 /usr/sbin/httpd
          ├ 2596 /usr/sbin/httpd
          ├ 2597 /usr/sbin/httpd
          ├ 2598 /usr/sbin/httpd
          ├ 2599 /usr/sbin/httpd
          ├ 2600 /usr/sbin/httpd
          ├ 2601 /usr/sbin/httpd
          ├ 2602 /usr/sbin/httpd
          └ 2603 /usr/sbin/httpd



如果我们打算让服务可以随机启动,那么如下运行:

[root@beyes   command]# systemctl enable httpd.service
httpd.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig httpd on



用 chkconfig 命令检测一下服务是否运行成功

[root@beyes   command]# chkconfig --list |grep httpd

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

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


可见服务已经在 第2 到 第5 运行等级打开。

同理禁止服务随机启动可以如下运行:

[root@beyes   command]# systemctl disable httpd.service
httpd.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig httpd off


用 chkconfig 检测一下:

[root@beyes   command]# chkconfig --list |grep httpd

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

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


已经关闭成功。

此外,我们还可以直接检测服务是否随机启动或否,如下运行命令:

[root@beyes   command]# systemctl is-enabled httpd.service; echo $?
httpd.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig httpd --level=5
0


上面,0 表示该服务已经使能随机启动;如果返回 1 那么表示该服务不随机启动

 

发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容