clear-journal-log/clear-journal-log
2024-10-24 12:37:34 +02:00

61 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
######################################################################
# First Created: 23112022 Author: Allan Desc: Clears the journal log #
######################################################################
#
# Are we root
#
if [[ $(id -u) -ne 0 ]]; then printf "\nMust be root or use sudo!\n\n"; exit; fi
#
# Are we in the right directory
#
scriptdir="clear-journal-log" && whereami=$(pwd |awk -F'/' '{print $NF}')
if [ $whereami != $scriptdir ]; then printf "\nWrong directory! Script must be run from $scriptdir\n\n"; exit 1; fi
#
# Define variables and functions
#
cronjob01="0 2 * * 7 root /usr/local/bin/clear-journal-log >/dev/null 2>&1"
#
# Clear journal log script
#
script="
#!/bin/bash
######################################################################
# First Created: 23112022 Author: Allan Desc: Clears the journal log #
######################################################################
#
# Define variables and functions
#
logfile=\"/var/log/cleanupjournal.log\"
line () { for i in {1..100}; do echo -n \"\$1\"; done && printf \"\\n\"; }
datenow=\$(date +\"%d-%m-%Y %H:%M\")
#
# Clear journal log
#
line '-' > \$logfile
diskusage=\$(journalctl --disk-usage)
printf \"\$datenow Before Cleanup: \$diskusage\\n\" >> \$logfile
journalctl --rotate >/dev/null 2>&1
journalctl --vacuum-time=1d >/dev/null 2>&1
diskusage=\$(journalctl --disk-usage)
printf \"\$datenow After Cleanup: \$diskusage\\n\" >> \$logfile
line '-' >> \$logfile
"
#
# Create clear journal log script in /usr/local/bin
#
printf '%s\n' "${script[@]}" |sed '$d' |tail -n +2 > /usr/local/bin/clear-journal-log
chmod +x /usr/local/bin/clear-journal-log
echo "$cronjob01" > /etc/cron.d/clear-journal-log
#
# Restart cron
#
systemctl restart cron.service
#
# End of script
#