常用定时脚本
查看脚本执行过程命令:bash -x strtst.s
常用定时脚本
#nginx日志备份脚本
#!/bin/sh
BASE_DIR=/usr/local/nginx
BASE_FILE_NAME1=access.log
BASE_FILE_NAME2=error.log
CURRENT_PATH=$BASE_DIR/logs
BAK_PATH=$BASE_DIR/backlogs
CURRENT_FILE1=$CURRENT_PATH/$BASE_FILE_NAME1
CURRENT_FILE2=$CURRENT_PATH/$BASE_FILE_NAME2
BAK_TIME=`/bin/date -d yesterday +%Y-%m-%d-%H%M`
BAK_FILE1=$BAK_PATH/$BAK_TIME-$BASE_FILE_NAME1
BAK_FILE2=$BAK_PATH/$BAK_TIME-$BASE_FILE_NAME2
echo $BAK_FILE1
echo $BAK_FILE2
$BASE_DIR/sbin/nginx -s stop
mv $CURRENT_FILE1 $BAK_FILE1
mv $CURRENT_FILE2 $BAK_FILE2
$BASE_DIR/sbin/nginx
#redis日志备份脚本
#!/bin/sh
BASE_DIR=/usr/local/redis5
BASE_FILE_NAME1=redis.log
CURRENT_PATH=$BASE_DIR/logs
BAK_PATH=$BASE_DIR/backlogs
CURRENT_FILE1=$CURRENT_PATH/$BASE_FILE_NAME1
BAK_TIME=`/bin/date -d yesterday +%Y-%m-%d-%H%M`
BAK_FILE1=$BAK_PATH/$BAK_TIME-$BASE_FILE_NAME1
echo $BAK_FILE1
sp_pid=`ps -ef | grep redis-server | grep -v grep | awk '{print $2}'`
if [ -z "$sp_pid" ];
then
echo "[ not find sp-tomcat pid ]"
else
echo "find result: $sp_pid "
kill -9 $sp_pid
fi
mv $CURRENT_FILE1 $BAK_FILE1
$BASE_DIR/redis-server redis.conf
#清理日志脚本
d=`date +%Y-%m-%d`
d2=`date -d'100 day ago' +%Y-%m`
d3=`date -d'200 day ago' +%Y-%m`
rm -rf *${d2}*
rm -rf /lims/ws/logs/*${d2}*
cd /lims/lims-server
>nohup.out
cd /lims/ws
>nohup.out
cd /usr/local/nginx/backlogs
rm -rf *${d3}*
cd /usr/local/redis5/backlogs/
rm -rf *${d3}*
#根据日期删除N天前的日志文件
sh -x ./test.sh
d=`date +%Y-%m-%d`
d2=`date -d'2 day ago' +%Y-%m-%d`
rm -rf catalina.${d2}.log
#日志文本内容部分删除
d=`date +%d-%b-%Y`
d2=`date -d'2 day ago' +%d-%b-%Y`
sed '/'${d2}'/d' catalina.out -i
tomcat重启脚本及开机启动
tomcat重启脚本
#!/bin/bash
# 引用环境变量(否则可能无法争取执行tomcat命令)
source /etc/profile
echo "==================== $(date +'%Y-%m-%d %T') 重启tomcat服务开始 ====================" >> /kht/tomcat9/restart_tomcat_log.txt
# 关闭tomcat服务
/kht/tomcat9/bin/shutdown.sh >> /kht/tomcat9/restart_tomcat_log.txt
echo "-------------------------------------------------------------------" >> /kht/tomcat9/restart_tomcat_log.txt
# 清除tomcat日志,并暂停5秒
cd /kht/tomcat9/bin/
rm -rf ../logs/*
rm -rf ../work/*
ping 127.0.0.1 -c 5 >> /kht/tomcat9/restart_tomcat_log.txt
echo "-------------------------------------------------------------------" >> /kht/tomcat9/restart_tomcat_log.txt
# 启动tomcat服务
/kht/tomcat9/bin/startup.sh >> /kht/tomcat9/restart_tomcat_log.txt
echo "==================== $(date +'%Y-%m-%d %T') 重启tomcat服务结束 ====================" >> /kht/tomcat9/restart_tomcat_log.txt
echo "" >> /kht/tomcat9/restart_tomcat_log.txt
echo "" >> /kht/tomcat9/restart_tomcat_log.txt
echo "" >> /kht/tomcat9/restart_tomcat_log.txt
CentOS7配置Tomcat8开机自动启动
1.创建文件
vi /etc/systemd/system/tomcat.service
[Unit]
Description=Tomcat8540
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=oneshot
ExecStart=/usr/local/apache-tomcat-8.5.40/bin/startup.sh
ExecStop=/usr/local/apache-tomcat-8.5.40/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
2.进入目录:cd /etc/systemd/system/
3.设置开机启动
systemctl enable tomcat
4.查询当前tomcat的状态:
systemctl status tomcat
5.关闭tomcat:
systemctl stop tomcat
6.关闭开机自启:
systemctl disable tomcat