serverauth files

I have numerous 'serverauth.***' files in my home directory. They seem to be related to xorg. Some have been hanging around for a long time. What is the recommended way of deleting them automatically after some period?
 
If you make sure you're not logged in you can remove all of them. They're created when you run startx. They should be automatically cleaned up if you exit X normally.
 
 
You only need the most recent one for your current X session. The rest is garbage.

nxjoseph,​

you don't need such a "complicated" script checking for the most recent one. Just do a
sh:
if [ -e "~/.serverauth.*" ] ; then
    rm ~/.serverauth.*
fi
in your
~/.login
to delete all, if there are any, before you
startx and produce the current one, would also do. :cool:

However, if you produce several of them you may check your setup. It's not a serious problem, but a sign for your X sessions are not closed cleany.
 
You only need the most recent one for your current X session. The rest is garbage.
Yes, my script gets today's date and finds the most recent one by comparing the date of file vs system date, then marks it to be ignored, then garbage gets deleted.


you don't need such a "complicated" script checking for the most recent one.
I do like and enjoy writing scripts, it feels challenging to me.


Just do a
if [ -e "~/.serverauth.*" ] ; then rm ~/.serverauth.* fi
dunno what '-e' implies here but this seems to be delete all the .serverauth files, also i don't use bash but csh, the reason i'm writing scripts in csh is because i want to learn more about my shell that i daily drive.


in your
~/.login
to delete all, if there are any, before you
startx and produce the current one, would also do. :cool:
I also noticed that adding some script to run in .login is causing some delay when spawning a shell. I removed mine from that file for that reason.

Also i think that it would delete them too after you ran Xorg session and open a term (spawn a shell).


However, if you produce several of them you may check your setup. It's not a serious problem, but a sign for your X sessions are not closed cleany.
Surprisingly, IIRC, there are no outdated serverauth files anymore. Did they wait for me to write a script to not produce garbage? 🤔
 
I do like and enjoy writing scripts, it feels challenging to me.
Don't get me wrong: I would never tell anybody how to write their scripts. I just wanted to say, there was another (my) solution.
I once had the issues of this thread myself: ~ is filling with not deleted serverauth-files. So I realized this work-around of deleting them until I found out why and set my X server to terminate correctly (but I forgot - it's many years ago.)

Also, you never know when you need to reuse what you've written once for another case.
I guess you already have some kind of a system where and how you organize your scripts.
Well structured, judicious named directories, a versioning control system and a good way of how to document is not to be underestimated in its value. Sooner or later the stuff you produce grows large. The earlier you have a sophisticated - your needs suiting system - the better. Otherwise you risk to lose survey.
For example I have a experimenting area where I have small scripts doing one thing, only, answering myself questions like "how can I calculate...?" or "how to measure time" and such things. You not only can learn a lot this way, but it's also a good "toolbox" you may find useful prewritten things in it you may need for "real" scripts later.
Of course, "you can google it all" ... yes. But besides seldom you find the exact thing you actually need, where is the fun in that?

Also i think that it would delete them too after you ran Xorg session and open a term (spawn a shell).
No. ~/.login is executed only once when you login the first time. ~/.cshrc is run every time you spawn a new shell.

dunno what '-e' implies here but this seems to be delete all the .serverauth files, also i don't use bash but csh, the reason i'm writing scripts in csh is because i want to learn more about my shell that i daily drive.
"if file exists" - it's sh file test condition; see Table B-3. TEST Operators: Files on Advanced Bash-Scripting Guide
It's to prevent an error message for executing rm blank if there weren't any file.
Of course it deletes all. That's the whole point.
Instead of find and separate the recent one from all others, I simply delete all severauth-files before a X session is started. This way only the one exists the X session produces, which - when not be deleted by a clean terminated X session, will be deleted before the next X session.
Of course it may no solution for every situation (I have no idea how this behaves with a display ["login"] manager), maybe not so good...)
It was just what I did.

The better solution anyway is to make the X server terminate correctly, so no extra serverauths are produced in the first place.
The one or the other serverauth file produced by seldom non clean termination events I simply delete by hand.

However, writing scripts is not only for fun but a very important competence in unix[like] systems.
Personally I also use tcsh for users and root. But - and again this is my very personal taste, absolutely not telling you what to do or not - I also once started myself learning scripting in csh - "If I use this shell, then why not learn to script in it", right? I learned to program in C long before I started scripting. I took a first glance at bash scripting and thought: "They must be nuts! No, I prefer to stick to my familar C syntax."
Well, as understandable as this sounds - the field of application of both languages are completely different. The target area of shell scripting is doing things with files, while C not only plays in another league, but it's a complete other game. I admit: To get into sh/bash scripting seems to be a bit odd for the start (especially testing is kind of weird (those spaces, especially after [ and before ] are important {space is shell's default delimiter}), and quoting is another special mission to be understood, but you'll find out quickly: There is a good reason why sh's syntnax is the way it is.
On scripting in csh I gave up pretty quickly. Scripting in csh is kind of masochism, at least to me, and I would not recommend to learn it first - if even at all. Quickly one faces its limits, which means you would need way less effort doing the same in sh (again, I only run scripts in its language) - if it's even possible within csh with an acceptable effort.
Frankly, before I'd torture myself with csh scripting I recommend to learn first scripting in sh, Python, or Perl.
Some things I also really do in C.

But I also do things on my machine myself just for fun, don't care if it's always most efficient, or even useful.
And of course everbody find the fun elsewhere. :cool:
Enjoy!
 
if [ -e "~/.serverauth.*" ] ; then rm ~/.serverauth.* fi
Wouldn't it be the same as simple: rm -f ~/.serverauth.*?

Yes, that's right, but I would suggest nxjoseph the better place to find the answer for his question :) Just look at test(1) (you can do either man test or man [ - it's the same program) and make a search for '-e' flag. In fact, while here, you can learn about some interesting flags you didn't even know about. For example,
Code:
file1 -ef file2
        True if file1 and file2 exist and refer to the same file.
is how you can tell hard links (see ln(1)).
Or
Code:
file1 -nt file2
        True if file1 exists and is newer than file2.
I could have used it in the script I attached below, but I looked at the sources and found that this flag uses st_mtim field from stat(2), which holds file's last modification date. It means that I can touch(1) one of the files and this check will have a diffent result. That's why in my script I'm using inode creation date (st_birthtim).

And I also like to share my script, that essentially does the same thing: cleans up .serverauth files in one's $HOME. Having it as a separate script is nice, because if you wish, you can execute it manually at any point of time or make this cleaning happen on system boot via rc(8).

sh:
#!/bin/sh

#
# hkeep -- remove garbage (unneeded files) from home directories.
#
# If no arguments are specified, the target directory is set to $HOME.
# Otherwise, the housekeeping will be done in directories, given as arguments.
#
# Options:
#     -v    -- Be verbose.
#

usage()
{
    echo "Usage: $(basename $0) [-v] [path ...]" 1>&2
    exit 2
}

#
# Verbose logging.
#
vlog()
{
    if [ $v_flg -eq 1 ]; then
        echo "$(basename $0): $target_dir: $1"
    fi
}

#
# .serverauth.* files are created by Xserver(1) and should be deleted every
# time the server is closed properly.  Thus, if Xserver(1) is not running now,
# we can freely delete all of them.  And in case it is running, we should find
# the file that was created most recently and leave it the only one alive.
#
clean_serverauth()
{
    target_files=$(find "$target_dir" -maxdepth 1 -type f \
        -name "\.serverauth*")
    
    if [ -z "$target_files" ]; then
        vlog "No .serverauth files found"
        return 0
    fi
    
    #
    # Xserver(1) creates a unix(4) socket for every active display in
    # /tmp/.X11-unix/X*.  If at least one such socket is found, we can tell
    # that server is currently running.
    #
    if [ -n "$(find /tmp/.X11-unix -type s 2>/dev/null)" ]; then
        x_running=1
    else
        x_running=0
    fi
    
    if [ $x_running -eq 0 ]; then
        vlog "X server is not running.  Remove all .serverauth files"
        rm -v $target_files
        return 0
    fi
    
    # If we only have a .serverauth file for currently running server.
    if [ $(echo "$target_files" |wc -l) -eq 1 ]; then
        vlog "No unnecessary .serverauth files found"
        return 0
    fi
    
    most_recent_name=""
    most_recent_ts=0
    
    for target in $target_files; do
        ts=$(stat -f %B "$target")
        if [ $ts -ge $most_recent_ts ]; then
            most_recent_ts=$ts
            most_recent_name="$target"
        fi
    done
    
    vlog ".serverauth file for currently running server: $most_recent_name"
    
    for target in $target_files; do
        if [ "$target" = "$most_recent_name" ]; then
            continue
        fi
        rm -v "$target"
    done
}

v_flg=0
while getopts "v" o; do
    case $o in
    v)
        v_flg=1
        ;;
    ?)
        usage
        ;;
    esac
done
shift $((OPTIND - 1))

if [ $# -eq 0 ]; then
    target_dirs="$HOME"
else
    target_dirs="$@"
fi

for target_dir in $target_dirs; do
    if [ ! -d "$target_dir" ]; then
        echo "$target_dir is not a directory" 1>&2
        continue
    fi
    
    clean_serverauth "$target_dir"
done

UPD: I actually updated the script and made it take a list of target directories to be housekept, intead of always doing the job at $HOME. It would be handy especially if we want to set up this script as rc(8) task, where we want to perform cleaning in all home directories in the system.
 
Wouldn't it be the same as simple: rm -f ~/.serverauth.*?
Yes, it would. Those things happen if you're not aware of all options because of not reading man pages enough :cool:

But don't we forget:
This is to patchwork a unclean situation - quick'n'dirty work around.
A clean solution was to avoid superfluid .severauth files by closing the X server cleanly.
 
A clean solution was to avoid overfluid .severauth files by closing the X server cleanly.
Sure! However, if you have already screwed something up - well, you've done that, there's no way to roll it back now. And in this case it would be nice to have thing that would automatically clean something that you've screwed up :) I, for example would better have this script as startup rc(8) task, that manually figuring out which files I can freely delete. Essentially, the idea is the same as with the fact that on every boot fsck(8) checks your filesystems and tries to fix them up, if they somehow happened to be corrupted.
 
Depends. To me it was fully sufficient if this few lines were part of the .xinitrc.
Btw. if you just simply want to run a script once at boot (when cron starts, at the end of the boot process), you can also simply use cron @reboot.
 
if you just simply want to run a script once at boot (when cron starts, at the end of the boot process), you can also simply use cron @reboot.
Oh, thanks, haven't thought about it! I would convert it from rc to cron then :) Although I found it quite useful to familiarize myself with writing simple rc script.
 
Back
Top