
linux设置定时执行压缩日志脚本和shell脚本自动输入scp密码
首先查看本机上是否已经安装了crontabs。log.sh 会在每月1号的0点0分执行。输入crontab -e进行定时任务编辑。修改后记得重启crond使改动生效。我的脚本位置在 /home/sh。如果已经安装了,那么会显示。如果没安装的话需要手动安装。
·
目录
一.linux设置定时执行脚本文件
1.安装crontabs并启动
首先查看本机上是否已经安装了crontabs
service crond status
如果已经安装了,那么会显示
如果没安装的话需要手动安装
yum install vixie-cron crontabs
yum install crontabs
#启动crontabs
service crond start
#显示状态,如果安装成功就会显示上图
service crond status
#开机自启动,这个根据自身需求决定是否使用
chkconfig crond on
2.编辑脚本
我的脚本位置在 /home/sh
vim log.sh
#!/bin/bash
#前一天的日期
TIME=`date -d yesterday +%Y-%m-%d`
#压缩日志的目录
baseFolder="/home/apache-tomcat-8.5.23/logs"
#临时目录
file="/home/apache-tomcat-8.5.23/logs/file"
#压缩后的新目录
log="/home/apache-tomcat-8.5.23/logs/log"
mkdir ${file}
mkdir ${log}
find ${baseFolder} -name '*.log' -exec mv {} ${file}/ \;
find ${baseFolder} -name '*.txt' -exec mv {} ${file}/ \;
cd ${baseFolder}
tar -zcf ${TIME}.tar.gz ${file}
find ${baseFolder} -name '*.tar.gz' -exec mv {} ${log}/ \;
rm -rf ${file}
3.设置定时
输入crontab -e进行定时任务编辑
crontab -e
log.sh 会在每月1号的0点0分执行
修改后记得重启crond使改动生效
service crond restart
查看脚本的执行情况
tail -f /var/log/cron
二.shell脚本自动输入scp密码
1.查看是否安装expect
本文实现方法是通过Linux中的expect实现,具体实现方法如下:
首先查看系统是否安装了expect,执行命令如下:
[root@iZuf6hxps1f8adqu07rbinZ ~]# whereis expect
expect:[root@iZuf6hxps1f8adqu07rbinZ ~]#
出现如上提示则表示未安装,然后执行如下命令安装expect:
[root@iZuf6hxps1f8adqu07rbinZ ~]# yum install -y expect
然后获取安装后的expect目录,执行命令如下:
[root@iZuf6hxps1f8adqu07rbinZ ~]# whereis expect
expect: /usr/bin/expect /usr/share/man/man1/expect.1.gz
2.编辑脚本
vim scp.log
#!/usr/bin/expect -f
# 要拷贝的目录
log="/home/apache-tomcat-8.5.23/logs/logMagellan"
#密码
des_pass=密码
expect -c "
spawn scp -r ${log} root@192.168.2.15:/opt/ossutil
expect \"password:\"
send \"${des_pass}\r\"
expect eof
"
rm -rf ${log}
3.执行脚本
sh scp.log
点击阅读全文
更多推荐
目录
所有评论(0)