linux开机启动脚本
大概分为5个步骤:建议:先理解整个业务的完整流程,再去按照步骤做相应的操作,理解更深,不容易忘记1.进入/etc目录,查找rc.local文件:ll rc.*2.给/etc/rc.d/rc.local 读写权限:chmod + 777 /etc/rc.d/rc.local3.reboot重启linux:此时开机时就会自动加载/etc/rc.d/rc.local 文件4.写入执行脚本命令:linux
·
文章目录
第一种:配置/etc/rc.local 大概分为5个步骤:
建议:先理解整个业务的完整流程,再去按照步骤做相应的操作,理解更深,不容易忘记
1.进入/etc目录,查找rc.local文件:
ll rc.*
2.给/etc/rc.d/rc.local 读写权限:
chmod + 777 /etc/rc.d/rc.local
3.reboot重启linux:
此时开机时就会自动加载/etc/rc.d/rc.local 文件
4.写入执行脚本命令:
linux可以开机自动加载/etc/rc.d/rc.local后,将自动执行脚本的执行命令写入到rc.local文件
5.自动启动脚本代码:
注意:着重提示一下,代码中APP_NAME的名字一定要不常用,不能出现相似的。不然脚本就会有bug,经调试了很久,才发现这个问题
#!/bin/bash
APP_NAME=minio-server
usage() {
echo "Usage: sh 脚本名称.sh [start|stop|restart|status]"
exit 1
}
process_exist(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
start(){
process_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ."
else
nohup /home/minio/${APP_NAME} server /home/minio/data > /home/minio/data/minio.log 2>&1 &
#BUILD_ID=dontKillMe nohup java -jar /home/sn_software/3I-tools.jar --spring.profiles.active=dev > ./deploy-sn-tool.log 2>&1 &
echo "${APP_NAME} started"
fi
}
stop(){
process_exist
if [ $? -eq "0" ]; then
kill -9 $pid
echo "${APP_NAME} stoped"
else
echo "${APP_NAME} is not running"
fi
}
status(){
process_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}
restart(){
stop
start
}
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
第二种:添加system启动文件
将启动脚本放在此目录 /etc/systemd/system 大概分为4个步骤:
建议:先理解整个业务的完整流程,再去按照步骤做相应的操作,理解更深,不容易忘记
1.进入/etc/systemd/system,创建应用脚本:
> cd /etc/systemd/system
> touch wms.service
> chmod 644 wms.service
2.复制脚本内容:
[Unit]
Description=mes service
After=syslog.target network.target
[Service]
Type=simple
ExecStart=/usr/local/jdk1.8/bin/java -jar /home/mes/mes-service/mes.jar --spring.profiles.active=prod &
ExecStop=/bin/kill -15 $MAINPID
User=root
Group=root
[Install]
WantedBy=multi-user.target
3.添加为系统开机启动:
> systemctl enable mes
4.测试重启:reboot
更多推荐
已为社区贡献3条内容
所有评论(0)