Category Archives: linux

logrotate – linux 系统集成的日志转储压缩工具

全局配置文件: /etc/logrotate.conf ,单独系统配置文件放置在/etc/logrotate.d/目录下

参数说明

参数            功能说明
compress            通过gzip 压缩转储以后的日志
nocompress          不需要压缩时,用这个参数

copytruncate        用于还在打开中的日志文件,把当前日志备份并截断;是先拷贝再清空
                   ,拷贝和清空之间有一个时间差,可能会丢失部分日志数据。
nocopytruncate      备份日志文件但是不截断

create mode owner group  转储文件,使用指定的文件模式创建新的日志文件。轮转时指定创
                          建新文件的属性,如create 0600 root root
nocreate            不建立新的日志文件

delaycompress       和 compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress     覆盖 delaycompress 选项,转储同时压缩

ifempty             即使是空文件也转储,这个是 logrotate 的缺省选项。
notifempty          如果日志文件为空时,不转储

errors address      转储时的错误信息发送到指定的Email 地址
mail address        转储的日志文件发送到指定的E-mail 地址
nomail              转储时不发送日志文件

olddir directory    转储后的日志文件放入指定的目录,必须和当前日志文件在同一文件系统
noolddir            转储后的日志文件和当前日志文件放在同一个目录下

prerotate/endscript 在logrotate转储之前需要执行的指令,例如修改文件的属性等动作;
                     这两个关键字必须单独成行;
postrotate/endscript 在logrotate转储之后需要执行的指令,例如重新启动 (kill -HUP) 
                     某个服务!必须独立成行;

daily              指定转储周期为每天
weekly             指定转储周期为每周
monthly            指定转储周期为每月

rotate count       指定日志文件删除之前转储的个数,0 指没有备份,5 指保留5个备份
tabootext [+] list 让logrotate不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, 
                   .rpmsave, v, 和 ~

size               size当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)
                   以及KB (sizek)或者MB (sizem).
missingok          如果日志丢失,不报错继续滚动下一个日志
sharedscripts      运行postrotate脚本,作用是在所有日志都轮转后统一执行一次脚本。
                   如果没有配置这个,那么每个日志轮转后都会执行一次脚本

dateext           使用当期日期作为命名格式
dateformat .%s    配合dateext使用,紧跟在下一行出现,定义文件切割后的文件名,必须配
                  合dateext使用,只支持 %Y %m %d %s 这四个参数 自己测试了%H也可以。
                  不过如果需要支持分的话logrotate需要升级到3.9版本 ,

测试配置结果:

#debug测试 -d, --debug   Don't do anything, just test (implies -v)
logrotate -d configfile

示例

1、列出多个文件,统一配置策略

#Please note:
#nocreate and copytruncate in configuration file are mutually exclusive and can't be configured at the same time,otherwise nocreate have no effect
#create and copytruncate in configuration file are mutually exclusive and can't be configured at the same time,otherwisecreate have no effect
/var/log/startup.log
/var/log/boot.log
/var/log/backup_conf.log
/var/log/cloud-init-output.log
/var/log/cloud-init.log
/var/log/daemon.log
/var/log/cron.log
/var/log/kern.log
/var/log/syslog
/var/log/unused.log
/var/log/sysmonitor.log
/var/log/tuned/*.log
/var/log/wtmp
/var/log/btmp
{
	missingok
	compress
	notifempty
	copytruncate
	rotate 10
	size +4000k
}

2、多个同目录下多个log 文件

/var/log/openvswitch/*.log {
	missingok
	notifempty
	size 2048k
	rotate 10
	copytruncate
}

3

# Logrotate file for psacct RPM

/var/account/pacct {
    compress
    delaycompress
    notifempty
    daily
    rotate 31
    create 0600 root root
    postrotate
       if /usr/bin/systemctl --quiet is-active psacct.service ; then
           /usr/sbin/accton /var/account/pacct | /usr/bin/grep -v "Turning on process accounting, file set to '/var/account/pacct'." | /usr/bin/cat
       fi
    endscript
}

4、mariadb

/var/log/mariadb/mariadb.log {
        create 600 mysql mysql
        notifempty
        daily
        rotate 3
        missingok
        compress
    postrotate
	# just if mysqld is really running
        if [ -e /run/mariadb/mariadb.pid ]
        then
           kill -1 $(</run/mariadb/mariadb.pid)
        fi
    endscript
}

JAVA安装

mkdir /usr/java/ && tar xf jdk-8u281-linux-x64.tar.gz -C /usr/java/
vim  /etc/profile

export JAVA_HOME=/usr/java/jdk1.8.0_281
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin
export PATH=${JAVA_HOME}/bin:$PATH

source /etc/profile
java -version

为wordpress 5.6安装PHP7.4

wordpress 部署需要环境如下:

  • php>=7.4
  • mysql >=5.6 or mariadb >=10.1
  • Apache or Nginx

centos 8.2 环境下官方yum源只能安装7.2版本php,而php 编译安装比较麻烦,我后面选择的remi的repo,用yum安装,记录如下:

dnf install http://rpms.remirepo.net/enterprise/remi-release-8.rpm
dnf module reset php
dnf module install php:remi-7.4
dnf install php-mysqli php-gd php-zip php-imagick

这样wordpress所需的php环境就部署完成了。

${@:2} 是什么意思

1.这是GNU Bash的官方文档:https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion

2. 其中第3.4节讲了Shell参数有两种取法:可定位参数(Positional Parameters)和特殊参数(Special Parameters)

Positional Parameters:The shell’s command-line arguments. Shell命令行入参
Special Parameters:Parameters denoted by special characters. 通过特殊字符表示的参数

3. 可定位参数(Positional Parameters)

A positional parameter is a parameter denoted by one or more digits, other than the single digit 0. Positional parameters are assigned from the shell’s arguments when it is invoked, and may be reassigned using thesetbuiltin command. Positional parameterNmay be referenced as${N}, or as$NwhenNconsists of a single digit. Positional parameters may not be assigned to with assignment statements. Thesetandshiftbuiltins are used to set and unset them (seeShell Builtin Commands). The positional parameters are temporarily replaced when a shell function is executed (seeShell Functions).

即可以通过非0的数字定位参数,0为命令本身,1,2,3,4,5,,,为命令后面的参数

4. 特殊参数(Special Parameters)

Shell对几个参数特殊对待,下列参数只能被引用,不能被赋值
*
  ($*)从1开始展开可定位参数, 当使用没有被双引号括起时,每个可定位参数展开一个独立的词。在被使用的时候,
  这些词可以被再次分割或路径展开。当有被双引号括起时,它将展开为一个词(也可以理解为合并为一个词),
  为"$1c$2C.."形式,c为IFS变量的首字母,如果IFS没有设置,由c为空格,如果IFS为null,则中间没有间隔
@
  ($@)从1开始展开可定位参数,在被使用的时候,默认展开为独立的多个词,如果没有被双引号括起来,这些词被
  用来做单词分割。当没有被做分割使用时,它展开(也理解为合并)为一个由空格拼接出来的一个词。当有被双引号
  括起且被单词分割时,每个参数展开为独立的词,即,"$@"等同于"$1" "$2" ...。
  (这里有一段话没太理解,和这个问题关系不大,先不翻译了)。
  如果没有任何可定位参数,则"$@"和$@展开为无,即它们被删除了。

5. 第3.5.3节讲了Shell参数展开

${parameter:offset}
${parameter:offset:length}

通过这种方式可以截取字符串或数组

If parameter is ‘@’, the result is length positional parameters beginning at offset. A negative offset is taken relative to one greater than the greatest positional parameter, so an offset of -1 evaluates to the last positional parameter. It is an expansion error if length evaluates to a number less than zero.

如果参数为@,则为截取可定位参数数组,从offset开始算,length为取的长度,如果不给,则取到最后。如果offset为负,则为从最后一位倒数,如offset为-1则为最后一个元素。如果offset和length计算得到的长度小于0则会报错

The following examples illustrate substring expansion using positional parameters:

后面为示例

$ set -- 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
$ echo ${@:7}
7 8 9 0 a b c d e f g h
$ echo ${@:7:0}

$ echo ${@:7:2}
7 8
$ echo ${@:7:-2}
bash: -2: substring expression < 0
$ echo ${@: -7:2}
b c
$ echo ${@:0}
./bash 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
$ echo ${@:0:2}
./bash 1
$ echo ${@: -7:0}

所以,echo "${@:2}" ,它的意思就是输出入参的第2个至最后一个参数,且以空格分隔,合并为一个单词(word)

转自知乎刘长远的答复:https://www.zhihu.com/question/368661192/answer/991165910

find 命令参数mtime atime ctime使用说明

参数说明

  • atime –access time 访问时间,可通过cat、 more修改
  • ctime –change status time 修改状态位时间。可通过访问权限chmod chown、重命名 remane等命令修改
  • mtime –modify data time 修改文件数据时间
#修改mtime:
touch -mt YYMMDDhhmm filename   /****时间格式为年月日时分
#修改atime: 
touch -at YYMMDDhhmm filename

结合find使用方法

#使用find命令查找最近24小时修改过的文件
find $HOME -mtime 0

man find 中关于在mtime后面的数字的说明:

Search for files in your home directory which have been modified in the last twenty-four hours. This command works this way because the time since each file was last modified is divided by 24 hours and any remainder is discarded. That means that to match -mtime 0, a file will have to have a modification in the past which is less than 24 hours ago.

Numeric arguments can be specified as
+n for greater than n,
-n for less than n,
n for exactly n.

重点在上面加黑中的内容:每满24小时计1,多余的会舍去

比如过去10个小时的改动,因为10小于24,系统自动记录为0,写成find $HOME -mtime 0 格式,就可查找出过去24小时内的改动,自然也包括10小时内的。也可以写成find $HOME -mtime -1

总结如下:
n=0 表示时间是0<=time<24小时
n=1 表示时间是24<=time<48 所以+1至少要48
n=2 表示时间是48<=time<72

…依此类推