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

linux 开机启动脚本或者服务

2016-12-22 16:15 323 查看
准备工作:

1. 先编写shell,将需要启动的服务都放在此脚本中,取名restart.sh

2. 在restart.sh 中将环境变量 执行一下 : .  /etc/profile

3. chmod +x restart.sh 将脚本可正确执行

以下是我滴例子:

restart.sh 文件:

#!/bin/bash

#环境变量

. /etc/profile

tomcatPath='/usr/tomcat/apache-tomcat-7.0.72'

nginxpath='/usr/local/nginx'

httpdPath='/usr/local/apache-httpd'

#tomcat-1

cd ${tomcatPath}/bin

./startup.sh

#nginx

cd ${nginxpath}/sbin

./nginx  -c ${nginxpath}/conf/nginx.conf

#httpd

cd ${httpdPath}/bin

./apachectl start

两种方法:

方法一: /etc/rc.local

1. vi rc.local 增加以下,注意脚本的全路径

cd 脚本存放的目录

./restart

2. 修改可执行

chmod +x rc.local

注意rc.local 是软连接/etc/rc.d/rc.local 所以要改处的

方法二:添加为系统服务

1. cd /etc/rc.d/init.d/

新建一个脚本,取名:my

可执行:chmod +x my

内容:(参考nginx开机启动的脚本)

#!/bin/bash

#
chkconfig: 2345 85 15 

(这句必须有,否则chkconfig
--add 会报错 :服务不支持 chkconfig。含义:此行的2345参数表示,在哪些运行级别启动,启动序号(S85);关闭序号(K15))

start()
{

cd 脚本存放的目录

./restart.sh

}

case
"$1" in
start)
        start
        ;;

*)
        echo $"Usage: $prog {start}"
        exit 1
esac

exit 

2.
添加chkconfig

chkconfig
--add my          (首先,添加为系统服务,注意add前面有两个横杠)

chkconfig
my on  (开机自启动)

chkconfig
--list (列表显示)

service
my start(启动服务,就是执行my的脚本)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: