CentOS chkconfig 添加 服务 开机启动

news/2024/7/24 12:28:03

1. 准备一个测试脚本service.sh, 用于模拟一个服务程序。真实情况下可以是任意可执行程序或脚本

cd /
[root@localhost /]# vim service.sh

#!/bin/bash

while [ 1 ]
do
echo `date` >> /tmp/d.log
sleep 1
done


2. 创建chkconfig服务脚本(CentOS7是用systemd管理服务,但是兼容chkconfig。相对而言chkconfig更加简单)

[root@localhost init.d]# cd /etc/init.d/
[root@localhost init.d]# cp network myservice
[root@localhost init.d]# vim myservice

直接拷贝network(用作参考!),然后编辑myservice内容如下

#! /bin/bash
#
# myservice       myservice
#
# chkconfig: 2345 10 90
# description: myservice description...
#

# See how we were called.
case "$1" in
  start)
	sh /service.sh
        ;;
  stop)
	pid=`ps aux | grep "/service.sh" | grep -v grep | awk '{print $2}'`
	kill $pid
        ;;
  status)
	ps aux | grep "/service.sh"
	;;
  restart|reload|force-reload)
        cd "$CWD"
	$0 stop
	$0 start
	rc=$?
	;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|reload|force-reload}"
        exit 2
esac

exit 0

3. 添加到chkconfig服务列表,开机启动

[root@localhost init.d]# chkconfig --add myservice
[root@localhost init.d]# chkconfig myservice on
[root@localhost init.d]# chkconfig | grep myservice

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

myservice          0:off    1:off    2:on    3:on    4:on    5:on    6:off

可以看到,已经添加到启动列表。由于没有使用systemd,打印了提示信息,做一个简单的服务无需理会。


4. 重启系统,检查服务是否正常启动(线上系统不要随意重启!!!,此处仅仅为了验证开机能否自启动

[root@localhost init.d]# reboot

[root@localhost ~]# ps aux | grep /service
root       674  0.0  0.1 115256  1540 ?        S    10:52   0:00 sh /service.sh
root      1325  0.0  0.0 112656   968 pts/0    S+   10:54   0:00 grep --color=auto /service
[root@localhost ~]# tail -f /tmp/d.log
2017年 12月 28日 星期四 10:54:20 CST
2017年 12月 28日 星期四 10:54:21 CST
2017年 12月 28日 星期四 10:54:22 CST
2017年 12月 28日 星期四 10:54:23 CST
2017年 12月 28日 星期四 10:54:24 CST
2017年 12月 28日 星期四 10:54:25 CST
2017年 12月 28日 星期四 10:54:26 CST
2017年 12月 28日 星期四 10:54:27 CST
2017年 12月 28日 星期四 10:54:28 CST
2017年 12月 28日 星期四 10:54:29 CST
2017年 12月 28日 星期四 10:54:30 CST
2017年 12月 28日 星期四 10:54:31 CST
2017年 12月 28日 星期四 10:54:32 CST
^C

可以看到程序成功启动并且正常执行




http://www.niftyadmin.cn/n/914515.html

相关文章

php esa加密方式

1、mcrypt_encrypt 2、openssl_encrypt转载于:https://www.cnblogs.com/loweringye/p/7193339.html

解决clion2016.3不能支持搜狗输入法的问题

解决clion2016.3不能支持搜狗输入法的问题

Vue HBuilder打包为app流程

vue 打包为app 记录以下自己将web app打包成移动端app的步骤及问题 事先准备,开发完成的web app项目(也可以利用vue-cli脚手架构建vue模板项目),npm run dev可以正常预览的项目 1,将项目目录下config文件内index.js中a…

添加购物车,横向滑动选择属性

最近写的一个微信的商城系统,其他功能我都会稍后更新,这里主要更新一个横向滑动选择属性功能,注意的是,这个功能下面的第二种属性我没有随着第一种的选择变化而变化(项目比较急,没写)&#xff0…

Fedora 利用dd制作Linux启动盘

1. 准备一个iso镜像 ubuntukylin-17.04-desktop-amd64.iso2. 准备一个U盘,查看U盘设备[yeqianglocalhost Downloads]$ ls /dev/sd sda sda1 sda2 sda3 sda4 sda5 sda6 sda7 sdb sdb1 sdc sdd sde sdf [yeqianglocalhost Downloads]$ ls /dev/s…

vue axios + Java 跨域

1.在 vue main.js 的 里面 import axios from axios//引入axios,来发送请求 Vue.prototype.axios axios //设置axios全局引用 axios.defaults.timeout 5000;// 在超时前,所有请求都会等待 5 秒 // 配置请求头 axios.defaults.headers.post[Content-Type]appl…

Python案例 003 (一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数)...

代码本身不难,难点在于解题方法,有点高中的感觉 #! /usr/bin/python # -*- coding:utf-8 -*- from __future__ import division #----> 返回精确的商""" 题目:一个整数,它加上100后是一个完全平方数&#xff…

linux c pcre 正则匹配多个目标

#include <pcre.h>#define OVECCOUNT 30void main() {char *in "20171208121020Z 20171208121020Z\n20171208121020Z";char *pattern "(19|20)([\\d]{2})([0-1][0-9])([0-3][0-9])([0-2][0-9])([0-6][0-9])([0-6][0-9])(\\.?[0-9]{0,3})(Z?)";…