我的應用程式日志檔案不斷旋轉,它的模式如下 postgresql-yyyy-mm-dd_hhmmss.log
-rw-------. 1 root root 331 Jun 1 22:04 postgresql-2022-06-01_220333.log.gz
-rw-------. 1 root root 417 Jun 1 22:04 postgresql-2022-06-01_220430.log.gz
-rw-------. 1 root root 289 Jun 2 19:28 postgresql-2022-06-02_000000.log.gz
-rw-------. 1 root root 26802 Jun 2 19:50 postgresql-2022-06-02_192809.log.gz
-rw-------. 1 root root 8440 Jun 2 23:57 postgresql-2022-06-02_195044.log.gz
-rw-------. 1 root root 15016 Jun 3 11:22 postgresql-2022-06-03_000000.log.gz
-rw-------. 1 root root 291 Jun 3 11:24 postgresql-2022-06-03_112405.log.gz
-rw-------. 1 root root 336 Jun 3 11:25 postgresql-2022-06-03_112553.log.gz
-rw-------. 1 root root 397 Jun 3 11:27 postgresql-2022-06-03_112714.log.gz
-rw-------. 1 root root 358 Jun 3 11:29 postgresql-2022-06-03_112901.log.gz
-rw-------. 1 root root 493 Jun 3 11:30 postgresql-2022-06-03_113031.log.gz
-rw-------. 1 root root 418 Jun 3 11:34 postgresql-2022-06-03_113354.log.gz
-rw-------. 1 root root 419 Jun 3 11:39 postgresql-2022-06-03_113920.log.gz
-rw-------. 1 root root 416 Jun 3 11:44 postgresql-2022-06-03_114437.log.gz
-rw-------. 1 root root 417 Jun 3 11:49 postgresql-2022-06-03_114943.log.gz
-rw-------. 1 root root 419 Jun 3 11:55 postgresql-2022-06-03_115530.log.gz
-rw-------. 1 root root 16961 Jun 3 23:56 postgresql-2022-06-03_120047.log.gz
-rw-------. 1 root root 35470 Jun 4 23:56 postgresql-2022-06-04_000000.log.gz
-rw-------. 1 root root 406059 Jun 5 17:56 postgresql-2022-06-05_000000.log
我想運行一個小腳本(unix/linux),以便它在這個檔案夾中創建一個符號鏈接,它總是自動指向最新的(當前)日志檔案,例如
postgres.LOG -> postgresql-2022-06-05_000000.log
這樣我就不必在打開之前 ls 并找出當前的日志檔案是什么,而只需打開 postgres.LOG。
uj5u.com熱心網友回復:
所有旋轉的日志檔案似乎都是 gzip 壓縮的,因此 globpostgresql-*.log將擴展到感興趣的日志檔案:
ln -sf postgresql-*.log postgres.LOG
uj5u.com熱心網友回復:
如果您希望在每次創建新日志檔案時自動更改符號鏈接,您需要inotify。
像這樣的東西
#! /bin/bash
log_dir=/your/log/dir
inotifywait -m -e create "$log_dir" | while read -r _dir event file
do
if [[ ! -h "$log_dir/$file" && "$file" =~ \.log$ ]]
then
ln -sf "$log_dir/$file" "$log_dir/postgres.LOG"
fi
done
uj5u.com熱心網友回復:
檔案名的格式確保最新的檔案使用ls. 所以這應該作業:
ln -sf $(ls postgresql-*.log | tail -1) postgres.LOG
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/487307.html
標籤:PostgreSQL 重击 日志记录 符号链接
上一篇:在Linux(bash)中使用SED在匹配前后突出顯示匹配的模式和更多字符
下一篇:使用sed進行欄位排序
