How do I prevent .xsession-errors from eating disk space?

Solution 1:

I found an interim solution.

I put a small script in /etc/X11/Xsession.d called 91redirect-xsession-errors that does the job for now, but if you want to have your own custom symlink for .xession-errors it does not work for that (I tried and it did not output any data).

#!/bin/sh

# Redirect $HOME/.xsession-errors to /dev/null.
# BJEM 11 January 2012

XSESSION_ERRFILE=$HOME/.xsession-errors

# This does not seem to work for a regular file,
# i.e. if you want to symlink $HOME/.xsession-errors
# to another file.  I do not know why.
XSESSION_ERRFILE_FINAL=/dev/null

# Creates target file if it does not exist.
touch "$XSESSION_ERRFILE_FINAL"

# Link .xsession-errors file to the desired target
# no matter what.
ln -sf "$XSESSION_ERRFILE_FINAL" "$XSESSION_ERRFILE"

# Test case.
#gedit &

##### END OF FILE #####

It's a bit 'rough and ready' but it does the job for me. Note that this is the only file that has been altered.

Solution 2:

There is a file called /etc/X11/Xsession. Which will create the symlink to a tmp file. IE. Starts on line number 61

ERRFILE=$HOME/.xsession-errors

# attempt to create an error file; abort if we cannot
if (umask 077 && touch "$ERRFILE") 2> /dev/null && [ -w "$ERRFILE" ] &&
  [ ! -L "$ERRFILE" ]; then
  chmod 600 "$ERRFILE"
elif ERRFILE=$(tempfile 2> /dev/null); then
  if ! ln -sf "$ERRFILE" "${TMPDIR:=/tmp}/xsession-$USER"; then
    message "warning: unable to symlink \"$TMPDIR/xsession-$USER\" to" \
             "\"$ERRFILE\"; look for session log/errors in" \
             "\"$TMPDIR/xsession-$USER\"."
  fi
else
  errormsg "unable to create X session log/error file; aborting."
fi

You can cp this Xsession file to Xsession.bak. Then go a head and point your ERRFILE to /dev/null IE. Line 83

exec >> /dev/null 2>&1

Solution 3:

In case you still need a solution that keeps logs and has a proper rotation (as it should be for any logged data).

Here is my approach:

replace

exec >>"$ERRFILE" 2>&1

with

exec > >(logger -t xsession-$USER) 2>&1

in the /etc/X11/Xsession file

That will send all logs to the local syslog server, which can send the logs to the /var/log/syslog file by default and have proper logrotate rules

Alternatively, you can route these messages to a separate file, using syslog configuration and have separate logrotate rules for it altogether.