一、启动脚本 /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. Ubuntu14.04运行行roscore错误

    错误提示: 网络配置有问题,ping不同自己(ping localhost). vim ~/.bashrc 在底部加入: export ROS_HOSTNAME=localhostexport ROS ...

  2. IFRAM随内部长宽高变化

    <iframe src="" id="iframe_CustomerVisitRecord" width="700" height=& ...

  3. [转]MySQL innodb buffer pool

    最近在对公司的 MySQL 服务器做性能优化, 一直对 innodb 的内存使用方式不是很清楚, 乘这机会做点总结. 在配置 MySQL 的时候, 一般都会需要设置 innodb_buffer_poo ...

  4. 161223、mysql锁的两个例子

    版本:mysql5.5.52 存储引擎:InnoDB 隔离级别:READ-COMMITTED 示例一: 事务1:左图      事务2:右图 1. 事务2中属于快照读,基于多版本的并发控制协议--MV ...

  5. Bash:-3次错误输入退出脚本

    Limit_Condition() { let count++ ]];then echo "超过3次机会,自动关停脚本" exit fi Comfirm() { count= wh ...

  6. Mysql查询重复记录

    第一步 使用group by 和 having cout 查找重复字段 SELECT t1.`order_book_id` FROM `quant_stock_info` t1 GROUP BY t1 ...

  7. MVC中view和controller相互传值的方法

    MVC项目中,在view层如果使用前台框架,框架中会有封装好的相互传值方法.但是,那些postdata[][]方法不一定能够满足功能需求,反而一些常用的传值方法可能会刚好解决它们的不足.总结如下: 一 ...

  8. c++多线程のunique和lazy initation

    unique更方便使用,但是会消耗更多的计算机性能 onceflag保证一个线程被调用一次,防止不能的加锁开锁

  9. Excel图表的基本类型与选择

    图表类型分成4大类:1. 比较 2. 分布 3. 构成 4. 联系 转载自:https://zhuanlan.zhihu.com/peter-zhang-jie

  10. nfs 配置

    服务端1.打印系统版本cat /etc/redhat-release2.检查并安装NFS服务rpm -aq nfs-utils portmap rpcbindyum grouplistyum grou ...