首页 > 代码库 > cenOS设置程序开机自启动的方法

cenOS设置程序开机自启动的方法

cenOS设置程序开机自启动的方法主要有两种

1.把启动程序的命令添加到/etc/rc.d/rc.local 文件夹中.

eg1.设置开机启动mysql

vim  /etc/rc.d/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 --更新访问时间和修改时间/usr/local/mysql/bin/mysql start

 eg2.设置开机启动tomcat

vim /etc/rc.d/rc.local# This script will be executed *after* all the other init scripts.# You can put your own initialization stuff in here if you dont# want to do the full Sys V style init stuff.export CATALINA_BASE=/usr/local/tomcat/export CATALINA_HOME=/usr/local/tomcat/export CATALINA_TMPDIR=/usr/local/tomcat/temp export JRE_HOME=/usr/java/jdk1.7.0_55/
#tomcat自启动
/usr/local/tomcat/bin/startup.sh start
touch /var/lock/subsys/local --此句如果文档里面有,无需再写

2、把写好的启动脚本添加到目录/etc/rc.d/init.d/,然后使用命令chkconfig设置开机启动。(推荐)

chkconfig 功能说明:检查,设置系统的各种服务。

语法:chkconfig [--add][--del][--list][系统服务] 或 chkconfig [--level <等级代号>][系统服务][on/off/reset]

--add 添加服务

--del 删除服务

--list 查看各服务启动状态

eg1.设置redis自启动

redis的启动要运行redis-server和redis.conf两个文件,所以设置开机自启动需要编写

编写脚本:vim /etc/init.d/redis 

vim /etc/init.d/redis  
# chkconfig: 2345 10 90 # description: Start and Stop redis PATH=/usr/local/bin:/sbin:/usr/bin:/bin REDISPORT=6379 #实际环境而定 EXEC=/usr/local/redis/src/redis-server #实际环境而定 REDIS_CLI=/usr/local/redis/src/redis-cli #实际环境而定 PIDFILE=/var/run/redis.pid CONF="/usr/local/redis/redis.conf" #实际环境而定 case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed." else echo "Starting Redis server..." $EXEC $CONF fi if [ "$?"="0" ] then echo "Redis is running..." fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE exists, process is not running." else PID=$(cat $PIDFILE) echo "Stopping..." $REDIS_CLI -p $REDISPORT SHUTDOWN while [ -x $(PIDFILE) ] do echo "Waiting for Redis to shutdown..." sleep 1 done echo "Redis stopped" fi ;; restart|force-reload) ${0} stop ${0} start ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2 exit 1 esac

 #修改权限和所有者

chown root.root  /etc/rc.d/init.d/redischmod  755 /etc/rc.d/init.d/redis

 #将redis放入到cenOS启动管理体系中

chkconfig --add redis

 #设置开机自启动

chkconfig redis on

 #测试redis的启动

redis-cli redis 127.0.0.1:6379> set foo 123OKredis 127.0.0.1:6379> get foo"123"redis 127.0.0.1:6379> exit

 eg2.fastdfs开机自启动

[root@localhost ~]# cp /usr/local/src/FastDFS/init.d/fdfs_storaged /etc/init.d/[root@localhost ~]# chkconfig --add fdfs_storaged[root@localhost ~]# chkconfig fdfs_storaged on[root@localhost ~]# service fdfs_storaged start

 ###############################如果是使用chkconfig命令来将tomcat设置为自启动############################

1、修改start.sh文件

vim /home/wwwroot/tomcat_wiki/bin/startup.sh

在文件头增加以下内容:

#!/bin/sh

# chkconfig: 2345 97 00  # description:tomcat auto start  #processname: tomcat

2、修改catalina.sh文件

vim /home/wwwroot/tomcat_wiki/bin/catalina.sh

增加以下内容:

export CATALINA_BASE=/home/wwwroot/tomcat_wiki  export CATALINA_HOME=/home/wwwroot/tomcat_wiki  export CATALINA_TMPDIR=/home/wwwroot/tomcat_wiki/temp  export JRE_HOME=/usr/local/jdk1.6.0_37

3、创建链接文件

ln –s /home/wwwroot/tomcat_wiki/bin/startup.sh /etc/init.d/tomcat

4、修改权限

chmod +x tomcat

5、添加启动

chkconfig –add  tomcat(add前是两个减号)

chkconfig tomcat on

6、检查

service tomcat start

或者reboot