首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux添加开机启动脚本 原

linux添加开机启动脚本 原

作者头像
晓歌
发布2018-09-28 15:20:31
1.6K0
发布2018-09-28 15:20:31
举报
文章被收录于专栏:破晓之歌破晓之歌

说明

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

一、chkconfig-CentOS6以前

chkconfig就是CentOS6以前用来控制系统服务的工具,系统开机时启动的部分服务存储在/etc/init.d/目录下。我们可以把需要开机启动的服务放在这个目录下然后用chkconfig来管理。

查看命令

chkconfig --list #列出所有的系统服务。

操作命令

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

示例

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

创建命令

(略)

二、systemd-CentOS6以后

查看命令

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

操作命令

针对单一服务的

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服务是否开机启动

示例:

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

创建命令

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

内容:

[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]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

保存退出。

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

三、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

四、对照表

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 说明
  • 一、chkconfig-CentOS6以前
    • 查看命令
      • 操作命令
        • 创建命令
        • 二、systemd-CentOS6以后
          • 查看命令
            • 操作命令
              • 创建命令
              • 四、对照表
                • 1.Systemd 命令和 sysvinit 命令的对照表
                  • 2.Sysvinit 运行级别和 systemd 目标的对应表
                  • 参考文档
                  相关产品与服务
                  云数据库 SQL Server
                  腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档