Friday, January 08, 2021

Prevent .xsession-error file from growing too big

After playing around with HDMI capture on my Raspberry Pi 4, I ended up with a .xsession-error file that was 16GB in size! 😱 This prevented my system from working properly. I deleted the file and rebooted the system, which helped, but I wanted to prevent the same thing from happening again.

I found two main ways.

One is to redirect the output of X session errors to null. First, make a backup of the file to change by
sudo cp /etc/X11/Xsession /etc/X11/Xsession.bak
Then, edit /etc/X11/Xsession and change the line
exec >>"$ERRFILE" 2>&1
to
exec >> /dev/null 2>&1
It basically redirects all error logging for X sessions to null. I don't like this method since it means there is no proper log of any errors.

The second method is to periodically check the size of the .xsession-error file, then empty it when it becomes too big. This is done using cron. For example, using the command
crontab -e
Then, add in
*/15 * * * *  [ $(du -k /home/$(whoami)/.xsession-errors | awk '{ print $1 }') -gt 50000 ] && >/home/$(whoami)/.xsession-errors
What this does is to check the size of the .xsession-error file every 15 minutes. If it is bigger than 50,000 kilobytes, the file is emptied. You can change the file size as necessary.

du -k /home/$(whoami)/.xsession-errors gives the size of the file in kilobytes.
awk '{ print $1 }' prints out the first argument from the piped output of the previous, which is the size of the file.
-gt 50000 compares the result to see if it is greater than a number (50000) in this case, returning true if so. This will cause the final command to be execute.
>/home/$(whoami)/.xsession-errors empties the file.

No comments: