linux编写自启动shell脚本

1.需求分析

   在很多情况下,程序员都做着重复枯燥的工作,虽然这些工作也是必须的,其实这些重复性的工作可以执行脚本替代;今天笔者就如何编写自启动shell脚本减少程序员开启服务器后的环境开启工作;

2.配置环境

linux版本:centos-6.3

jdk:1.8

tomcat: 9.0

3.实现方案

①抒写脚本

 

#!/bin/sh
#chkconfig: 2345 80 90
#description:hello.sh
echo "=============close firewall=================="
service iptables stop
echo "==============start redis server============"
redis-server /usr/local/redis/redis-4.0.1/redis.conf
cd /usr/local/tomcat/apache-tomcat-9.0.0.M22/bin
echo "=================start tomcat================"
./startup.sh

命名为hello.sh

 

前面三行是固定样式,后续的都是脚本实现需求

②赋予hello.sh可执行权限

 

chmod +x ./hello.sh

linux编写自启动shell脚本

 

③将hello.sh移动到 /etc/profile.d

 

mv hello.sh /etc/profile.d

linux编写自启动shell脚本

 

④添加自启动项hello.sh(执行添加自启动前提:该脚本必须置于/etc/init.d下)

 

chkconfig --add hello.sh

linux编写自启动shell脚本

 

⑤默认开启自启动项hello.sh

 

chkconfig hello.sh on

 

linux编写自启动shell脚本

⑥如果在第④步报错,则进入hello.sh目录并编辑

依次执行

 

:set ff

查看文件格式,这里可能会是doc

 

所以还需执行

 

:set ff=unix

 

⑦重启服务器,脚本就自动执行了

4.测试效果

如果重启服务后以下错误

 

linux编写自启动shell脚本

 

接下来在tomcat/bin中的catalina.sh中添加以下两句代码(看清楚位置)

export JAVA_HOME=/usr/local/java/jdk1.8.0_60

export JRE_HOME=/usr/local/java/jdk1.8.0_60/jre

具体位置如下

linux编写自启动shell脚本

 

然后reboot重启服务就好

 

linux编写自启动shell脚本

访问tomcat

linux编写自启动shell脚本

 

访问redis

linux编写自启动shell脚本