[Linux] 定期执行脚本(自动升级solo)
序
最近把博客从Tomcat转到Docker中,虽然期间遇见不少问题,但都解决了,使用Docker后,发现真的在配置上方便了很多,现想把solo变成自动升级,这样就不用每次升级都要手动升级,要实现这个功能首先,要写升级脚本,然后用Linux中的crontab进行定期执行此脚本。
升级脚本
脚本程序如下:
1 #!/bin/bash
2 #
3 # solo uograde script
4 # Reference Guide :https://hacpai.com/article/1492881378588
5 # Reference Guide :https://hacpai.com/article/1554909875932
6 #
7 echo "---------------------starting---------------------" >> /home/timingscript/solologs.txt
8 start_time=`date +'%Y-%m-%d %H:%M:%S'`
9 docker pull b3log/solo
10 docker stop solo
11 docker rm solo
12 docker run --detach --name solo \
13 --network=host --env RUNTIME_DB="MYSQL" --volume /opt/solo/skins/:/opt/solo/skins/ \
14 --env JDBC_USERNAME="root" \
15 --env JDBC_PASSWORD="XXXXXX" \
16 --env JDBC_DRIVER="com.mysql.cj.jdbc.Driver" \
17 --env JDBC_URL="jdbc:mysql://localhost:3306/solo?useUnicode=yes&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC" \
18 b3log/solo --listen_port=8080 --server_scheme=https --server_host=aeneag.xyz --server_port=80
19 if [[ $? == 0 ]];
20 then
21 echo -e "Upgrade Successful " >> /home/timingscript/solologs.txt
22 else
23 echo -e "solo docker Errors in upgrade, please check " >> /home/timingscript/solologs.txt
24 fi
25 end_time=`date +'%Y-%m-%d %H:%M:%S'`
26 start_seconds=$(date --date="$start_time" +%s);
27 end_seconds=$(date --date="$end_time" +%s);
28 echo "Upgrade time:"$((end_seconds-start_seconds))"s" >> /home/timingscript/solologs.txt
29 echo "start_time: $start_time ,end_time:$end_time" >> /home/timingscript/solologs.txt
30 echo "------------------------end------------------------" >> /home/timingscript/solologs.txt
31
Crontab配置
系统目前是Centos7.6,自带Crontab,不需安装,打开配置文件
1 vi /etc/crontab
打开后,默认如下:
1 SHELL=/bin/bash
2 PATH=/sbin:/bin:/usr/sbin:/usr/bin
3 MAILTO=root
4
5 # For details see man 4 crontabs
6
7 # Example of job definition:
8 # .---------------- minute (0 - 59)
9 # | .------------- hour (0 - 23)
10 # | | .---------- day of month (1 - 31)
11 # | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
12 # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
13 # | | | | |
14 # * * * * * user-name command to be executed
配置自动升级如下:
1 0 3 * * mon root /home/timingscript/soloupdate.sh
设置为每个周的周一凌晨三点自动更新
测试
脚本已进行测试,可以进行更新,每周周一定时更新待测试。
而今更笃凌云志,
莫教冰鉴负初心!
——乾隆
莫教冰鉴负初心!
——乾隆
评论
0 评论