LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   script root notify-send for current X11 user. (https://www.linuxquestions.org/questions/linux-software-2/script-root-notify-send-for-current-x11-user-4175420008/)

igor012 08-02-2012 11:48 AM

script root notify-send for current X11 user.
 
Hello,
I created a script that checks the quotas of the disk and display a notification when the quota is reached. It works well.
I want to have this script executed by the root user.

here is the script
Code:

TIMEOUT="12000"
ICON_WARN="/usr/share/pixmaps/warning.png"
ICON_INFO="/usr/share/pixmaps/info.png"
XUSR=$(w | grep -w :0 | awk '{ print $1 }') #get the current X11 user

#test if the current is root or empty and stops if so.
if [ -z $XUSR ] ; then
        exit 0
elif [ $XUSR = "root" ] ; then
        exit 0
fi

getquota () {
        quota -s -w -u $XUSR | grep homeserv | sed -r 's/ +/ /g'| cut -d " " -f "$1" | \
        sed -r 's/M/ /g' | sed -r 's/\*//g'
}

#GET HOME SIZE
HME=$(getquota 2)

# #GET SOFTWARE LIMIT
SLIMIT=$(getquota 3)

# #GET HARDWARE LIMIT
HLIMIT=$(getquota 4)

#TEST
#Test if the limit is reached or not
if [ ${HME} -eq ${HLIMIT} ]; then
        DISPLAY=:0.0 /usr/bin/notify-send "WARNING ! ${XUSR} Your HOME directory is full."\
        "It has reached the hardware limit (${HLIMIT}MB). Please clean up now ! "\
        -i ${ICON_WARN} -t ${TIMEOUT}
elif [ ${HME} -ge ${SLIMIT} ]; then
        DISPLAY=:0.0 /usr/bin/notify-send "WARNING ! ${XUSR}  Your HOME directory is almost full: ${HME}MB"\
        "It has reached the software limit (${SLIMIT}MB). Please clean up !"\
        -i ${ICON_WARN} -t ${TIMEOUT}
      -i ${ICON_INFO} -t ${TIMEOUT}
fi

Where is the problem ?
Thank you

YankeePride13 08-02-2012 12:37 PM

Quote:

Originally Posted by igor012 (Post 4744256)
Hello,
Code:

#test if the current is root or empty and stops if so.
if [ -z $XUSR ] ; then
        exit 0
elif [ $XUSR = "root" ] ; then
        exit 0
fi


Not 100% sure what the question means, but here you have it set to exit when you are root. Also, if you are logging in as a non-root user and then 'sudo -s'ing or 'su -'ing, the return value of w will be the user you had logged in as, not root.

igor012 08-03-2012 03:19 AM

Hello,
I didn't explain enough. This script is not correctly executed as root user.
Code:

#test if the current is root or empty and stops if so.
if [ -z $XUSR ] ; then
        exit 0
elif [ $XUSR = "root" ] ; then
        exit 0
fi

Just checks if the current x11 user is root or not.

igor012 08-03-2012 04:25 AM

I also added a file creation in the test to make sure it's been executed. I ran it under root and it worked but no sign of a notification.
Code:


if [ ${HME} -eq ${HLIMIT} ]; then
        DISPLAY=:0.0 /usr/bin/notify-send "WARNING ! ${XUSR} Your HOME directory is full."\
        "It has reached the hardware limit (${HLIMIT}MB). Please clean up now ! "\
        -i ${ICON_WARN} -t ${TIMEOUT}
      echo "1" >> /datas/overH
elif [ ${HME} -ge ${SLIMIT} ]; then
        DISPLAY=:0.0 /usr/bin/notify-send "WARNING ! ${XUSR}  Your HOME directory is almost full: ${HME}MB."\
        "It has reached the software limit (${SLIMIT}MB). Please clean up !"\
        -i ${ICON_WARN} -t ${TIMEOUT}
        echo "1" >> /datas/overS
else
        DISPLAY=:0.0 /usr/bin/notify-send "INFO ! ${XUSR} Your home directory: ${HME}MB"\
        -i ${ICON_INFO} -t ${TIMEOUT}
        echo "1" >> /datas/good
fi

So it seems related to the display.


All times are GMT -5. The time now is 07:07 PM.