centos安装redis及设置自启动
- centos安装redis及设置自启动
- redis安装脚本
- redis启动脚本
- chkconfig设置开机自启动
- systemctl设置开机自启动
- nginx脚本
- mysql脚本
- mongodb脚本
- rabbitmq脚本
- java服务脚本
- 删除日志
- 定时任务
centos安装redis及设置自启动
redis安装脚本
进入 /opt/redis/目录,创建安装脚本
vim /opt/redis/redis-install.sh
#!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop redis
# wget http://download.redis.io/releases/redis-5.0.4.tar.gz
# tar -xf redis-5.0.4.tar.gz
# cd redis-5.0.4
# make & make install
#
#配置redis的安装目录
currentTime=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "请输入安装目录,不存在脚本自动创建,例如 /opt/es"
read installpath
#创建安装的目录
if [ ! -d $installpath ]; then
mkdir -p $installpath
fi
if [ ! -d $installpath ]; then
echo "创建目录$installpath失败!请检查目录是否有权限"
exit
fi
#服务名称
SERVER_NAME=redis
#解压tar包
currentdir=$(cd $(dirname $0); pwd)
echo "当前目录:$currentdir"
ls | grep 'redis-.*[gz]$'
if [ $? -ne 0 ]; then
#当前目录没有压缩包
echo "在$currentdir下没有发现$SERVER_NAME-*.tar.gz,执行网络下载!"
echo "下载redis安装包..........."
wget https://download.redis.io/releases/redis-6.0.9.tar.gz
echo "下载redis安装包完成,目录:$installpath"
fi
#解压缩
ls | grep 'redis-.*[gz]$'
if [ $? -eq 0 ]; then
#解压
echo "解压缩进行中............."
tar -xf $currentdir/$(ls | grep 'redis-.*[gz]$') -C $installpath
echo "解压缩完成,文件目录:$installpath"
else
echo "请下载或者自行上传redis压缩包!"
exit
fi
#必须环境
yum list centos-release-scl
if [ $? -ne 0 ]; then
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
#scl enable devtoolset-9 bash
echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
source /etc/profile
fi
#安装目录
#install_home=`ls $installpath | grep 'redis-.*'`
install_home=`find $installpath -name 'redis-*' -type d | awk '{print $1}' | head -n 1`
echo "安装文件目录:$install_home"
#授权
chmod 777 -R $install_home
#编译运行
echo "编译运行........."
cd $install_home
make & make install
echo "编译完成........."
#数据及日志目录
data_path=$install_home/data
#if [ ! -d ${path} ];then
if [ -d ${data_path} ];then
echo dir ${data_path} exist!
else
mkdir $data_path
chmod 777 -R $data_path
fi
logs_path=$install_home/logs
#if [ ! -d ${path} ];then
if [ -d ${logs_path} ];then
echo dir ${logs_path} exist!
else
mkdir $logs_path
chmod 777 -R $logs_path
fi
#配置目录
conf_path=$install_home/redis.conf
echo "配置文件目录:$conf_path"
#运行服务
EXEC=$install_home/src/redis-server
PORT=6379
param=`fuser -n tcp $PORT`
if [ "$param"="0" ]; then
echo "$SERVER_NAME is running ............."
else
echo "starting $SERVER_NAME server..........."
$EXEC $conf_path
fi
redis启动脚本
进入 /opt/redis/redis-6.0.9目录,编写启动脚本
vim /opt/redis/redis-6.0.9/redis.sh
#!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop redis
# wget http://download.redis.io/releases/redis-6.0.9.tar.gz
# tar -xf redis-6.0.9.tar.gz
# cd redis-6.0.9
# make & make install
#
#找到本机安装redis后,存放redis命令的目录
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
#redis的默认端口, 要和下文中的redis.conf中一致
REDISPORT=6379
#redis服务端的命令
BASE_HOME=/opt/redis-6.0.9/
#redis服务端的命令
EXEC=$BASE_HOME/src/redis-server
#redis客户端的命令 这两个一般都在 PATH目录下
REDIS_CLI=$BASE_HOME/src/redis-cli
#reids的进程文件生成的位置
PIDFILE=/var/run/redis.pid
#redis的配置文件所在的目录
CONF=$BASE_HOME/redis.conf
#AUTH="1234" 这句没什么用可以不要
function start(){
echo "staring redis server..........."
ps -ef | grep redis-server | grep -v grep
if [ $? -eq 0 ];then
echo "redis server is running......."
else
echo "start redis server"
$EXEC $CONF >$BASE_HOME/logs/redis.log 2>&1 &
echo "staring redis server success !"
ps -ef | grep $EXEC | grep -v grep
fi
echo "staring redis server end "
}
function stop(){
echo "stop redis server ......."
param=`ps -ef | grep redis-server | grep -v grep | awk '{print $2}'`
if [ "$param" = "" ];then
echo "redis server not running"
# exit 1
fi
for item in ${param[*]}
do
echo "stop process num: $item"
sudo kill -9 $item
echo " stop redis server success"
done
echo "staring redis server end "
}
function status(){
echo "check redis status......."
param=`ps -ef | grep redis-server | grep -v grep | awk '{print $2}'`
if [ "$param" = "" ];then
echo "redis server not running"
exit 1
else
echo "redis server is running"
echo $param
fi
echo "check redis server end "
}
case "$1" in
start)
echo "staring redis server: ${0}"
stop
start
;;
stop)
stop
;;
status)
status
;;
restart)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac
启动:sh /opt/redis/redis-6.0.9/redis.sh start
停止:sh /opt/redis/redis-6.0.9/redis.sh stop
重启:sh /opt/redis/redis-6.0.9/redis.sh restart
查看:sh /opt/redis/redis-6.0.9/redis.sh status
chkconfig设置开机自启动
将以上脚本放入/etc/init.d/redis文件中即可。
添加开机启动服务:chkconfig --add redis
删除开机启动服务:chkconfig --del redis
设置开机启动:chkconfig redis on
关闭开机启动:chkconfig redis off
systemctl设置开机自启动
vim /etc/systemd/system/redis.service
#!/bin/bash
#导入路径
export REDIS_HOME=/opt/redis-5.0.4/
export PATH=$PATH:$REDIS_HOME
#服务的说明
[Unit]
#描述服务
Description=redis server
#描述服务类别
After=network.target remote-fs.target nss-lookup.target
#服务运行参数的设置
[Service]
Environment="REDIS_HOME=$REDIS_HOME"
#forking是后台运行的形式
Type=forking
#启动服务的具体运行命令
ExecStart=/opt/redis-5.0.4/src/redis-server /opt/redis-5.0.4/redis.conf >/opt/redis-5.0.4/logs/redis.log 2>&1 &
#重启命令
ExecReload=/bin/kill -s HUP $MAINPID
#停止命令
ExecStop=ps -ef | grep redis-server | grep -v grep | awk '{print $2}' | xargs kill -9
#服务分配独立的临时空间
PrivateTmp=true
#服务安装的相关设置,可设置为多用户
[install]
WantedBy=multi-user.target
设置开机自启动:systemctl enable redis
启动redis服务:systemctl start redis
停止redis服务:systemctl stop redis
查看redis服务:systemctl status redis
nginx脚本
vim nginx.sh
#!/bin/sh
#chkconfig: 2345 80 05
# 这里表示在linux运行环境2,3,4,5级别执行
#description: admin-server
#系统命令
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
#根目录
DIR=/opt/nginx-14.5
SERVER_NAME=nginx
NGINX_CONF=$DIR/conf/nginx.conf/nginx
NGINX_HOME=$DIR/$SERVER_NAME
if [ ! -f "$NGINX_HOME" ];then
echo "$NGINX_HOME is not exist!"
exit 1
fi
echo "nginx server file : $NGINX_HOME"
#日志目录
LOGS_DIR=/home/encrypt/logs
#if [ ! -d ${path} ];then
if [ -d ${LOGS_DIR} ];then
echo dir ${LOGS_DIR} exist!
else
sudo mkdir $LOGS_DIR
sudo chmod 777 -R $LOGS_DIR
fi
#log file
LOG_NAME=$LOGS_DIR/$SERVER_NAME.log
sudo echo "">$LOG_NAME
echo "log file : $LOG_NAME"
#授权
chmod 777 -R $DIR
echo "current dir : $DIR"
#启动
function start(){
if [ ! -f "$NGINX_HOME"];then
echo "$NGINX_HOME is not exist !"
exit 1
fi
echo "start server $NGINX_HOME ................."
stop
echo "">/$LOGS_DIR/nginx.pid
ps -ef | grep $NGINX_HOME | grep -v grep
if [ $? -eq 0 ];then
echo "$NGINX_HOME is running......."
echo "$NGINX_HOME -c $NGINX_CONF -s reload "
cd $DIR
$NGINX_HOME -c $NGINX_CONF -s reload
echo "$NGINX_HOME reload success !......."
else
cd $DIR
./nginx -c $NGINX_CONF
echo "starting $NGINX_HOME server success !......."
fi
echo "start server $JAR_HOME end!................."
}
#停止
function stop(){
echo "stop server $NGINX_HOME start................."
pid=`ps -ef | grep "$NGINX_HOME" | grep -v grep | awk '{print $2}'`
if [ "$pid" == "" ];then
echo "$NGINX_HOME is running......."
exit 1
fi
if [ "x${pid}" == "x"];then
kill -9 $pid
echo "$NGINX_HOME is stop......."
fi
# for item in ${pid[*]}
# do
# echo "stop process num: $item"
# kill -9 $item
# echo "$NGINX_HOME is stop success !......."
# done
#
# echo "stop server $JAR_HOME end!................."
}
#查看状态
function status(){
echo "status server $NGINX_HOME start................."
param=`ps -ef | grep "$NGINX_HOME" | grep -v grep`
if [ "$param" == "" ];then
echo "$NGINX_HOME is running......."
exit 1
else
echo "$NGINX_HOME is running......."
echo $param
fi
echo "status server $NGINX_HOME end!................."
}
#备份
function backup(){
echo "backup server $NGINX_HOME start................."
stop
d=`date '+%Y%m%d%H%M%S'`
echo "date : $d"
file=$DIR/html/dist
echo "bakc file : $file"
mv $file $file-$d
echo "bakc file : $file-$d"
echo "backup server $NGINX_HOME end!................."
}
#删除备份
function delete(){
echo "delete back file start................."
cd $DIR
year=`date '+%Y'`
back_file=$DIR/html/dist-$year*
echo "delete file : $back_file"
rm -rf $back_file
echo "delete back file end!................."
}
#同步时钟
function sync(){
echo "sync time start................."
cd $DIR
systemctl stop ntpd
/usr/sbin/ntpdate 8.8.8.8
echo "sync time end!................."
}
#同步ssh公钥到其他服务器
function syncssh(){
echo "sync ssh publickey start................."
arr=("192.168.3.101" "192.168.3.102" "192.168.3.103")
# su root
#ssh-keygen -t rsa
len=${#arr[@]}
for ((i=0;i<len;i++))
do
ip=${arr[i]}
echo "copy /root/.ssh/id_rsa.pub to ip server: $ip"
ssh-copy-id -p 22 -i /root/.ssh/id_rsa.pub root@$ip
echo "copy /root/.ssh/id_rsa.pub end................."
done
echo "sync ssh publickey end!................."
}
#拷贝部署公用包到其他服务器
function copy(){
echo "copy deploy package start................."
arr=("192.168.3.101" "192.168.3.102" "192.168.3.103")
# su root
#ssh-keygen -t rsa
len=${#arr[@]}
for ((i=0;i<len;i++))
do
ip=${arr[i]}
echo "copy /root/.ssh/id_rsa.pub to ip server: $ip"
scp -r /opt/redis/* root@$ip:/opt/redis/
scp -r /usr/openjdk/* root@$ip:/usr/openjdk/
echo "copy deploy package end................."
done
echo "copy deploy package end!................."
}
#启动服务,需要参数分别为ip、env环境
function exec_start(){
echo "publish the project start ............."
if [ -z "$1" ];then
echo "exec_start param ip is null, correct grammar: exec_start ip ............."
exit 1
fi
if [ -z "$2" ];then
echo "exec_start param ip is null, correct grammar: exec_start ip dev|test|prod ............."
exit 1
fi
#java project
#cd $DIR
#mvn clean package install -Dmaven.test.skip=true
#stop server and back file
ssh root@$1 'sh /opt/deploy/admin-server/user.sh stop'
ssh root@$1 'sh /opt/deploy/admin-server/user.sh backup'
#copy deploy file
scp -r /opt/deploy/* root@$1:/opt/deploy/
#start server
if [ "$2" == "dev" ];then
ssh root@$1 'sh /opt/deploy/admin-server/user.sh start dev'
elif [ "$2" == "test" ];then
ssh root@$1 'sh /opt/deploy/admin-server/user.sh start test'
elif [ "$2" == "prod" ];then
ssh root@$1 'sh /opt/deploy/admin-server/user.sh start prod'
else
echo "start param is error , correct grammar: sh xxx.sh start dev|test|prod"
fi
echo "root@$1 start success ............."
echo "publish the project end ............."
}
#前端服务,需要一个参数ip
function exec_start_front(){
echo "publish the project start ............."
if [ -z "$1" ];then
echo "exec_start param ip is null, correct grammar: exec_start ip ............."
exit 1
fi
#web project
#cd $DIR
#npm install && npm run build:prod
#stop server and back file
ssh root@$1 'sh /opt/deploy/nginx/nginx.sh stop'
ssh root@$1 'sh /opt/deploy/nginx/nginx.sh backup'
#copy deploy file
scp -r /opt/deploy/* root@$1:/opt/deploy/
#start server
ssh root@$1 'sh /opt/deploy/nginx/nginx.sh start'
echo "root@$1 start success ............."
echo "publish the project end ............."
}
function dev(){
#java server
exec_start 192.168.3.101 start dev
#nginx server
exec_start_front 192.168.3.101
}
function test(){
#java server
exec_start 192.168.3.102 start test
#nginx server
exec_start_front 192.168.3.102
}
function prod(){
#java server
exec_start 192.168.3.103 start prod
#nginx server
exec_start_front 192.168.3.103
}
case "$1" in
start)
${0} stop
start
;;
stop)
stop
;;
status)
status
;;
restart)
${0} stop
${0} start
;;
backup)
backup
;;
delete)
delete
;;
sync)
sync
;;
syncssh)
syncssh
;;
copy)
copy
;;
dev)
dev
;;
test)
test
;;
prod)
prod
;;
*)
echo "usage: /home/admin/admin-server.sh {start|stop|restart|status|backup|delete|sync|syncssh|copy|dev|test|prod}" >&2
exit 1
esac
通过该脚本执行启动前端、后端服务
部署开发环境:ssh nginx.sh dev
部署测试环境:ssh nginx.sh test
部署生成环境:ssh nginx.sh prod
nginx操作:sh nginx.sh start|stop|status|delete|backup|sync|copy
vim /etc/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
mysql脚本
vim nginx.sh
#!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop mongodb
#找到本机安装redis后,存放redis命令的目录
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
#mysql 的默认端口6379
PORT=6379
#mysql 服务端的命令
BASE_HOME=/etc/init.d/mysql
case "$1" in
start)
${0} stop
echo "start $BASE_HOME server "
sudo $BASE_HOME start
echo "staring $BASE_HOME success !"
ps -ef | grep nginx | grep -v grep
;;
stop)
echo "stop $BASE_HOME ......."
$BASE_HOME stop
;;
status)
$BASE_HOME status
;;
restart)
$BASE_HOME restart
;;
*)
echo "usage: /home/mysql-server.sh {start|stop|restart}" >&2
exit 1
esac
vim /etc/systemd/system/nginx.service
[Unit]
Description=mysql
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/etc/init.d/mysql start
#ExecReload=/etc/init.d/mysql restart
#ExecStop=/etc/init.d/mysql stop
#PrivateTmp=true
[Install]
WantedBy=multi-user.target
mongodb脚本
#!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop mongodb
#找到本机安装redis后,存放redis命令的目录
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
#mongodb 的默认端口, 要和下文中的redis.conf中一致
PORT=27017
#mongodb 服务端的命令
BASE_HOME=/usr/local/mongodb-linux-x86_64-rhel70-3.6.23
#数据存储目录
DATA_PATH=/home/daison/mongodata/data
#日志文件
LOG_PATH=/home/daison/mongodata/log/mongodb.log
case "$1" in
start)
${0} stop
echo "start mongodb"
echo "delete mongod.lock file"
sudo rm -rf $DATA_PATH/mongod.lock
sudo rm -rf $DATA_PATH/db/mongod.lock
sudo $BASE_HOME/bin/mongod --dbpath $DATA_PATH --repair
sudo $BASE_HOME/bin/mongod --config $BASE_HOME/bin/mongodb.conf
echo "staring mongodb success !"
ps -ef | grep mongodb | grep -v grep
;;
stop)
echo "stop mongodb ......."
sudo $BASE_HOME/bin/mongod --shutdown --config $BASE_HOME/bin/mongodb.conf
echo "stop mongodb succes !"
#param=`ps -ef | grep $SERVER_NAME | grep -v grep | awk '{print $2}'`
#if [ "$param" = "" ];then
# echo "servr $SERVER_NAME not running"
# exit 1
#fi
#
#if [ "$param" -gt 0 ];then
# kill -9 $param
# echo "stop $SERVER_NAME succes !"
#else
# echo "stop $SERVER_NAME fail!"
#fi
;;
status)
ps -ef | grep mongo | grep -v grep
;;
reload)
"$0" start
;;
*)
echo "usage: /home/mongodb-server.sh {start|stop|restart}" >&2
exit 1
esac
rabbitmq脚本
#!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop mongodb
#找到本机安装redis后,存放redis命令的目录
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
#mongodb 的默认端口, 要和下文中的redis.conf中一致
PORT=27017
#mongodb 服务端的命令
BASE_HOME=/usr/lib/rabbitmq
case "$1" in
start)
echo "start rabbitmq-server "
sudo systemctl start rabbitmq-server
echo "staring rabbitmq success !"
ps -ef | grep rabbitmq-server | grep -v grep
;;
stop)
echo "stop rabbitmq ......."
sudo systemctl stop rabbitmq-server
echo "stop rabbitmq success!"
#param=`ps -ef | grep $BASE_HOME | grep -v grep | awk '{print $2}'`
#if [ "$param" = "" ];then
# echo "servr $BASE_HOME not running"
# exit 1
#fi
#if [ "$param" -gt 0 ];then
# kill -9 $param
# echo "stop $BASE_HOME succes !"
#else
# echo "stop $BASE_HOME fail!"
#fi
;;
status)
sudo systemctl status rabbitmq-server
;;
restart)
systemctl restart rabbitmq-server
ps -ef | grep nginx | grep -v grep
;;
*)
echo "usage: /home/nginx-server.sh {start|stop|restart}" >&2
exit 1
esac
java服务脚本
vim deploy.sh
#!/bin/sh
#chkconfig: 2345 80 05
# 这里表示在linux运行环境2,3,4,5级别执行
#description: admin-server
#jdk
export JAVA_HOME=/usr/local/java/jdk1.8.0_192
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
source /etc/profile
#根目录
DIR=/home/encrypt/admin
#启动类
SERVER_NAME=admin-server
JAR_HOME=$DIR/$SERVER_NAME-1.0.1-SNAPSHOT.jar
if [ ! -f "$JAR_HOME" ];then
echo "$JAR_HOME is not exist!"
exit 1
fi
echo "start class jar dir: $JAR_HOME"
#日志目录
LOGS_DIR=/home/encrypt/logs
#if [ ! -d ${path} ];then
if [ -d ${LOGS_DIR} ];then
echo dir ${LOGS_DIR} exist!
else
sudo mkdir $LOGS_DIR
sudo chmod 777 -R $LOGS_DIR
fi
#log file
LOG_NAME=$LOGS_DIR/$SERVER_NAME.log
sudo echo "">$LOG_NAME
echo "log file : $LOG_NAME"
#授权
chmod 777 -R $DIR
echo "current dir : $DIR"
#监控服务目录
SKYWALKING_HOME=/home/skywalking/skywalking-6.5
echo "skywalking monitor server dir : $SKYWALKING_HOME"
#java agent jar
JAVA_AGENT=-javaagent:$SKYWALKING_HOME/agent/skywalking-agent.jar
echo "javaagent cmd : $JAVA_AGENT"
#service name
SERVICE_NAME=-Dskywalking.agent.service_name=$SERVER_NAME
echo "service name : $SERVICE_NAME"
#backend service
BACKEND_SERVICE=-Dskywalking.collector.backend_service=8.135.42.12:11800
echo "backend service : $BACKEND_SERVICE"
#启动服务
#nohup java -jar $JAR_HOME -javaagent:$SKYWALKING_HOME/agent/skywalking-agent.jar -Dskywalking.agent.service_name=websocket -Dskywalking.collector.backend_service=localhost:11800 > $DIR/logs/admin.log 2>&1 &
#nohup java -jar $JAR_HOME $JAVA_AGENT $SERVICE_NAME -Dskywalking.collector.backend_service=localhost:11800 > $LOG_NAME 2>&1 &
#nohup java $JAVA_AGENT $SERVICE_NAME $BACKEND_SERVICE -jar $JAR_HOME > $LOG_NAME 2>&1 &
#启动环境变量
ENV=$2
#启动
function start(){
if [ ! -f "$JAR_HOME"];then
echo "$JAR_HOME is not exist !"
exit 1
fi
if [ -z "$ENV" ];then
echo "start params is null, corrent grammar: sh xxx.sh start dev|test|prod !"
exit 1
fi
echo "start server $JAR_HOME ................."
ps -ef | grep $SERVER_NAME | grep -v grep
if [ $? -eq 0 ];then
echo "$JAR_HOME is running......."
else
echo "start server $SERVER_NAME"
#nohup java $JAVA_AGENT $SERVICE_NAME $BACKEND_SERVICE -jar -Xms512m -Xmx1024m $JAR_HOME > $LOG_NAME 2>&1 &
#$DIR/xjar java -server -Xms512m -Xmx1024m -jar $JAR_HOME > $LOG_NAME 2>&1 &
nohup java -jar -Xms512m -Xmx1024m $JAR_HOME --spring.profiles.active=$ENV > $LOG_NAME 2>&1 &
echo "start log file : $LOG_NAME"
echo "staring server success !"
ps -ef | grep $SERVER_NAME | grep -v grep
fi
echo "start server $JAR_HOME end!................."
}
#停止
function stop(){
echo "stop server $JAR_HOME start................."
pid=`ps -ef | grep "$SERVER_NAME" | grep -v grep | awk '{print $2}'`
if [ "$pid" == "" ];then
echo "$JAR_HOME is running......."
exit 1
fi
if [ "x${pid}" == "x"];then
kill -9 $pid
echo "$JAR_HOME is stop......."
fi
echo "stop server $JAR_HOME end!................."
}
#查看状态
function status(){
echo "status server $JAR_HOME start................."
param=`ps -ef | grep "$SERVER_NAME" | grep -v grep`
if [ "$param" == "" ];then
echo "$JAR_HOME is running......."
exit 1
else
echo "$JAR_HOME is running......."
echo $param
fi
echo "status server $JAR_HOME end!................."
}
#备份
function backup(){
echo "backup server $JAR_HOME start................."
stop
d=`date '+%Y%m%d%H%M%S'`
echo "date : $d"
mv $JAR_HOME $JAR_HOME-$d
echo "backup file: $JAR_HOME-$d"
echo "backup server $JAR_HOME end!................."
}
#删除备份
function delete(){
echo "delete back file start................."
cd $DIR
year=`date '+%Y'`
back_file=$SERVER_NAME-$year*
echo "delete file : $back_file"
rm -rf $back_file
echo "delete back file end!................."
}
#同步时钟
function sync(){
echo "sync time start................."
cd $DIR
systemctl stop ntpd
/usr/sbin/ntpdate 8.8.8.8
echo "sync time end!................."
}
#同步ssh公钥到其他服务器
function syncssh(){
echo "sync ssh publickey start................."
arr=("192.168.3.101" "192.168.3.102" "192.168.3.103")
# su root
#ssh-keygen -t rsa
len=${#arr[@]}
for ((i=0;i<len;i++))
do
ip=${arr[i]}
echo "copy /root/.ssh/id_rsa.pub to ip server: $ip"
ssh-copy-id -p 22 -i /root/.ssh/id_rsa.pub root@$ip
echo "copy /root/.ssh/id_rsa.pub end................."
done
echo "sync ssh publickey end!................."
}
#拷贝部署公用包到其他服务器
function copy(){
echo "copy deploy package start................."
arr=("192.168.3.101" "192.168.3.102" "192.168.3.103")
# su root
#ssh-keygen -t rsa
len=${#arr[@]}
for ((i=0;i<len;i++))
do
ip=${arr[i]}
echo "copy /root/.ssh/id_rsa.pub to ip server: $ip"
scp -r /opt/redis/* root@$ip:/opt/redis/
scp -r /usr/openjdk/* root@$ip:/usr/openjdk/
echo "copy deploy package end................."
done
echo "copy deploy package end!................."
}
case "$1" in
start)
${0} stop
start
;;
stop)
stop
;;
status)
status
;;
restart)
${0} stop
${0} start
;;
backup)
backup
;;
delete)
delete
;;
sync)
sync
;;
syncssh)
syncssh
;;
copy)
copy
;;
*)
echo "usage: /home/admin/admin-server.sh {start|stop|restart|status|backup|delete|sync|syncssh|copy}" >&2
exit 1
esac
启动:sh deploy.sh start
停止:sh deploy.sh stop
查看服务状态:sh deploy.sh status
删除日志
vim delete-log.sh
#!/bin/sh
#chkconfig: 2345 80 05
#description: admin-server
#jdk
export JAVA_HOME=/usr/local/jdk1.8.0_192
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
source /etc/profile
#根目录
DIR=/home/sysop
#日志目录
LOGS_DIR=$DIR/logs
#if [ ! -d ${path} ];then
if [ -d ${LOGS_DIR} ];then
echo dir ${LOGS_DIR} exist!
else
sudo mkdir $LOGS_DIR
sudo chmod 777 -R $LOGS_DIR
fi
#log file
LOG_NAME=$LOGS_DIR/$SERVER_NAME.log
echo "log file : $LOG_NAME"
#delete log file
function delete_log_zip(){
echo "删除 $LOGS_DIR 目录下的所有zip日志文件开始"
sudo rm -rf $LOGS_DIR/admin-*.zip
echo "删除 $LOGS_DIR 目录下的所有zip日志文件完成"
}
#clean log file
function clean_log(){
echo "清空 $LOGS_DIR 目录下的日志文件内容开始"
sudo echo "">$LOGS_DIR/admin.log
echo "清空 $LOGS_DIR 目录下的日志文件内容完成"
}
#delete temp file
function delete_tmp(){
echo "删除 $LOGS_DIR 目录下的所有tmp临时日志文件开始"
sudo rm -rf $LOGS_DIR/admin-*.tmp
echo "删除 $LOGS_DIR 目录下的所有tmp临时日志文件完成"
}
#delete log file
function delete_log_dir(){
echo "删除 $LOGS_DIR 目录下的所有日志文件及目录开始"
sudo rm -rf $LOGS_DIR/*
echo "删除 $LOGS_DIR 目录下的所有日志文件及目录完成"
}
#检查服务开启
t=$(date "+%Y-%m-%d %H:%M:%S")
echo "delete log server start ............"
echo "time: $t"
while true
do
cat <<EOF
****************************************
the flowing is optional
*****************************************
1) delete_log_zip
2) delete_log_dir
3) clean_log
4) delete_tmp
5)
99) 全部服务(1-10)
****************************************
EOF
read -p "please enter your chioce:" option
case $option in
1)
delete_log_zip
;;
2)
delete_log_dir
;;
3)
clean_log
;;
4)
delete_tmp
;;
5)
;;
99)
;;
*)
echo "option is inviald."
echo "usage: /home/script/delete-log.sh {1-99}" >&2
exit 1
esac
done
定时任务
vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
#minute|hour|day-of-month|month|day-of-week|full-path-to-shell-script
# minute: 区间为 0 – 59
# hour: 区间为0 – 23
# day-of-month: 区间为0 – 31
# month: 区间为1 – 12. 1 是1月. 12是12月.
# Day-of-week: 区间为0 – 7. 周日可以是0或7.
# full-path-to-shell-script: 脚本全路径
#每小时执行一次服务检查命令
* */1 * * * /home/daison/admin/script/check-server.sh 98