one-click-installation-script/resize_journal.sh

84 lines
2.9 KiB
Bash
Raw Normal View History

2022-12-17 23:05:21 +08:00
#!/bin/bash
2022-12-17 23:10:25 +08:00
head() {
# 支持系统Ubuntu 12+Debian 6+
ver="2022.12.17"
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
read -p "Do you want to proceed with changing? [y/n] " -n 1 confirm
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-17 23:05:21 +08:00
2022-12-17 23:10:25 +08:00
main() {
# Prompt the user for the desired size of the journal directory in MB
read -p "Enter the desired size of the journal directory in MB: " JOURNAL_SIZE_MB
2022-12-17 23:05:21 +08:00
2022-12-17 23:10:25 +08:00
# Convert the size from MB to bytes
JOURNAL_SIZE=$((JOURNAL_SIZE_MB * 1024 * 1024))
2022-12-17 23:05:21 +08:00
2022-12-17 23:18:59 +08:00
# Set the path to the journal directory
JOURNAL_DIR="/var/log/journal"
2022-12-17 23:10:25 +08:00
# Set the name of the log recording service
LOG_SERVICE="systemd-journald"
2022-12-17 23:05:21 +08:00
2022-12-17 23:18:59 +08:00
# Try setting the size of the journal directory with systemd-journal-size
if command -v systemd-journal-size &> /dev/null; then
systemd-journal-size --disk-space=$JOURNAL_SIZE
2022-12-17 23:31:20 +08:00
if [ $? -ne 0 ]; then
echo "Failed to set journal size using systemd-journal-size"
else
success=true
fi
fi
2022-12-17 23:18:59 +08:00
2022-12-17 23:31:20 +08:00
# If the previous method failed, try setting the size with journalctl
if ! $success && command -v journalctl &> /dev/null; then
2022-12-17 23:18:59 +08:00
journalctl --disk-space=$JOURNAL_SIZE
2022-12-17 23:31:20 +08:00
if [ $? -ne 0 ]; then
echo "Failed to set journal size using journalctl"
else
success=true
fi
fi
2022-12-17 23:18:59 +08:00
2022-12-17 23:31:20 +08:00
# If the previous methods failed, try setting the size in journald.conf
if ! $success && [ -f /etc/systemd/journald.conf ]; then
2022-12-17 23:18:59 +08:00
sed -i "s/^SystemMaxUse=.*/SystemMaxUse=$JOURNAL_SIZE/g" /etc/systemd/journald.conf
2022-12-17 23:31:20 +08:00
if [ $? -ne 0 ]; then
echo "Failed to set journal size using journald.conf"
else
success=true
fi
2022-12-17 23:18:59 +08:00
fi
2022-12-17 23:10:25 +08:00
2022-12-17 23:31:20 +08:00
2022-12-17 23:13:12 +08:00
# Restart the log recording service to force log rotation
systemctl restart $LOG_SERVICE
# Print the size of the journal directory
2022-12-17 23:18:59 +08:00
du -sh $JOURNAL_DIR
2022-12-17 23:31:20 +08:00
2022-12-17 23:10:25 +08:00
}
head
main