Linux 中 chkconfig 用法

sed "2 a # chkconfig:   2345 90 10" -i /etc/init.d/$TOMCAT_SERVICE_NAME
sed "3 a CATALINA_HOME=$TOMCAT_PATH" -i /etc/init.d/$TOMCAT_SERVICE_NAME

JAVA_HOME=`echo $JAVA_HOME`
sed "4 a JAVA_HOME=$JAVA_HOME" -i /etc/init.d/$TOMCAT_SERVICE_NAME

[root@wangjian-5 etc]# pwd
/etc
[root@wangjian-5 etc]# ls rc*
rc.local

rc0.d:
K10tomcat7  K15agentd  K15jboss7  K15nginx  K20rabbitmq  K36mysql  K50netconsole  K90network
rc1.d:
K10tomcat7  K15agentd  K15jboss7  K15nginx  K20rabbitmq  K36mysql  K50netconsole  K90network
rc2.d:
K15jboss7  K50netconsole  S10network  S64mysql  S85agentd  S85nginx  S90rabbitmq  S90tomcat7
rc3.d:
K50netconsole  S10network  S64mysql  S84jboss7  S85agentd  S85nginx  S90rabbitmq  S90tomcat7
rc4.d:
K50netconsole  S10network  S64mysql  S84jboss7  S85agentd  S85nginx  S90rabbitmq  S90tomcat7
rc5.d:
K50netconsole  S10network  S64mysql  S84jboss7  S85agentd  S85nginx  S90rabbitmq  S90tomcat7
rc6.d:
K10tomcat7  K15agentd  K15jboss7  K15nginx  K20rabbitmq  K36mysql  K50netconsole  K90network
rc.d:
init.d  rc0.d  rc1.d  rc2.d  rc3.d  rc4.d  rc5.d  rc6.d  rc.local

用 chkconfig 配置 linux 自启动服务
要将一个服务变为自动启动的服务,可以利用 chkconfig  命令来完成
例如要将 httpd 服务配置成开机自动启动服务
学习: [http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html]

1) 在 /etc/rc.d/init.d 目录下 增加 Apache 的启动脚本, 文件名可以取 apache [shell 文件]

#!/bin/sh
#description:Apache auto start-stop script.
#chkconfig:2345 20 80
APACHE_HOME=/home/psd/local/apache
APACHE_OWNER=psd

if [ !-f $APACHE_HOME/bin/apachectl]; then
    echo "no apache ctrl should check it"
	exit
fi 
case $1 in
   'start')
        su -$APACHE_OWNER -c "$APACHE_HOME/bin/apachectl start"
		;;
	'stop')
		su -$APACHE_OWNER -c "$APACHE_HOME/bin/apachectl stop"
		;;
	'restart')	
        su -$APACHE_OWNER -c "$APACHE_HOME/bin/apachectl restart"
		;;
esac

2) 赋脚本权限 chmod 755 apache
3) 添加服务  chkconfig --add apache

上面三步可以将 apache 服务增加到自启动服务列表中,通过 chkconfig --list 查看服务列表如果有
则证明配置成功 

[root@admin system]# chkconfig --list
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]'.

mysql          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
nginx          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
rabbitmq       	0:off	1:off	2:on	3:on	4:on	5:on	6:off
tomcat7        	0:off	1:off	2:on	3:on	4:on	5:on	6:off

这里需要注意:
"apache" 这个脚本中一定需要至少两行以上的注释,其中一行是:
#chkconfig: 2345 20 80
2345 表示系统运行级别是2.3.4.5 
20,这里表示启动优先级
80,这里表示关闭优先级 

[CentOS 7 自带MySQL,改使用MariaDB, 这里自带的 mysql 就放在 init.d 目录下面]
[root@CS4 ~]# /etc/init.d/mysql stop
Shutting down MySQL..                        [  OK  ]
[root@CS4 ~]# mysqld_safe --skip-grant-tables &
[root@CS4 ~]# mysql
mysql> use mysql
mysql> update user SET Password=PASSWORD("123456") where USER='root';

[安全模式进入mariadb]
[root@localhost ~]# mysqld_safe --skip-grant-tables &
[1] 12059
[root@localhost ~]# 171207 04:03:04 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
171207 04:03:04 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe --skip-g mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> 

chkconfig这个命令来管理Linux的服务 【的运行级别在 /etc/inittab 中设置】
Linux的运行级别,Linux一般会有7个运行级别:
0 - 停机
1 - 单用户模式
2 - 多用户,但是没有NFS ,不能使用网络
3 - 完全多用户模式,我最喜欢的模式
4 - 
5 - 图形化
6 - 重新启动

以TSM为例,安装好TSM的Server后,在目录/opt/tivoli/tsm/server/bin中可以找到一个文件dsmserv.rc,
将其ln到/etc/init.d这个目录中,最后改改名字,让它开起来更像一个服务的名字
[root@FWDB init.d]$ pwd
/etc/rc.d/init.d
[root@FWDB init.d]$ ln -s /opt/tivoli/tsm/server/bin/dsmserv.rc ./dsmserv
[root@FWDB init.d]$ ll *dsm*
lrwxrwxrwx 1 root root 37 Nov  8 10:43 dsmserv -> /opt/tivoli/tsm/server/bin/dsmserv.rc

可以像管理普通的服务那样控制TSM的随机启动和关闭了
[root@FWDB init.d]$ chkconfig --level 3  dsmserv on  
[root@FWDB init.d]$ ls /etc/rc3.d/*dsm*                    
/etc/rc3.d/S90dsmserv
[root@FWDB init.d]$ service dsmserv restart 

启动和关闭TSM的顺序是不同的,分别是90和10,数值越小越优先启动和关闭,这是在哪里的定义的呢? 
这是在dsmserv.rc这个文件的中定义的:
[root@FWDB init.d]$ head -n 8 dsmserv   
#!/bin/bash
# dsmserv   Start/Stop IBM Tivoli Storage Manager
# chkconfig: - 90 10
# description: Starts/Stops the IBM Tivoli Storage Manager Server
# processname: dsmserv
# pidfile: /var/run/dsmserv.pid

第5行的90代表Start的顺序,10代表Kill(Stop)的顺序,不过更常用的做法是
# chkconfig: 2345 90 10
指定需要注册的运行级别。注意一下,这种脚本的这两行是必须的,否则chkconfig --add会报错的:
# chkconfig: _____________
# description: _____________

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值