您的位置:首页 > 运维架构 > Linux

Linux中设置服务开机自启动的三种方式

2016-10-17 17:41 369 查看
一、自己写的脚本设置成开机启动,如启动后自动发一封邮件给管理员:
1,vim  /etc/rc.local 

2, 添加如下内容

source /etc/profile
/data/server/tomcat/bin/startup.sh(目标脚本的绝对名) >> /data/tomcatstart.log
内容说明:
1,source /etc/profile 参数不能少,因为执行本文件命令时环境变量还没加载,
需要环境变量的程序会服务运行。
2,>> /data/tomcatstart.log 启动错误日志。如果服务启动失败,可到这个文件查看具
体原因
3,请用脚本的绝对路径


二、以服务的方式添加到service服务中,如Mysql,ssh服务等;

参考URL:http://www.cnblogs.com/nerxious/archive/2013/01/18/2866548.html
http://jiajun.iteye.com/blog/387265


1,有现成的service脚本,以mysql二进制安装设置自启动为例:

1,添加服务到服务列表 cp /mysql/support-files/mysql.server /etc/init.d/mysql

2,设置开机自启动 chkconfig mysql on

3,chkconfig --list | grep -i mysql 查看开启情况


2,以自己的脚本为例:

 1、编写如下面的脚本simpleTest:

Shell代码  


#!/bin/bash  

#chkconfig:2345 80 05 --指定在哪几个级别执行,0一般指关机,6指的是重启,其他为正常启动。80为启动的优先级,05为关闭的优先级别  

#description:simple example service  

RETVAL=0  

start(){ #启动服务的入口函数  

echo  "simple example service is started..."  

}  

  

stop(){ #关闭服务的入口函数  

echo  "simple example service is stoped..."  

}  

  

#使用case选择  

case $1 in  

start)  

start  

;;  

stop)  

stop  

;;  

*)  

echo "error choice ! please input start or stop";;  

esac  

exit $RETVA  

3、运行chmod +x /etc/rc.d/init.d/simpleTest,使之可直接执行

4、运行chkconfig --add simpleTest,把该服务添加到配置当中(可能有问题,推荐chkconfig simpleTest on 的方式实现,原文http://jiajun.iteye.com/blog/387265)

5、运行chkconfig --list simpleTest,可以查看该服务进程的状态

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: