2022-12-17 23:05:21 +08:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
2022-12-17 23:10:25 +08:00
|
|
|
|
head() {
|
|
|
|
|
# 支持系统:Ubuntu 12+,Debian 6+
|
2022-12-18 09:01:52 +08:00
|
|
|
|
ver="2022.12.18"
|
2022-12-17 23:10:25 +08:00
|
|
|
|
changeLog="一键修改journal日志记录大小,释放系统盘空间"
|
|
|
|
|
clear
|
|
|
|
|
echo "#######################################################################"
|
2022-12-17 23:13:12 +08:00
|
|
|
|
echo "# ${YELLOW}一键修改journal大小脚本${PLAIN} #"
|
2022-12-17 23:10:25 +08:00
|
|
|
|
echo "# 版本:$ver #"
|
2022-12-17 23:13:12 +08:00
|
|
|
|
echo "# 更新日志:$changeLog #"
|
2022-12-17 23:10:25 +08:00
|
|
|
|
echo "# ${GREEN}作者${PLAIN}: spiritlhl #"
|
|
|
|
|
echo "# ${GREEN}作仓库${PLAIN}: https://github.com/spiritLHLS/one-click-installation-script #"
|
|
|
|
|
echo "#######################################################################"
|
|
|
|
|
echo "支持系统:Ubuntu 12+,Debian 6+"
|
|
|
|
|
echo "自定义修改大小,单位为MB,一般500M或者1G即可,有的系统日志默认给了5G甚至更多,不是做站啥的没必要"
|
2022-12-17 23:13:53 +08:00
|
|
|
|
echo "请注意,修改journal目录大小可能会影响系统日志的记录。因此,在修改 journal 目录大小之前,建议先备份系统日志到本地"
|
2022-12-17 23:10:25 +08:00
|
|
|
|
# Display prompt asking whether to proceed with changing
|
2022-12-18 11:29:55 +08:00
|
|
|
|
reading "Do you want to proceed with changing? [y/n] " -n 1 confirm
|
2022-12-17 23:10:25 +08:00
|
|
|
|
echo ""
|
2022-12-17 23:05:21 +08:00
|
|
|
|
|
2022-12-17 23:10:25 +08:00
|
|
|
|
# Check user's input and exit if they do not want to proceed
|
|
|
|
|
if [ "$confirm" != "y" ]; then
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
2022-12-18 11:29:55 +08:00
|
|
|
|
|
|
|
|
|
reading "Enter the desired day of the journal retention days(eg: 7): " retention_days
|
|
|
|
|
reading "Enter the desired size of the journal directory in MB (eg: 500): " size
|
2022-12-17 23:10:25 +08:00
|
|
|
|
}
|
2022-12-17 23:05:21 +08:00
|
|
|
|
|
2022-12-18 11:32:15 +08:00
|
|
|
|
red(){ echo -e "\033[31m\033[01m$1$2\033[0m"; }
|
|
|
|
|
green(){ echo -e "\033[32m\033[01m$1$2\033[0m"; }
|
|
|
|
|
yellow(){ echo -e "\033[33m\033[01m$1$2\033[0m"; }
|
2022-12-18 11:29:55 +08:00
|
|
|
|
reading(){ read -rp "$(green "$1")" "$2"; }
|
|
|
|
|
|
2022-12-17 23:10:25 +08:00
|
|
|
|
main() {
|
2022-12-18 11:04:03 +08:00
|
|
|
|
|
|
|
|
|
# Set default value for size
|
|
|
|
|
size="$size"M
|
|
|
|
|
|
2022-12-18 11:15:58 +08:00
|
|
|
|
sed -i "s/^\(#\)\{0,1\}SystemMaxUse=.*/SystemMaxUse=$size/" /etc/systemd/journald.conf
|
2022-12-17 23:52:30 +08:00
|
|
|
|
|
2022-12-18 11:04:03 +08:00
|
|
|
|
# Restart journald service
|
|
|
|
|
systemctl restart systemd-journald
|
2022-12-17 23:10:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-18 11:29:55 +08:00
|
|
|
|
|
2022-12-18 11:04:03 +08:00
|
|
|
|
level() {
|
|
|
|
|
# Set default values for variables
|
|
|
|
|
log_level=warning
|
2022-12-18 11:29:55 +08:00
|
|
|
|
journald_log_dir="/var/log/journal"
|
2022-12-18 11:09:27 +08:00
|
|
|
|
|
|
|
|
|
# Check if log directory exists
|
2022-12-18 11:29:55 +08:00
|
|
|
|
if [ ! -d "$journald_log_dir" ]; then
|
2022-12-18 11:09:27 +08:00
|
|
|
|
echo "Log directory not found" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2022-12-18 11:04:03 +08:00
|
|
|
|
|
|
|
|
|
# Set log retention period
|
2022-12-18 11:29:55 +08:00
|
|
|
|
find "$journald_log_dir" -mtime +$retention_days -exec rm {} \;
|
2022-12-18 11:09:27 +08:00
|
|
|
|
|
|
|
|
|
# Check if config file exists
|
|
|
|
|
if [ ! -f /etc/rsyslog.conf ]; then
|
|
|
|
|
echo "Config file not found" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2022-12-18 11:04:03 +08:00
|
|
|
|
|
|
|
|
|
# Set log level
|
2022-12-18 11:29:55 +08:00
|
|
|
|
if grep -q "loglevel" /etc/rsyslog.conf; then # Add this line
|
|
|
|
|
sed -i "s/^\(#\)\{0,1\}loglevel = .*/loglevel = $log_level/" /etc/rsyslog.conf
|
|
|
|
|
else # Add this block
|
|
|
|
|
echo "loglevel = $log_level" >> /etc/rsyslog.conf
|
|
|
|
|
fi
|
|
|
|
|
}
|
2022-12-18 11:09:27 +08:00
|
|
|
|
|
2022-12-18 11:29:55 +08:00
|
|
|
|
check_again() {
|
|
|
|
|
# Loop for 5 seconds, printing journald disk usage every second
|
|
|
|
|
count=0
|
|
|
|
|
while [ $count -lt 5 ]; do
|
|
|
|
|
journalctl --disk-usage
|
|
|
|
|
count=$((count+1))
|
|
|
|
|
sleep 1
|
|
|
|
|
done
|
2022-12-18 11:04:03 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-17 23:10:25 +08:00
|
|
|
|
|
|
|
|
|
head
|
|
|
|
|
main
|
2022-12-18 11:04:03 +08:00
|
|
|
|
level
|
2022-12-18 11:29:55 +08:00
|
|
|
|
check_again
|