一、启动脚本 /etc/rc.local 启动

最简单的一种方式,在启动脚本 /etc/rc.local (其实 /etc/rc.local 是/etc/rc.d/rc.local 的软链接文件,实际的脚本文件为 /etc/rc.d/rc.local)中添加要启动的程序。当然需要注意将 /etc/rc.d/rc.local 和 /etc/rc.local 添加启动权限。 
    这种方式虽然简单,但是并不能保证程序在其依赖的系统服务之后启动,所以有可能会启动不成功。

二、编写启动服务.service

centos支持为自定义的服务编写service文件。centos系统服务脚本目录:/usr/lib/systemd。有系统(system)和用户(user) 之分,如需要开机没有登录的情况下就能运行的程序,存在系统服务里/lib/systemd/system/;反之,需要用户登录之后才能运行的程序,存在用户里/lib/systemd/user/,服务以 .service 结尾。

以ngix 开机运行为例: 
1. 建立服务文件 
nginx.service

[Unit]
Description=nginx
After=network.target [Service]
Type=forking
ExecStart=/www/lanmps/init.d/nginx start
ExecReload=/www/lanmps/init.d/nginx restart
ExecStop=/www/lanmps/init.d/nginx stop
PrivateTmp=true [Install]
WantedBy=multi-user.target

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

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

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

2.保存目录 
以754的权限保存在目录 /lib/systemd/system 中。

3. 设置开机自启动 
systemctl enable nginx.service

其他相关命令

任务 旧指令 新指令
使某项服务自启动 chkconfig --level 3 httpd on systemctl enable httpd.service
使某项服务不自启动 chkconfig --level 3 httpd off systemctl disable httpd.service
检查服务状态 serivce 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
启动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

centos 开机启动服务的更多相关文章

  1. centos 开机启动服务 systemctl

    systemctl 实现开机自启服务 转载起一个好听的名字 最后发布于2018-06-26 13:49:06 阅读数 13473  收藏 展开 systemctl是RHEL 7 的服务管理工具中主要的 ...

  2. (转) CentOS 7添加开机启动服务/脚本

    CentOS 7添加开机启动服务/脚本 原文:http://blog.csdn.net/wang123459/article/details/79063703 一.添加开机自启服务 在CentOS 7 ...

  3. centos 7 开机启动服务项优化

    1. 使用 systemctl list-unit-files 可以查看启动项 systemctl list-unit-files | grep enable 过滤查看启动项如下 abrt-ccpp. ...

  4. Hortonworks HDP Sandbox定制(配置)开机启动服务(组件)

    定制Hortonworks HDP开机启动服务能够这样做:本文原文出处: http://blog.csdn.net/bluishglc/article/details/42109253 严禁不论什么形 ...

  5. Centos7.x:开机启动服务的配置和管理

    一.开机启动服务的配置 1.创建服务配置(权限754) vim /usr/lib/systemd/system/nginx.service 文件内容解释 [Unit]:服务的说明Description ...

  6. linux chkconfig添加开机启动服务

    --add:增加所指定的系统服务,让chkconfig指令得以管理它,并同时在系统启动的叙述文件内增加相关数据: --del:删除所指定的系统服务,不再由chkconfig指令管理,并同时在系统启动的 ...

  7. linux自定义开机启动服务和chkconfig使用方法

    linux自定义开机启动服务和chkconfig使用方法 1. 服务概述在linux操作系统下,经常需要创建一些服务,这些服务被做成shell脚本,这些服务需要在系统启动的时候自动启动,关闭的时候自动 ...

  8. (转)linux自定义开机启动服务和chkconfig使用方法

    原文:https://www.cnblogs.com/jimeper/archive/2013/03/12/2955687.html linux自定义开机启动服务和chkconfig使用方法 1. 服 ...

  9. Linux创建Jenkins启动脚本以及开机启动服务

    1.jenkins.sh #!/bin/bash ###主要目的用于开机启动服务,不然 启动jenkins.war包没有java -jar的权限 JAVA_HOME=/usr/lib/jdk1.8.0 ...

随机推荐

  1. MongoDB - 在Windows上安装

    1 下载MongoDB社区版, 下载链接 http://www.mongodb.org/downloads?_ga=1.129742796.1997610832.1481940266 2 安装Mong ...

  2. Hiho coder 1236 2015 北京网络赛 Score

    五维偏序..一开始被吓到了,后来知道了一种BITSET分块的方法,感觉非常不错. 呆马: #include <iostream> #include <cstdio> #incl ...

  3. Html5的表单

    跨浏览器 https://github.com/westonruter/webforms2 https://github.com/Modernizr/Modernizr http://www.blog ...

  4. 技巧分享:解决Word 2010当中“分页符”造成的空白行

    技巧分享:解决Word 2010当中“分页符”造成的空白行 P1:关于“分页符” 在Word当中插入“分页符”之后,后面的内容就会“更起一段”.就好像“换行符”(回车)会让后面的内容“另起一行”一样. ...

  5. [转]在 Web 项目中应用 Apache Shiro

    目录[-] 用户权限模型 图 1. 用户权限模型 认证与授权 Shiro 认证与授权处理过程 Shiro Realm 清单 1. 实现自己的 JDBC Realm 为何对 Shiro 情有独钟 与 S ...

  6. LayaAir学习笔记

    1.Text文本类 var Text=laya.display.Text; Laya.init(620,400); Laya.stage.bgColor="#323232";//设 ...

  7. redis.conf配置详细翻译解析

    # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 bytes # 1kb ...

  8. Java8函数式编程

    在Java8的 java.util.function中包含以下几个接口 1.Function,先上源码 /* * Copyright (c) 2010, 2013, Oracle and/or its ...

  9. AES 与Base64加密

    public class AESHelper     {         /// <summary>         /// 获取密钥         /// </summary&g ...

  10. .net平台下获取高精度时间类

    原文:http://www.it118.org/specials/321869dd-98cb-431b-b6d2-82d973cd739d/d940a15c-0d36-4369-8de0-b07cf3 ...