分类 Linux 下的文章

thinkphp批量启动、停止命令行脚本

task.sh

#!/bin/bash

#---------------------------------------
# 批量启动、停止 task [考虑性能,此接口不再使用]
# author: SenSen 1050575278
# date: 2021/10/07
# 注意:所有脚本需测试后方可使用!!!
#---------------------------------------

#获取当前时间
curtime=`date +"%Y/%m/%d %H:%M:%S"`

#判断是否存在参数,不存在则取消
if [ "$1" == 'start' ] || [ "$1" == 'stop' ]; then
    #所在目录
    dir='/web/oa/'

    #自动获取系统目录文件 仅可包含oa系统文件
    sysDir=`ls /web/oa`

    #排除目录
    excluded=()

    for f in ${sysDir}
        do
            #排除指定目录
            if [[ ${excluded[@]/${f}/} != ${excluded[@]} ]]; then
                continue
            fi
            #进入目录
            cd ${dir}${f}
            #执行对应操作
            #/usr/bin/php think task $1
            sleep 1
            echo "${curtime} ${f} done"
        done

else
    echo "Error: param is empty"
fi

tar.xz文件解压

XZ压缩最新压缩率之王,不过xz也有一个坏处就是压缩时间比较长。
压缩文件:xz -z file
如果要保留被压缩的文件加上参数 -k ,如果要设置压缩率加入参数 -0 到 -9调节压缩率。如果不设置,默认压缩等级是6。
解压文件:xz -d[k] file.xz

解压tar.xz文件
xz -d xxx.tar.xzxxx.tar.xz 解压成 xxx.tar 然后,再用 tar xvf xxx.tar 来解包。

linux下批量更新升级多个系统文件

工作中需要部署上千套相同系统,为方便系统更新升级,除常规的系统内置升级程序外,必要的时候还需要手动批量覆盖(如系统存在严重bug时),shell脚本代码如下:

注:

  1. 支持排除指定目录升级(原则上建议将定制系统放于不同目录);
  2. 系统内置升级程序方便查看各系统使用情况(当前为登录系统后即可与中心服务器检测版本,如存在新版本则自动拉取升级);
  3. 如使用系统内置升级模式,则有重大bug时使用shell更新可能因各系统版本不一致导致系统异常;
  4. 建议仅使用一种升级模式;
#!/bin/bash

#---------------------------------------
# 批量更新多文件夹内容 支持排除指定目录
# 注:多系统升级时,为避免文件冲突,建议
# 单次单压缩包更新
# author: SenSen 1050575278
# date: 2021/09/10
#---------------------------------------

#添加定时任务 10min
# */10 * * * * /www/upgrade/upgrade.sh

#记录日志
log="/www/upgrade/upgrade.log"

#获取当前时间
curtime=`date +"%Y/%m/%d %H:%M:%S"`

#进入升级包所在目录 升级包必须包含version.txt文件
cd /www/upgrade/fix

#判断目录中是否有文件 原则上一次只能存在一个文件,避免多个升级包导致混乱
count=`ls -l|grep '^-'|wc -l`

#获取所有系统 废弃
# source ./wlt_dir.sh

#所在目录
dir='/diskb/www/'

#自动获取系统目录文件 仅可包含oa系统文件
sysDir=`ls /diskb/www`

#排除目录
excluded=()

#测试示例
#sysDir=('wlt_3163')

if [ $count -ne 0 ];then
    for i in `ls`
        do
            for f in ${sysDir}
                do
                    #排除指定目录
                    if [[ ${excluded[@]/${f}/} != ${excluded[@]} ]]; then
                        continue
                    fi
                    echo "系统:${f} 升级文件:${i}">>${log}
                    #解压到对应系统目录
                    unzip -oq /www/upgrade/fix/${i} -d ${dir}${f}
                    echo "${curtime} ${i} done">>${log}
                done
        #移动升级包
        mv /www/upgrade/fix/${i} /www/upgrade/bak;
        done
    echo "upgrade successfully">>$log
else
    #echo "${curtime} upgrade_fix is empty">>$log
fi

shell判断文件、文件夹是否存在

dir="/folder"
file="/test/test.txt"
file2="/test/test.php"

判断文件夹是否存在,不存在则创建
if [ ! -d "${dir}" ]; then
    mkdir -p ${dir}
fi

判断文件是否存在且可执行
if [ ! -x "${dir}"]; then
    mkdir "${dir}"
fi

判断文件是否存在
if [ ! -f "$file" ]; then
    touch "$file"
fi

判断变量是否有值
if [ ! -n "$file" ]; then
 echo "$file 变量为空!"
 exit 0
fi

判断两个变量的字符串内容是否相同
if [ "$file" = "$file2" ]; then
    echo "$file equal $file2"
else
    echo "$file not equal $file2"
fi

shell脚本批量检测mysql表中是否存在指定字段

批量查询数据库中表是否存在指定字段,批量添加表字段:

#!/bin/bash
#exit
db_con="/usr/local/mysql/bin/mysql -uroot -p123456"
arr=(db1 db2 db3)
for db in ${arr[*]}
do
    ${db_con} -e "select count(*) from information_schema.columns where table_schema = '${db}' and table_name = 'table_name' and column_name = 'column_name';"  >> /data/shell/test.txt
    echo ${db} >> /data/shell/test.txt 
    #${db_con} -e "ALTER TABLE ${db}.gy_com_customer ADD new_column varchar(100) NOT NULL default '';" 2>/dev/null
    echo ${db} " --- done"
done

Let’s Encrypt 证书自动更新

Let’s Encrypt 每次申请仅3个月有效期
更新命令:certbot renew

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
0 0 1 */2 * certbot renew --quiet --force-renewal

certbot安装
yum -y install yum-utils
yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional
执行certbot certonly测试安装是否正确

PS:我也是随便复制的,只是想到了Let’s Encrypt的自动更新便百度了一下 :)

lamp linux centos升级php5.4到5.6

升级编译参数

./configure --prefix=/usr/local/php56 \
--with-config-file-path=/usr/local/php56/etc \
--with-bz2 \
--with-curl \
--enable-ftp \
--enable-sockets \
--disable-ipv6 \
--with-gd \
--with-jpeg-dir=/usr/local \
--with-png-dir=/usr/local \
--with-freetype-dir=/usr/local \
--enable-gd-native-ttf \
--with-iconv-dir=/usr/local \
--enable-mbstring \
--enable-calendar \
--with-gettext \
--with-ldap \
--with-libxml-dir=/usr/local \
--with-zlib \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-mysql=mysqlnd \
--enable-dom \
--enable-xml \
--enable-fpm \
--enable-bcmath \
--enable-pcntl \
--with-mhash \
--enable-zip \
--with-openssl \
--enable-opcache \
--with-pear \
--with-apxs2=/usr/local/apache2/bin/apxs