常用的有两种方式:
1、在/etc/rc.d/rc.local里面写启动命令
如:
/usr/common/apache-tomcat-8.5.37/bin/startup.sh
2、更常用的是注册到系统服务里
以下举几个常用的例子:
(1)tomcat开机自启动(软件本身就提供启动、停止和重启等命令)
在/etc/init.d/目录下建立一个文件如tomcat8080(避免后面部署多个tomcat的时候好区分),里面的脚本内容如下:
#!/bin/sh
#chkconfig: 2345 90 06
#description: tomcat8080
case “$1” in
start)
cd /usr/common/tomcat8080/bin
./startup.sh
echo “tomcat8080 startup”
;;
stop)
cd /usr/common/tomcat8080/bin
./shutdown.sh
echo “tomcat8080 stopped”
;;
restart)
cd /usr/common/tomcat8080/bin
./shutdown.sh
echo “tomcat8080 stopped”
cd /usr/common/tomcat8080/bin
./startup.sh
echo “tomcat8080 restart”
;;
*)
echo “start|stop|restart”
;;
esac
exit $?
设置执行权限:
chmod +x tomcat8080
添加到开机启动任务:
chkconfig –add tomcat8080
测试:
service tomcat8080 start
(2)elasticsearch开机自启动(软件本身没有提供启动、停止和重启等命令)
在/etc/init.d/目录下建立一个文件如elastic,里面的脚本内容如下:
#!/bin/sh
#chkconfig: 2345 80 05
#description: elasticsearch
case “$1” in
start)
su elastic<<! # 切换到用户elastic,因为elasticsearch不运行root用户启动
cd /usr/common/elasticsearch-6.5.4
./bin/elasticsearch -d
!
echo “elasticsearch startup”
;;
stop)
# 停止就是找到正在运行的进程,然后kill掉
es_pid=`ps aux|grep elasticsearch | grep -v ‘grep elasticsearch‘ | awk ‘{print $2}’`
kill -9 $es_pid
echo “elasticsearch stopped”
;;
restart)
# 重启就是先找到正在运行的进程,然后kill掉,最后启动
es_pid=`ps aux|grep elasticsearch | grep -v ‘grep elasticsearch‘ | awk ‘{print $2}’`
kill -9 $es_pid
echo “elasticsearch stopped”
su elastic<<!
cd /usr/common/elasticsearch-6.5.4
./bin/elasticsearch -d
!
echo “elasticsearch startup”
;;
*)
echo “start|stop|restart”
;;
esac
exit $?
设置执行权限:
chmod +x elastic
添加到开机启动任务:
chkconfig –add elastic
测试:
service elastic start
3、编码问题
如遇到编码问题,可以参考【linux脚本编码问题】
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!