GNU/Linux, Open Source, Cloud Computing, DevOps and more...

How to prevent the .xsession-errors file from growing to a huge size

2 comments

Xorg iconThe .xsession-errors file is where the X Window system logs all errors that occur within the Linux graphical environment. All desktop environments, whether Gnome, KDE, Cinnamon, XFCE, LXDE, etc., and all lighter window managers like FVWM, IceWM or Window Maker make use of the X Window system. Therefore any graphical application running on your computer can cause that error messages are written to the .xsession-errors file, reason why it can grow wildly until reaching very big sizes of tens of GB or even hundreds if your disk capacity allows it.

Just as this problem affects different desktop environments and window managers based on X Window (that is, all of them), all Linux distributions, whether Ubuntu, Fedora, Debian or ArchLinux can also be affected.

Although there is a control mechanism in the /etc/X11/Xsession file that causes the file to be emptied each time the graphical environment starts up if it exceeds a certain size, if you are one of those who, like me, normally suspend the computer instead of powering it off when the working day ends, it may be that you don’t restart your computer for weeks or even months, which means that the .xsession-errors file will never be truncated and can reach a gigantic size. And although you do frequently restart the system, it also happens that some applications, whether due to some type of failure/error or because they misuse the error log, start to send uncontrolled thousands and thousands of messages to .xsession-errors.

The .xsession-errors file is usually located in your home directory and the only problem it should cause is that your personal folder is full. But if you have not partitioned your disk properly and only have one partition for the entire root (/) file system, the fast growth of .xsession-errors can cause your computer to stop working and crash.

Further reading:
  The importance of properly partitioning a disk in Linux

Empty the .xsession-errors file

If it already happened that you ran out of disk space and using the du -k /home | sort -n | tail -5 command you were able to determine that the .xsession-errors file is the one that takes up more space, the first step to fix the problem is to empty it completely:

$ >~/.xsession-errors

Prevent .xsession-errors from growing out of control

Once the space is freed, you will want this situation not to be repeated again in the future. To achieve this it is best to try to find the origin of the problem, ie, to know which process is writing uncontrolled to the error log and why. For this you can use the fatrace command as indicated in this other post: Fatrace command: how to know in real time which processes are writing to a file.

Another interesting measure to adopt is to add a task to your crontab to periodically check the size of .xsession-errors file. If it exceeds a certain threshold, empty or truncate it so that only the last lines are retained:

– Example #1: Check every 15 minutes if the file size is greater than 5 GB and if so empty it:

*/15      *       *       *       *       [ $(du -k .xsession-errors | awk '{ print $1 }') -gt 5000000 ] && >/home/$(whoami)/.xsession-errors

– Example #2: Do the same check, but instead of emptying the entire log, keep the last 10,000 lines:

*/15      *     *       *       *       [ $(du -k .xsession-errors | awk '{ print $1 }') -gt 5000000 ] && tail -10000 /home/$(whoami)/.xsession-errors > /home/$(whoami)/.xsession-errors

Disable writes on .xsession-errors file

If you want to forget about this log because in normal operation you are not interested in its debugging information, you can redirect to /dev/null everything that is written to it and thus always keep a size of 0 bytes. For this you can edit the /etc/X11/Xsession configuration file of the X Window system and locate the following line:

ERRFILE=$HOME/.xsession-errors

Replace it with:

ERRFILE=/dev/null

You can also delete the .xsession-errors file and create a symbolic link to /dev/null instead in order to get the same result:

$ rm .xsession-errors
$ ln -s /dev/null .xsession-errors

The problem is that when you restart the session the symbolic link will be replaced back by a regular file and will start to grow again. To avoid this you must add the following lines to the .bashrc script in your home directory:

# If the .xsession-errors file is not a symbolic link, delete it and create it as such
if [ ! -h $HOME/.xsession-errors ]; then
 /bin/rm $HOME/.xsession-errors
 ln -s /dev/null $HOME/.xsession-errors
fi

Finally, there is another way which consists in setting the immutable attribute to the file, which will prevent to write anything to it by any user or process:

$ sudo chattr +i .xsession-errors


 

About the author

Daniel López Azaña
Cloud Solutions Architect

Entrepreneur, a generator of ideas and restless mind. Passionate about new technologies, especially Linux systems and Open Source Software. I also like to write about Technology News, Cloud Computing, DevOps, System Security, Web Development and Programming, SEO, Science, Innovation, Entrepreneurship, etc.

DanielHow to prevent the .xsession-errors file from growing to a huge size

Related Posts

2 comments

Join the conversation
  • Robert - 15/10/2017 reply

    It appears that this part of your cron job commands don’t work:

    (du -k .xsession-errors | awk ‘{ print $1 }’) -gt 5000000

    I get:

    bash: syntax error near unexpected token `-gt’

    …when running just that part of the command.

    Daniel - 16/10/2017 reply

    Hi Robert, please note the brackets ([ ]) around as they are very important for the command to work. They are equivalent to the test builtin bash command, so if you don’t use them the -gt comparison operator doesn’t make sense and you get the error message you mentioned.

Leave a Reply

Your email address will not be published. Required fields are marked *