2023元旦第3天,没虚渡过。
一,练习
[root@127 shell_demo]# dd if=/dev/urandom of=./test.txt bs=1M count=50
50+0 records in
50+0 records out
52428800 bytes (52 MB) copied, 1.10659 s, 47.4 MB/s
[root@127 shell_demo]# ls -lh test.txt
-rw-r--r-- 1 root root 50M Jan 1 14:37 test.txt
[root@127 shell_demo]# split -b 10M test.txt test_
[root@127 shell_demo]# ll test*
-rw-r--r-- 1 root root 10485760 Jan 1 14:37 test_aa
-rw-r--r-- 1 root root 10485760 Jan 1 14:37 test_ab
-rw-r--r-- 1 root root 10485760 Jan 1 14:37 test_ac
-rw-r--r-- 1 root root 10485760 Jan 1 14:37 test_ad
-rw-r--r-- 1 root root 10485760 Jan 1 14:37 test_ae
-rw-r--r-- 1 root root 52428800 Jan 1 14:37 test.txt
test:
total 0
[root@127 shell_demo]# split -l 1000 test.txt testline_
[root@127 shell_demo]# ls -lh test*
-rw-r--r-- 1 root root 10M Jan 1 14:37 test_aa
-rw-r--r-- 1 root root 10M Jan 1 14:37 test_ab
-rw-r--r-- 1 root root 10M Jan 1 14:37 test_ac
-rw-r--r-- 1 root root 10M Jan 1 14:37 test_ad
-rw-r--r-- 1 root root 10M Jan 1 14:37 test_ae
-rw-r--r-- 1 root root 244K Jan 1 14:38 testline_aa
-rw-r--r-- 1 root root 249K Jan 1 14:38 testline_ab
-rw-r--r-- 1 root root 239K Jan 1 14:38 testline_ac
-rw-r--r-- 1 root root 261K Jan 1 14:38 testline_ad
-rw-r--r-- 1 root root 242K Jan 1 14:38 testline_ae
-rw-r--r-- 1 root root 244K Jan 1 14:38 testline_af
......
-rw-r--r-- 1 root root 265K Jan 1 14:38 testline_hv
-rw-r--r-- 1 root root 120K Jan 1 14:38 testline_hw
-rw-r--r-- 1 root root 50M Jan 1 14:37 test.txt
test:
total 0
[root@127 shell_demo]# wc -l testline_hs
1000 testline_hs
[root@127 shell_demo]# wc -l testline_hw
494 testline_hw
[root@127 shell_demo]# echo {1..5} > test.txt
[root@127 shell_demo]# echo {a..f} >> test.txt
[root@127 shell_demo]# echo {10..20} >> test.txt
[root@127 shell_demo]# echo {h..z} >> test.txt
[root@127 shell_demo]# cat test.txt
1 2 3 4 5
a b c d e f
10 11 12 13 14 15 16 17 18 19 20
h i j k l m n o p q r s t u v w x y z
[root@127 shell_demo]# split -b 4 test.txt multi_
[root@127 shell_demo]# ls -lh multi_*
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_aa
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_ab
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_ac
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_ad
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_ae
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_af
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_ag
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_ah
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_ai
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_aj
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_ak
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_al
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_am
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_an
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_ao
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_ap
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_aq
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_ar
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_as
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_at
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_au
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_av
-rw-r--r-- 1 root root 4 Jan 1 14:39 multi_aw
-rw-r--r-- 1 root root 1 Jan 1 14:39 multi_ax
-rw-r--r-- 1 root root 1.4K Dec 28 04:40 multi_procs.sh
-rw-r--r-- 1 root root 133 Dec 27 17:19 multi_table.sh
[root@127 shell_demo]# cat multi_ab
3 4 [root@127 shell_demo]# cat multi_ac
5
a [root@127 shell_demo]#
[root@127 shell_demo]#
[root@127 shell_demo]# vim logbak.sh
[root@127 shell_demo]#
二,介绍
image.png三,代码
cat logbak.sh
#!/bin/bash
# 功能描述:使用脚本结合计划任务,定期对NGINX日志进行切割
# 脚本是以天为单位进行日志分割的,如果需要以小时为单位,则需要更精确的时间标签
datetime=$(date +%Y%m%d)
# 假设日志目录为源码安装的标准目录,如果不是该目录,则需要根据实际情况修改
logpath=/usr/local/nginx/logs
mv $logpath/access.log $logpath/access-$datetime.log
mv $logpath/error.log $logpath/error-$datetime.log
# 脚本会读取标准日志目录下的nginx.pid文件,该文件保存有NGINX进程的进程号
# 如果该进程文件在其它目录下,则需要根据实际情况进行适当修改
kill -USR1 $(cat $logpath/nginx.pid)
# 附一个crontab的记录
# crontab -e
# 00 03 * * 5 /usr/local/nginx/logbak.sh
网友评论