CentOS中增加开机启动程序的两种方式
第一种方式是chkconfig
抄袭链接:
http://www.abhigupta.com/2010/06/how-to-auto-start-services-on-boot-in-centos-redhat/
http://www.centos.org/docs/5/html/Deployment_Guide-en-US/s1-services-chkconfig.html
http://support.suso.com/supki/CentOS_Init_startup_scripts
要在CentOS或者Redhat OS中开机自动启动多个服务,可以使用内置的chkconfig应用。这个应用在/sbin路径下面,如果你是一个普通的用户,而不是root用户的话,/sbin可能不在用户路径下面,因此,你可能必须要使用全路径来访问到chkconfig应用。
要自动开始一个新的service:
- 在/etc/init.d路径下面找到/etc/init.d路径,然后找到服务的名称。比如mysqld或者httpd.
- 通过/sbin/chkconfig --add mysqld 来增加这个服务到chkconfig
- 执行/sbin/chkconfig --list mysqld来确保已经加上了。
- 执行sudo /sbin/chkconfig mysqld on 来设定为自动启动
要去掉一个已有的自动启动:
- 执行sudo /sbin/chkconfig mysqld off
chkconfig命令还可以被用来activate和deactivate服务。这个chkconfig --list命令展示了一系列系统服务,以及他们在运行级别0-6下是否自动启动或者关闭。在列表的结尾是一个被xinetd管理的服务表。
如果用chkconfig --list 命令来定义是否被xinetd管理,它展示了是否xinetd服务被启用还是没有被启用。举例来说,chkconfig --list rsync 返回了下面的结果:
rsync on
上面显示的就是,xinetd 服务会启动rsync,如果xinetd在运行中,rsync就也在运行中。
如果你使用chkconfig --list来看一个/etc/rc.d中的服务,那么这个服务的每个运行级别中的设定都被显示出来,例如:chkconfig --list httpd返回了如下的输出:
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
chkconfig 可以设定在特定的运行级别中是否启用这个程序.例如,要在运行级别3,4中将nscd关闭,使用如下命令:
chkconfig --level 345 nscd off
第二种方式是rc.local
抄袭链接:
http://drewsymo.com/2013/11/run-a-script-or-command-on-linux-centos-startup/
/etc/rc.local可以解决开机启动的问题。/etc/rc.local是一个symboloc link链接到/etc/rc.d/rc.local.rc.local是一个脚本,这个脚本在醉蛛的启动服务之后就会被执行。
下面是一个使用/etc/rc.local的脚本实例:
#!/bin/sh## This script will be executed *after* all the other init scripts.# You can put your own initialization stuff in here if you don't# want to do the full Sys V style init stuff. touch /var/lock/subsys/local
如何在linux启动的时候,设定自动执行脚本或者命令
只要简单的将你想要执行的命令或者脚本增加到这个脚本中去。
例如,我们想要运行/opt路径下面一个叫做kittens.sh的脚本来在Linux脚本启动的时候打印系统登录状态。
创建一个kittens.sh的启动脚本
touch /opt/kittens.sh && echo last>>/opt/kittens.sh && chmod +x /opt/kittens.sh
增加脚本到linux启动
echo sh /opt/kittens.sh >>/etc/rc.local
完成!接下来你要重启一下linux,那么kitten.sh脚本就会运行而且你会看到之前的用户登录信息。

浙公网安备 33010602011771号