CentOS 6和CentOS 7都可以定义开机启动哪些服务,但CentOS 6的命令是chkconfig,CentOS 7是systemctl。

本文将介绍两种命令的实现方式。

一、CentOS 6的服务

在CentOS 6下编写一个服务http,位于/etc/init.d目录下,具体的脚本如下:

#!/bin/bash
# chkconfig:
# description: http .... start() {
echo "HTTP is enabled now"
}
stop() {
echo "HTTP is disable now"
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
start
stop
;;
*)
echo "USAGE $0 {start|stop|restart}"
exit
esac

注意,两个注释"# chkconfig: 2345 10 90"和 "# description: http ...."表示:启动的level和优先级,已经服务的描述。这两段注释是一定要加上的。否则服务添加是报错。

通过如下命令实现把服务注册到chkconfig中:

chkconfig --add http

然后可以通过:

chkconfig http on

定义开机启动这个服务。另外可以查看chkconfig的状态:

chkconfig --list

二、CentOS 7的服务

在CentOS 7的机器中创建一个服务的脚本: /etc/init.d/myuptime。具体的脚本如下:

#!/bin/bash

start() {
echo starting
while true
do
uptime >> /root/myuptime.txt
sleep
done
}
stop() {
echo stoping
pid=`ps -ef | grep myuptime | grep -v grep | awk '{print $2}'`
kill $pid &
}
case "$1" in
start)
start
;;
stop)
stop
;;
*)
echo "USAGE $0 {start|stop|restart}"
exit
esac

在/etc/systemd/system中创建服务描述文件myuptime.service

[Unit]
Description=uptime Service
After=network.target [Service]
Type=simple
User=root
ExecStart=/etc/init.d/myuptime start
ExecStop=/etc/init.d/myuptime stop [Install]
WantedBy=multi-user.target

这个文件中包含Unit、Service和Install三个部分。定义了描述、服务属性的类型和安装参数等。其中ExecStart、ExecStop定义了启动和停止的实现方式。

配置好后,运行:

[root@hwcentos70-01 system]#systemctl enable myuptime

ln -s '/etc/systemd/system/myuptime.service' '/etc/systemd/system/multi-user.target.wants/myuptime.service'

systemctl把myuptime服务加入到了启动项目中。

执行:

[root@hwcentos70-01 system]#systemctl start myuptime

查看:

[root@hwcentos70-01 system]#systemctl status myuptime

myuptime.service - uptime Service

Loaded: loaded (/etc/systemd/system/myuptime.service; enabled)

Active: active (running) since Fri -- :: UTC; 10s ago

Main PID:  (myuptime)

CGroup: /system.slice/myuptime.service

├─ /bin/bash /etc/init.d/myuptime start

└─ sleep 

Feb  :: hwcentos70- systemd[]: Started uptime Service.

Feb  :: hwcentos70- myuptime[]: starting

通过以上的方法实现把myuptime作为服务加入启动项。

CentOS6和CentOS7服务开机启动的更多相关文章

  1. 【CentOS】centos7上查看服务开机启动列表

    centos7上查看服务开机启动列表 命令: systemctl list-unit-files; 点击回车,可以向下翻页查询

  2. CentOS7添加开机启动服务或脚本

    方法一(rc.local) 改方式配置自动启动最为简单,只需要修改rc.local文件 由于在centos7中/etc/rc.d/rc.local的权限被降低了,所以需要赋予其可执行权 chmod + ...

  3. centos7 管理开机启动:systemd

    一.CentOS7 systemd 介绍 在 CentOS7 中,使用 systemd 来管理其他服务是否开机启动,systemctl 是 systemd 服务的命令行工具 [root@mysql ~ ...

  4. linux系统chkconfig使用方法及服务开机启动

    一.基础知识 有关linux系统开机过程.运行等级,执行权限请看另一篇:linux系统启动过程及运行等级详解. 本篇文章实践的系统:centos6.5 二.创建服务 通过之前的说明,我们知道了如果需要 ...

  5. centos设置服务开机启动

    Linux CentOS设置服务开机启动的方法 by 天涯 · 2013/07/26 CentOS设置服务开机启动的两种方法 1.利用 chkconfig 来配置启动级别 在CentOS或者RedHa ...

  6. CentOS设置服务开机启动的方法

    CentOS设置服务开机启动的两种方法 1.利用 chkconfig 来配置启动级别在CentOS或者RedHat其他系统下,如果是后面安装的服务,如httpd.mysqld.postfix等,安装后 ...

  7. linux系统设置服务开机启动3种方法,Linux开机启动程序详解

    linux系统设置服务开机启动 方法1:.利用ntsysv伪图形进行设置,利用root登陆 终端命令下输入ntsysv 回车:如下图     方法2:利用命令行chkconfig命令进行设置 简要说明 ...

  8. centos 6 与 centos 7 服务开机启动、关闭设置的方法

    简单说明下 centos 6 与 centos 7 服务开机启动.关闭设置的方法: centos 6 :使用chkconfig命令即可. 我们以apache服务为例: #chkconfig --add ...

  9. (转)CentOS 7 sytemctl 自定义服务开机启动

    CentOS 7 sytemctl 自定义服务开机启动 原文:http://blog.csdn.net/ithomer/article/details/51766319 CentOS 7继承了RHEL ...

随机推荐

  1. effect request

    from bs4 import BeautifulSoup import os filepath = 'D:\\pymine\\clean\\spider_map\\baidu_map_html_fi ...

  2. window.scrollTo(0,document.body.scrollHeight)

    滑轮到页面底部 左下角window.scrollTo(0,document.body.scrollHeight) 右下角window.scrollTo(document.body.scrollWidt ...

  3. What I learned from competing against a ConvNet on ImageNet

    http://karpathy.github.io/2014/09/02/what-i-learned-from-competing-against-a-convnet-on-imagenet/

  4. 低秩近似 low-rank approximation

  5. spark的若干问题

    问题1:SPARK与HADOOP之间的关系? spark是一种高效处理hadoop分布式数据的处理引擎.借助hadoop的yarn框架,spark就可以运行在hadoop集群中.同时spark也可以处 ...

  6. linux 中sort命令 按照指定列排序

    sort怎样按指定的列排序0000 27189 41925425065f 15 419254250663 7 419254250675 5 419254250691 76 419254250693 2 ...

  7. elk示例-精简版2

    作者:Danbo 时间:2016-03-13 1.保存进Elasticsearch Logstash可以试用不同的协议实现完成将数据写入Elasticsearch的工作,本节中介绍HTTP方式. 配置 ...

  8. php 通用redis类

    php 通用redis类 <?php /** * redis操作类 * 说明,任何为false的串,存在redis中都是空串. * 只有在key不存在时,才会返回false. * 这点可用于防止 ...

  9. Mac OS下新建文本文档

    ---恢复内容开始--- 介绍 不知道小伙伴们有没有发现这样一件事情:Mac下没有新建文本文档!如果你恰好经常需要新建类似于.cpp,.in,.out等文件的话,每次终端用一堆$ cd命令再加上一句$ ...

  10. SpringCloud之服务提供和服务调用的搭建

    pom文件: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3 ...