linux添加开机启动脚本

说明


0——关机,
1——单用户,就是咱们以前修改root帐户密码的模式,
2——多用户模式,但比3模式少了一个nfs服务
3——多用户命令行模式,最经常使用
4——保留级别暂时没用,
5——图形模式,
6——重启
html

 

1、chkconfig-CentOS6之前

chkconfig就是CentOS6之前用来控制系统服务的工具,系统开机时启动的部分服务存储在/etc/init.d/目录下。咱们能够把须要开机启动的服务放在这个目录下而后用chkconfig来管理。mysql

查看命令

chkconfig --list #列出全部的系统服务。

操做命令

chkconfig --add httpd #增长httpd服务。
chkconfig --del httpd #删除httpd服务。
chkconfig --level httpd 2345 on #设置httpd在运行级别为二、三、四、5的状况下都是on(开启)的状态。
chkconfig --list mysqld #列出mysqld服务设置状况。
chkconfig --level 35 mysqld on #设定mysqld在等级3和5为开机运行服务,--level 35表示操做只在等级3和5执行,on表示启动,off表

示例linux

chkconfig --add nginx #添加nginx服务开机启动项

建立命令

(略)nginx

 

2、systemd-CentOS6之后

查看命令

systemctl list-units --all --type=service #查看全部服务
systemctl list-units --type=service #查看全部已经启动的服务

操做命令

针对单一服务的sql

systemctl enable crond ##设置开机启动crond服务或工具
systemctl disable crond ##设置关闭开机启动crond服务或工具
systemctl status crond ##查看crond服务当前状态,如是否运行
systemctl stop crond ##中止crond服务是,但开机仍会运行
systemctl start crond ##开启crond服务
systemctl restart crond ##重启crond服务
systemctl is-enabled crond ##检查crond服务是否开机启动

示例:vim

systemctl enable nginx.service #添加nginx服务开机启动项

 

建立命令

vim /lib/systemd/system/nginx.service #在系统服务目录里建立nginx.service文件

内容:bash

[Unit]

Description=nginx

After=network.target

  

[Service]

Type=forking

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/usr/local/nginx/sbin/nginx -s quit

PrivateTmp=true

  

[Install]

WantedBy=multi-user.target

 

说明:工具

[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为中止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、中止命令所有要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3ui

保存退出。spa

systemctl enable nginx.service #设置开机启动


3、chkconfig 和systemctl 对比

任            务 旧         指        令 新        指      令
使某服务自动启动 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
显示全部已启动的服务 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

 

4、对照表

1.Systemd 命令和 sysvinit 命令的对照表

2.Sysvinit 运行级别和 systemd 目标的对应表

 

参考文档

1.Linux系统管理初步(七)系统服务管理、chkconfig与systemd 编辑中:https://www.cnblogs.com/superlinux/p/bfd4812adffaccb36520279aaafcc160.html

2.Nginx+Center OS 7.2 开机启动设置:https://www.cnblogs.com/piscesLoveCc/p/5867900.html

3.Linux 设置程序开机自启动 (命令systemctl 和 chkconfig用法区别比较):https://blog.csdn.net/kenhins/article/details/74518978

相关文章
相关标签/搜索