[FreeNX-kNX] kded4 runs away. A bit O.T.for this group now

Matthieu.Rioteau at skf.com Matthieu.Rioteau at skf.com
Fri Oct 21 13:38:25 UTC 2011


Hi Julie and all,

After some time thinking and coding, I found an acceptable solution (at 
least it's satisfying myself :-) ).

This is based around a bash script that I called "cleanNXsession" (you can 
choose your own, but I'll explain things with this one). I'll copy the 
script at the end of this email for better clarity.
This script basically browses all processes and check if they are linked 
to a closed NX session (running and even suspended sessions will be 
detected and kept). If they are, the script kill them.

The script has a "chmod 700" applied on it so only root can use it. It 
takes some optional parameters at launch time, but I will explain them on 
the job.

First, I use it by programming 2 cronjobs as below :
----------------------------------------------------------------------------------------------------------------------------------
0 * * * * /usr/local/bin/cleanNXsession -e " : launched by cron"
0 0 * * * /usr/local/bin/cleanNXsession -m
----------------------------------------------------------------------------------------------------------------------------------
The first one runs a clean up every hour and the '-e' option add an extra 
title in the log file
The second sends a digest email every day at midnight ( '-m' option send 
the mail)

Then, I use it in a different manner by launching it when a NX session 
ends. To do so, first thing is to allow all users to run the script as 
root. I did it through 'sudo' by creating the file 
"/etc/sudoers.d/cleanNXsession" with this text inside :
----------------------------------------------------------------------------------------------------------------------------------
ALL ALL=NOPASSWD:/usr/local/bin/cleanNXsession
----------------------------------------------------------------------------------------------------------------------------------

Then second thing is to hack the 'nxnode' script (normally 
/usr/lib/nx/nxnode).
At the total end of the 'node_terminate_session()' function, add ONE of 
the following 2 lines, depending which 'screen' or 'nohup' program is 
installed ('screen' seems a little bit better, but both work fine).
----------------------------------------------------------------------------------------------------------------------------------
screen -d -m -S cleanNX sudo cleanNXsession -e " : Launched after NX 
session ending by $USER " -w 15
----------------------------------------------------------------------------------------------------------------------------------
OR
----------------------------------------------------------------------------------------------------------------------------------
nohup sudo cleanNXsession -e " : Launched after NX session ending by $USER 
" -w 15 &
----------------------------------------------------------------------------------------------------------------------------------
The '-e' option adds extra title in log, and '-w' option tells the script 
to wait 15 seconds before executing (basically the time for the session to 
close).

Now this is ready to run, so with that configuration, NX session clean up 
will happens at every session closing, and regularly once an hour.

The last option is '-d' that can be added to dry run (the processes won't 
be really killed).

I advise that you browse the script before using it and check that things 
are set accordingly to you needs (log level, mail adresses, ...).

Now it's time to paste the script below, all variables that you shall set 
up are prefixed by 'TBS:'. Feel free to do any comment about it (I tested 
it only with KDE4).
Hope it helps. Matthieu

----------------------------------------------------------------------------------------------------------------------------------
#!/bin/bash

####################################################
# General functions
####################################################

# Write log according to log level
cslog() {
        if [ $2 -le $LOGLEVEL ]
        then
                echo -e $1
        fi
}

####################################################
# Define parameters
####################################################

LOGLEVEL=2      # log level : 3=debug ; 2=verbose ; 1=normal ; 0=no log
KEEPLOG=0       # 0 = erase log after digest mail sending ; 1 = keep
LOGFILE=TBS:LOG_FILE       # path & name of log file
exec >> $LOGFILE        # comment this line to debug on console

MAILSUB="TBS:MAIL_SUBJECT"  # subject of digest mail
MAILRECEIPT="TBS:MAIL_ADDRESS"     # receiver of digest mail

EXTRATITLE=""   # default extra title for log ; will be overwritten by the 
one in argument if exist

WAITTIME=0      # time to wait before executing ; will be overwritten by 
the one in argument if exist

DRYRUN=0        # 0 = processes will be truly killed ; 1 = dry run

####################################################
# Browse script arguments
####################################################

cslog "\n`date` ==> NX session cleaning script launched with arguments : 
$*" 3

until [ -z "$1" ]       # going through arguments
do
        case $1 in
                -d )    # dry run
                        cslog "Dry run mode activated" 3
                        DRYRUN=1
                ;;
                -e )    # add extra title
                        cslog "Extra title added : $2" 3
                        EXTRATITLE="$2"
                        shift
                ;;
                -w )    # set up time to wait before starting
                        cslog "Wait time set up : $2 seconds" 3
                        WAITTIME=$2
                        shift
                ;;
                -m )    # send digest mail
                        if [ -f $LOGFILE ]
                        then
                                NBERR=`cat $LOGFILE | grep -E 
"\[\!*ERR\!*\]" | wc -l`  # counting errors in log
                                if [ $NBERR -ge 1 ] # if some errors 
happened
                                then
                                        MAILSUB="$MAILSUB : $NBERR errors" 
 # add error number in the mail subject
                                fi
                                if cat $LOGFILE | grep -E 
"\[\!*[A-Z]{3}\!*\]" | mailx -E -s "$MAILSUB" "$MAILRECEIPT"  # create the 
digest and try to send it ; if email sending successes ...
                                then
                                        if [ $KEEPLOG -eq 0 ]
                                        then
                                                echo "" > $LOGFILE      # 
clear log file else ...
                                        fi
                                        cslog "`date` ==> Digest mail has 
been sent" 3
                                else
                                        cslog "`date`==> [!ERR!] Digest 
mail hasn't been sent" 1        # ... log the error
                                fi
                        else
                                cslog "[INF] Log file doesn't exist so no 
digest mail has been sent" 3
                        fi
                        exit
                ;;
                * )
                        cslog "]!ERR![ Script called with bad argument : 
$*" 3
                        exit 1
                ;;
        esac
        shift
done

sleep $WAITTIME # used to wait for the NX session to close

####################################################
# Search zombie processes and try to kill them
####################################################

cslog "\n`date` ==> Starting clean up of NX sessions $EXTRATITLE" 1
if [ ! $DRYRUN -eq 0 ]
then
        cslog "[INF] Launching in RUN DRY mode" 1
fi

ZOMBPROCNUM=0   # to count number of killed processes

SONPROC="$$"

# Search the script process hierarchy to prevent to kill one
until [ $SONPROC -eq 1 ]        # until "init" is reached
do
        FATHER=`strings /proc/$SONPROC/status | grep PPid | sed 
s/[^0-9]*//g`   # get father Pid
        FAMILY="$FAMILY,/proc/$FATHER"  # add father in the family
        SONPROC=$FATHER # father becomes the son
done
FAMILY="$FAMILY,"
cslog "My process hierarchy is : $FAMILY" 3

for PROC in /proc/[0-9]*        # going through processes
do
        cslog "Testing $PROC" 3
        if [ "$PROC" = "/proc/$$" ]     # if tested process is this script
        then
                cslog "Skipping myself (suicide is penally punishable)" 3
                continue        # go to the next one
        fi
        if [ `echo $FAMILY | grep -F ",$PROC," | wc -l` -gt  0 ]        # 
if tested process is in the hierarchy of this script
    then
        cslog "Skipping a member of my hierarchy" 3
        continue        # got to the next one
    fi
        if [ -d $PROC ] && [ -f $PROC/environ ] # if process have an 
"environ" file
        then
                cslog "$PROC/environ exists" 2
                if [ `strings $PROC/environ | grep XAUTHORITY | grep nx | 
wc -l` -eq 1 ]        # if "environ" defines a "XAUTHORITY" variable
                then
                        cslog "XAUTHORITY variable defined" 2
                        NXCHECK=`strings $PROC/environ | grep XAUTHORITY | 
grep -F "/.nx/" | sed s/.*\=//g`     # extract the path in "XAUTHORITY" 
checking that it has been created by NX
                        cslog "Checker file = $NXCHECK" 2
                        if [ -f $NXCHECK ]      # if the NX session that 
the process was link to is still running or suspended
                        then
                                cslog "File exists : do nothing" 2      # 
do nothing
                        else
                                cslog "File doesn't exist : have to kill 
it" 2
                                cslog "[DET] Process $PROC with NX checker 
$NXCHECK is no more attached to a running or suspended NX session" 1
                                cslog "[INF] Zombie process cmdline : `cat 
$PROC/cmdline`" 1
                                cslog "[ACT] Try to run : kill -9 `echo 
$PROC | sed s=/.*/==g`" 1
                                if [ $DRYRUN -eq 0 ]    # if dry run mode 
disabled
                                then
                                        if kill -9 `echo $PROC | sed 
s=/.*/==g` # else try to kill the process
                                        then
                                                let 
ZOMBPROCNUM=ZOMBPROCNUM+1
                                                cslog "[RES] Done 
sucessfully" 1
                                        else
                                                cslog "[!ERR!] Failed to 
kill process" 1
                                        fi
                                else
                                        let ZOMBPROCNUM=ZOMBPROCNUM+1
                                        cslog "[RES] DRY-RUN : process not 
truly killed" 1
                                fi
                        fi
                fi
        else
                cslog "$PROC is not a directory or hasn't any 'environ' 
file" 3
        fi
done

if [ $ZOMBPROCNUM -gt 0 ]
then
        if [ $DRYRUN -eq 0 ]
        then
                cslog "\n[INF] $ZOMBPROCNUM zombie proces(es) killed" 1
        else
                cslog "\n[INF] $ZOMBPROCNUM zombie process(es) found but 
not killed : DRY RUN mode" 1
        fi
else
        cslog "\n]INF[ no zombie process found" 1
fi
cslog "\n`date` ==> End of clean up of NX sessions $EXTRATITLE" 1
----------------------------------------------------------------------------------------------------------------------------------

==> End or reply :-)

freenx-knx-bounces at kde.org wrote on 18/10/2011 20:49:22:

> From: julie.ashworth
> To: freenx-knx at kde.org
> Date: 18/10/2011 20:49
> Subject: Re: [FreeNX-kNX] kded4 runs away.   A bit O.T.for this group 
now
> Sent by: freenx-knx-bounces at kde.org
> 
> I have a similar problem with xfce - specifically, 
> 'xfce4-xfapplet-plugin' processes remain after a NX session is 
> terminated. These plugin processes consume 99% cpu: 
> 
> PPID  C CMD
> 1     0 /usr/libexec/gconfd-2 5
> 1    99 /usr/libexec/xfce4/panel-plugins/xfce4-xfapplet-plugin 
> 1    99 /usr/libexec/xfce4/panel-plugins/xfce4-xfapplet-plugin 
> 1    99 /usr/libexec/xfce4/panel-plugins/xfce4-xfapplet-plugin 
> 1     0 /usr/libexec/bonobo-activation-server --ac-activate 
--ior-output-fd=17
> 1     0 /usr/libexec/gnome-dictionary-applet --oaf-activate-
> iid=OAFIID:GNOME_DictionaryApplet_Factory --oaf-ior-fd=27
> 1     0 /usr/libexec/multiload-applet-2 --oaf-activate-
> iid=OAFIID:GNOME_MultiLoadApplet_Factory --oaf-ior-fd=21
> 1     0 /usr/libexec/mini_commander_applet --oaf-activate-
> iid=OAFIID:GNOME_MiniCommanderApplet_Factory --oaf-ior-fd=24
> 
> If I terminate the xfce4-xfapplet-plugin processes, then the others
> also die. I am tempted to modify the nxnode script to force kill this 
> process at logout. 
> 
> Btw, I use NX in a multi-user environment with load-balancing.
> There are about 60 simultaneous NX sessions open (~1/3 running). 
> Only one NX session is allowed per uid.
> 
> Does anybody else have a similar problem (and solution)?
> 
> Thanks!
> Best,
> Julie
> 
> -- 
> Julie Ashworth <julie.ashworth at berkeley.edu>
> http://www.neuro.berkeley.edu
> PGP Key ID: 0x17F013D2
> ________________________________________________________________
>      Were you helped on this list with your FreeNX problem?
>     Then please write up the solution in the FreeNX Wiki/FAQ:
> 
> 
http://openfacts2.berlios.de/wikien/index.php/BerliosProject:FreeNX_-_FAQ
> 
>          Don't forget to check the NX Knowledge Base:
>                  http://www.nomachine.com/kb/
> 
> ________________________________________________________________
>        FreeNX-kNX mailing list --- FreeNX-kNX at kde.org
>       https://mail.kde.org/mailman/listinfo/freenx-knx
> ________________________________________________________________




More information about the FreeNX-kNX mailing list