Really large .xsession-errors

Post your bugs here.
User avatar
ilu
Posts: 2073
Joined: 09 Oct 2013 12:45

Re: Really large .xsession-errors

Postby ilu » 06 Oct 2018 21:11

I'm testing again, now using .xsession-errors. This doesn't work as quickly as with using dummy data.

I've already found a mistake, the MAX_BYTES calculation is wrong by 1000. But even with that change the script still operates even if the file has just 1000 bytes. :-( The if clause is not working correctly.

I also found another neat command which is part of the moreutils package: sponge. It keeps the inode.

Code: Select all

~/bin$ ls -i test.log
3014673 test.log
~/bin$ tail -n 1000 test.log | sponge test.log
~/bin$ ls -i test.log
3014673 test.log

ls -i test.log
3014673 test.log
~/bin$ sed -e :a -e '$q;N;1000,$D;ba' test.log | sponge test.log
~/bin$ ls -i test.log
3014673 test.log
That would be another way of doing it

Code: Select all

#!/bin/bash

# Backup .xsession-errors if larger than n MB (MAX_MB)
# Leave last n lines in original (LEAVE_LINES: set to 0 to skip backup)
# Check every n minutes (INTERVAL_MIN)

MAX_MB=1
LEAVE_LINES=10000
INTERVAL_MIN=5
# LOG='test.log'
LOG='/var/home/.xsession-errors'
# LOG='$HOME/.xsession-errors'

while true; do
    LOG_BYTES=$(stat --printf=%s "$LOG")
    MAX_BYTES=$((MAX_MB*1000000))
    if (( $LOG_BYTES > $MAX_BYTES )); then
        tail -n $LEAVE_LINES "$LOG" | sponge "$LOG"
    fi
    sleep $((INTERVAL_MIN*60))
done
This is working except for the if clause.

User avatar
Arjen Balfoort
Site Admin
Posts: 8887
Joined: 26 Jan 2013 19:36
Location: Netherlands
Contact:

Re: Really large .xsession-errors

Postby Arjen Balfoort » 06 Oct 2018 22:20

This is working except for the if clause.
Do you see what's wrong with the if statement?

I've got this, and it's working as expected (variables set smaller for testing and echoing debug info):

Code: Select all

#!/bin/bash

# Backup .xsession-errors if larger than n MB (MAX_MB)
# Leave last n lines in original (LEAVE_LINES)
# Check every n seconds (INTERVAL_SECS)
# Needs the moreutils package

MAX_MB=1
LEAVE_LINES=10
INTERVAL_MIN=1
LOG="$HOME/.xsession-errors"

while true; do
    LOG_BYTES=$(stat --printf=%s "$LOG")
    MAX_BYTES=$((MAX_MB*1000000))
    echo "LOG_BYTES=$LOG_BYTES, MAX_BYTES=$MAX_BYTES"
    if (( $LOG_BYTES > $MAX_BYTES )); then
        echo "Sponge $LOG"
        tail -n $LEAVE_LINES "$LOG" | sponge "$LOG"
    fi
    sleep $((INTERVAL_MIN*60))
done


SolydXK needs you!
Development | Testing | Translations

User avatar
Arjen Balfoort
Site Admin
Posts: 8887
Joined: 26 Jan 2013 19:36
Location: Netherlands
Contact:

Re: Really large .xsession-errors

Postby Arjen Balfoort » 06 Oct 2018 22:30

Sorry but sponge seems to have the same problem as sed :cry:

Back to truncate and tail. Simpler code:

Code: Select all

tail -n $LEAVE_LINES "$LOG" > "$LOG.bak"


SolydXK needs you!
Development | Testing | Translations

User avatar
ilu
Posts: 2073
Joined: 09 Oct 2013 12:45

Re: Really large .xsession-errors

Postby ilu » 06 Oct 2018 23:10

What's making this difficult is that I have to remember to always log out and in to make sure that the updated script is used. It doesn't seem to update without that procedure. For me the sponge version works fine, logging just continues. But just now an xsession-error file the size of 456.885 bytes got backed up and set to zero. And 5 min later one with 4000 bytes. That should not happen because we have specified 1 MB.

Wait, why does it backup anything? There is no backup code in it atm. How do I refresh what's used by xfce autostart? Log in and out doesn't seem to cut it. Restart also not ... ooookay.
Edit: I checked .xsession-errors :P - Whatever happened there I can't reproduce it.

Upon manual execution things seem to work ok with this code (the truncate way of doing things is probably safer):

Code: Select all

while true; do
    LOG_BYTES=$(stat --printf=%s "$LOG")
    MAX_BYTES=$((MAX_MB*1000000))
    if (( $LOG_BYTES > $MAX_BYTES )); then
        tail -n $LEAVE_LINES "$LOG" > "$LOG.bak"
        truncate -s 0 "$LOG"
    fi
    sleep $((INTERVAL_MIN*60))
done 
It seems that after several relogs I've also managed to get the autostart right and will just let it run now with -x and the reduced values 1 MB and 5 min.

User avatar
Arjen Balfoort
Site Admin
Posts: 8887
Joined: 26 Jan 2013 19:36
Location: Netherlands
Contact:

Re: Really large .xsession-errors

Postby Arjen Balfoort » 07 Oct 2018 08:57

Before adding this to solydxk-system, this is what I have now:

~/bin/xsession-logcontrol

Code: Select all

#!/bin/bash

# Truncate .xsession-errors if larger than n MB (MAX_MB)
# Backup last n lines (LEAVE_LINES: set to 0 to skip backup)
# Check every n minutes (INTERVAL_MIN)

MAX_MB=100
LEAVE_LINES=10000
INTERVAL_MIN=60
LOG="$HOME/.xsession-errors"

while true; do
    LOG_BYTES=$(stat --printf=%s "$LOG")
    MAX_BYTES=$((MAX_MB*1000))
    if (( $LOG_BYTES > $MAX_BYTES )); then
        if (( $LEAVE_LINES > 0 )); then tail -n $LEAVE_LINES "$LOG" > "$LOG.bak"; fi
        truncate -s 0 "$LOG"
    fi
    sleep $((INTERVAL_MIN*60))
done
~/.config/autostart/xsession-logcontrol.desktop

Code: Select all

[Desktop Entry]
Name=Xsession Errors Log Control
Comment=Limit the xsession-errors file size
Exec=$HOME/bin/xsession-logcontrol
Type=Application
StartupNotify=false
Terminal=false
After login these processes are running:

Code: Select all

$ ps aux | grep xsession
arjen     1828  0.0  0.0   4288   712 ?        S    10:48   0:00 /bin/sh -c $HOME/bin/xsession-logcontrol 
arjen     1830  0.0  0.0  12728  2948 ?        S    10:48   0:00 /bin/bash /home/arjen/bin/xsession-logcontrol
arjen     2519  0.0  0.0  14324   976 pts/1    S+   10:48   0:00 grep --color=auto xsession


SolydXK needs you!
Development | Testing | Translations

User avatar
Arjen Balfoort
Site Admin
Posts: 8887
Joined: 26 Jan 2013 19:36
Location: Netherlands
Contact:

Re: Really large .xsession-errors

Postby Arjen Balfoort » 07 Oct 2018 09:54

When I package this, I will move the .desktop file to /etc/xdg/autostart/ and the script to /usr/bin
This way, the script will run on user login but not show up in the users home directory.
The .desktop file will also be translatable.


SolydXK needs you!
Development | Testing | Translations

User avatar
Arjen Balfoort
Site Admin
Posts: 8887
Joined: 26 Jan 2013 19:36
Location: Netherlands
Contact:

Re: Really large .xsession-errors

Postby Arjen Balfoort » 07 Oct 2018 11:22

Forgot that xdg will run the process as root. Back to the original idea.


SolydXK needs you!
Development | Testing | Translations

User avatar
Arjen Balfoort
Site Admin
Posts: 8887
Joined: 26 Jan 2013 19:36
Location: Netherlands
Contact:

Re: Really large .xsession-errors

Postby Arjen Balfoort » 07 Oct 2018 12:34

solydxk-system is in testing now.
/etc/apt/sources.list:

Code: Select all

deb https://repository.solydxk.com solydxk-9 main upstream import testing
Github: https://github.com/SolydXK/solydxk-syst ... 3c17aa16dd
Transifex: https://www.transifex.com/solydxk/solyd ... dashboard/


SolydXK needs you!
Development | Testing | Translations

User avatar
ilu
Posts: 2073
Joined: 09 Oct 2013 12:45

Re: Really large .xsession-errors

Postby ilu » 07 Oct 2018 18:29

Code: Select all

MAX_BYTES=$((MAX_MB*1000))
This needs to be 1000000 otherwise you have KB not MB.

And I would per default set MAX_MB to maybe 500 to make sure that if a log overflow happens the script won't run too often (reports are up to 8 GB growth/hour). 2 x 500 MB is a size every system can tolerate.

And I'm wondering why 2 processes are running on your system? There's only one on mine. I might have had 2,3, many when the confusion of my last post happened.

Code: Select all

ps aux | grep xsession
       2685  0.0  0.0  12732  2824 ?        S    19:49   0:00 /bin/bash -x /home/.../bin/xsession-logcontrol.sh
       3416  0.0  0.0  14324   944 pts/1    S+   20:31   0:00 grep --color=auto xsession
Regarding the place of the script: Since you are now packaging this, wouldn't the correct place be /usr/bin or at least /usr/local/bin? I don't think you should tamper more than absolutely necessary with users home directories. also it would need to be placed into every users home dir (which usually are not too many on our users machines but could be many). I see that creating the autostart entry needs to be done in the home dir since XDG won't work. But all the other things called by autostart (f-e. redshift-gtk which I installed) sit in usr/bin and just create their autostart entry in the user home upon installation/activation.

Aside from this, I think a ~/bin or ~/,bin directory is very useful, especially if added to the PATH.

User avatar
Arjen Balfoort
Site Admin
Posts: 8887
Joined: 26 Jan 2013 19:36
Location: Netherlands
Contact:

Re: Really large .xsession-errors

Postby Arjen Balfoort » 08 Oct 2018 06:39

I cannot package files in /usr/local. Only the user may use that directory (a package is installed by root). If I try to package that directory, dpkg will throw an error and exit. So, all scripts are packaged in /usr/bin.

The .desktop file is going to /etc/skel and will only be installed when a new user is created. A deb package must never interfere with the user's home directory: it's run by root. You can do it with some ugly code (copying files from the package directory to the current user's directory, then changing ownership) in the postinst script of the package but it is not according to Debian policy. Personally, I don't like to mess with the user's home directory.


SolydXK needs you!
Development | Testing | Translations

User avatar
ilu
Posts: 2073
Joined: 09 Oct 2013 12:45

Re: Really large .xsession-errors

Postby ilu » 13 Oct 2018 23:04

Does it work for you? My xsession-errors has reached 600 MB but nothing happens. I've added the -x flag to get a log if the script runs.

User avatar
Arjen Balfoort
Site Admin
Posts: 8887
Joined: 26 Jan 2013 19:36
Location: Netherlands
Contact:

Re: Really large .xsession-errors

Postby Arjen Balfoort » 14 Oct 2018 09:52

Check if xsession-logcontrol is running:

Code: Select all

ps aux | grep xsession
arjen     2531  0.0  0.0  12728  3004 ?        S    10:35   0:00 /bin/bash /usr/bin/xsession-logcontrol
arjen     3233  0.0  0.0  14324   988 pts/1    S+   10:38   0:00 grep --color=auto xsession
Check the size of .xsession-errors

Code: Select all

ls -hal | grep xsession
-rw-------  1 arjen arjen 996K Oct 14 10:39 .xsession-errors
-rw-------  1 arjen arjen  26K Oct 14 10:34 .xsession-errors.old
Add random data to .xsession-errors (more than 500MB to trigger xsession-logcontrol):

Code: Select all

base64 /dev/urandom | head -c 600000000 >> .xsession-errors
Check that the size is over 500MB:

Code: Select all

ls -hal | grep xsession
-rw-------  1 arjen arjen 574M Oct 14 10:41 .xsession-errors
-rw-------  1 arjen arjen  26K Oct 14 10:34 .xsession-errors.old
...wait an hour...

And check again:

Code: Select all

ls -hal | grep xsession
-rw-------  1 arjen arjen 6.9K Oct 14 11:49 .xsession-errors
-rw-r--r--  1 arjen arjen 756K Oct 14 11:35 .xsession-errors.bak
-rw-------  1 arjen arjen  26K Oct 14 10:34 .xsession-errors.old
Which is exactly what I expected!

[EDIT]
This was tested on SolydK 9 and SolydX 9.
Make sure you have copied /etc/skel/.config/autostart/xsession-logcontrol.desktop to ~/.config/autostart/


SolydXK needs you!
Development | Testing | Translations

User avatar
ilu
Posts: 2073
Joined: 09 Oct 2013 12:45

Re: Really large .xsession-errors

Postby ilu » 14 Oct 2018 16:18

My mistake! I forgot that I have moved xsession-errors and that therefore I have to adapt the script ... I'm sorry.

We can't use the ERRFILE variable from /etc/x11/Xsession can't we? I think it's not global but I'll check.

Edit: No, doesn't work. Well, not too many people will change the ERRFILE location anyway.

Works now.

User avatar
Arjen Balfoort
Site Admin
Posts: 8887
Joined: 26 Jan 2013 19:36
Location: Netherlands
Contact:

Re: Really large .xsession-errors

Postby Arjen Balfoort » 16 Oct 2018 06:44

I have moved solydxk-system from testing to main.


SolydXK needs you!
Development | Testing | Translations


Return to “Bug control”

Who is online

Users browsing this forum: No registered users and 2 guests