You are not logged in.

#1 2012-04-04 12:45:39

comicosmos
Member
From: suzhou, China mainland
Registered: 2011-04-16
Posts: 45

NetworkManager dispatcher script error

I would like to  do system update automatically  when connected. So I add a script to /etc/NetworkManager/dispatcher.d.

I am able to run the script through cmd line and it works fine.
But it just does not work when invoked by networkmanager. It was frustrating.
Appreciate if someone could help.

The script:

#!/bin/sh                                                                                                      

IF=$1
STATUS=$2

logFileSize=`du -b ~/.NetWorkManagerDispatcher|awk {'print $1'}`
if [ "$logFileSize" -gt 500000 ];then
    tail -n 150 ~/.NetWorkManagerDispatcher > ~/.NetWorkManagerDispatcher
fi
echo "====================" >> ~/.NetWorkManagerDispatcher
echo "[ "`date +'%F %I:%M:%S %p'`" ]: size of log file is now $logFileSize" >> ~/.NetWorkManagerDispatcher
echo "[ "`date +'%F %I:%M:%S %p'`" ]: "$0", "$1"/"$2 >> ~/.NetWorkManagerDispatcher

wait_for_process(){
    PNAME=$1
    PID=`pgrep $PNAME`
    while [ -z "$PID" ];
    do
        sleep 3;
        PID=`pgrep $PNAME`
    done
    echo "[ "`date +'%F %I:%M:%S %p'`" ]: $1 is running!" >> ~/.NetWorkManagerDispatcher
}

if [ "$IF" = "eth1" ] && [ "$STATUS" = "up" ]; then
    wait_for_process nm-applet
    echo "[ "`date +'%F %I:%M:%S %p'`" ]: system update in xterm as $USER" >> ~/.NetWorkManagerDispatcher

    #this won't work
    /usr/bin/xterm -e "sudo ntpd -qdg && exit" || echo "[ "`date +'%F %I:%M:%S %p'`" ]: cannot run xterm"  >>  ~/.NetWorkManagerDispatcher

    #neither do this
    (sudo -u $USER xterm -e "sudo ntpd -qdg && exit") &

and here's some log file:

====================
[ 2012-04-04 07:58:35 PM ]: size of log file is now 6554
[ 2012-04-04 07:58:35 PM ]: /etc/NetworkManager/dispatcher.d/00yaourt, eth1/down
====================
[ 2012-04-04 07:59:00 PM ]: size of log file is now 6711
[ 2012-04-04 07:59:00 PM ]: /etc/NetworkManager/dispatcher.d/00yaourt, eth1/up
[ 2012-04-04 07:59:01 PM ]: nm-applet is running!
[ 2012-04-04 07:59:01 PM ]: system update in xterm as
[ 2012-04-04 07:59:01 PM ]: cannot run xterm

Offline

#2 2012-04-04 15:40:10

framas
Member
Registered: 2009-07-24
Posts: 50

Re: NetworkManager dispatcher script error

hi,

the following example script works for me.
What's important for your script:

* the  XAUTHORITY and DISPLAY variables must be set
* xterm must be launched in background (xterm &), since the network dispatcher wait's till xterm is closed
* You have no $USER variable set when the dispatcher executes the script so I don't think ~ is working. Better is to use logger or  "echo "some logging info" >> /absolute/path"


#!/bin/sh
logger  "$(date) ===================="  
                                                                                               
export DISPLAY=$(echo $DISPLAY | cut -c -2)

if [ -z $DISPLAY ]
then
	export DISPLAY=:0
fi

user=$(who | grep " $DISPLAY" | awk '{print $1}' | tail -n1)
export XAUTHORITY="/home/$user/.Xauthority"

echo "Display $DISPLAY user $user" >> /tmp/bla

su  $user -c "/usr/bin/xterm -e 'sudo ntpd -qdg && exit' &" || echo "cannot run xterm" >> /tmp/bla

Last edited by framas (2012-04-04 19:31:36)

Offline

#3 2012-04-05 13:26:19

comicosmos
Member
From: suzhou, China mainland
Registered: 2011-04-16
Posts: 45

Re: NetworkManager dispatcher script error

thanks very much, framas.
I learned a lot of thing.

and your script is by far more elegant.

After deal with a few small errors, finally it works!.

Last edited by comicosmos (2012-04-05 13:57:17)

Offline

#4 2012-04-09 13:29:54

comicosmos
Member
From: suzhou, China mainland
Registered: 2011-04-16
Posts: 45

Re: NetworkManager dispatcher script error

execuse me.
just one more problem.

I have to hardcode user name into the script, rather than setting the value dynamically.
If I use `who` to detect user name, I only get a null string, according to the log file.

The script:

#!/bin/sh                                                                                                      

IF=$1
STATUS=$2

if [ "$STATUS"x != 'up'x ];then
   exit
fi

logger  "==$0=="
wait_for_process(){
    PNAME=$1
    PID=`pgrep $PNAME`
    while [ -z "$PID" ];do
        logger "waiting $1 running for another 3 sec.."
        sleep 3;
        PID=`pgrep $PNAME`
    done
    logger "$1 is running!"
}
wait_for_process nm-applet

export DISPLAY=$(echo $DISPLAY | cut -c -2)

if [ -z $DISPLAY ];then
    export DISPLAY=:0
fi

user=$(who | grep "$DISPLAY" | awk '{print $1}' | tail -n1)
#the above argument will not yield correct value for user :(
#I have to hard code the user name, any idea?
user='xxx'
export XAUTHORITY="/home/$user/.Xauthority"

logger "Display $DISPLAY user $user"

su $user -c "xterm -e 'sudo /usr/bin/yaourt -Syua && exit' &" || (logger "cannot run xterm" && exit)

Last edited by comicosmos (2012-04-09 13:32:16)

Offline

Board footer

Powered by FluxBB