Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have the script below, which seem to work perfectly when run from the commandline with /root/.scripts/peter-laptop-backup-launcher.sh daily (it displays an xmessage asking what I want to do), but when it's run via anacron, it behaves differently, saying that the xmessage was displayed and the user clicked the 'x' in the corner, when actually it doesn't seem to have been displayed at all.

When run from the commandline, it displays the message and acts on the user's choice correctly.

The script:

#!/bin/sh

# Script to launch the appropriate Backup command to backup My laptop, on condition that the drive is mounted.

type=$1
action="102"
ip="192.168.0.5"

# Set the display
DISPLAY=peter-aspire-ubuntu:0
export DISPLAY
cp /home/peter/.Xauthority /root/.Xauthority

while [ $action -ne 101 ]
do
        echo "Begin loop"

        echo "Perform ping"
        ping -c 1 $ip

        if [ $? -eq 0 ] ; then

                echo "Machine is giving ping response"
                mount /mnt/NAS > /dev/null 2>&1;

        else
                echo "Machine is not responding to pings"

        fi

        if grep -qs '/mnt/NAS' /proc/mounts; then

                # It's mounted so carry on.
                echo "NAS is mounted"

                if [ "$type" = "daily" ]; then

                        echo "Performing daily backup"
                        nice -n 17 rsnapshot daily
                        echo "Daily backup finished"

                        ## 
                        ##  FINISHED - SWITCH OFF
                        ## 

                        umount -l /mnt/NAS
                        echo "NAS unmounted"
                        ssh root@nas 'poweroff -pih';
                        echo "NAS powered down"
                else
                        echo "Performing $type backup"
                        rsnapshot $type
                        echo "$type backup finished"
                fi

                action="101"

        else

                # It's not mounted so...
                  # 101 = "Cancel"
                  # 102 = "Try again"
                  # 1   = User clicked the 'X'
                  # 0   = Timeout occured

                echo "Displaying xmessage"

                xmessage -buttons "Cancel","Try again","Snooze 30min","Snooze 1hr" -default "Try again" -center -timeout 1200 "The NAS is not mounted and your $type backup is due to run."

                action=$?
                echo "xmessage signal was = $action"

                if [ $action -eq 0 ] ; then
                        echo "User clicked 'Try again'"
                        action="102"
                fi

                if [ $action -eq 1 ] ; then
                        echo "User clicked 'x'"
                        action="101"
                fi

                if [ $action -eq 103 ] ; then
                        echo "Snooze for 30m"
                        sleep 30m
                        # Set "Try again"
                        action="102"
                fi

                if [ $action -eq 104 ] ; then
                        echo "Snooze for 1hr"
                        sleep 1hr
                        action="102"
                fi

                echo "final adjusted = $action"

        fi
        echo "End of loop"
done

This is the entry in /etc/anacrontab

1       4       rsnapshot.daily /root/.scripts/peter-laptop-backup-launcher.sh daily > /var/log/backup/rsnapshot.daily.log

This is what is logged in /rsnapshot.daily.log

Begin loop
Perform ping
PING 192.168.0.5 (192.168.0.5) 56(84) bytes of data.
From 192.168.0.8 icmp_seq=1 Destination Host Unreachable

--- 192.168.0.5 ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms

Machine is not responding to pings
Displaying xmessage
xmessage signal was = 1
User clicked 'x'
final adjusted = 101
End of loop

This is the output in the terminal when running it from the commandline:

Begin loop                                                                                                     
Perform ping                                                                                                   
PING 192.168.0.5 (192.168.0.5) 56(84) bytes of data.                                                           
From 192.168.0.8 icmp_seq=1 Destination Host Unreachable                                                       

--- 192.168.0.5 ping statistics ---                                                                            
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms                                       

Machine is not responding to pings                                                                             
Displaying xmessage
{after this the result depends on what you choose and behaves as expected}

The purpose of the script is to launch my rsnapshot backup each day, but if my NAS which I use for storing backups on is not switched on and mounted, it asks me what I want to do, giving me the chance to switch it on before continuing. If I'm not around to respond to the message, it will try again later.

share|improve this question

2 Answers 2

Your xmessage could not be displayed as anacron has no permission to write to your X display; returns 1 on error; from the manpage:

EXIT STATUS
       If it detects an error, xmessage returns 1, so this value should not be
       used with a button.

You possibly need to set DISPLAY=:0 to allow X commands inside anacron to access your server.

share|improve this answer
    
Ah ok. Well done. I missed that error code in the man page.However, I am already setting the DISPLAY value to the name of my display but replaced that with your suggestion, but no change. It still works from the commandline as root, even I'm logged in as me in 'X' but from Anacron, it logs that the message was displayed but '1' was returned by xmessage. –  Peter Snow May 27 '13 at 4:30
up vote 0 down vote accepted

I've finally got this working with the help of this thread.

The corrected script is:

#!/bin/sh                                                                                                      

# Script to launch the appropriate Backup command to backup My laptop, on condition that the drive is mounted. 

type=$1                                                                                                        
action="102"                                                                                                   
ip="192.168.0.5"                                                                                               

# Set the display                                                                                              
export DISPLAY=:0                                                                                              
xauth merge /var/lib/kdm/:0.Xauth                                                                              

while [ $action -ne 101 ]                                                                                      
do                                                                                                             
        echo "Begin loop"                                                                                      
        date                                                                                                   

        echo "Perform ping"                                                                                    
        ping -c 1 $ip                                                                                          

        if [ $? -eq 0 ] ; then                                                                                 

                echo "Machine is giving ping response"                                                         
                mount /mnt/NAS > /dev/null 2>&1;                                                               

        else                                                                                                   
                echo "Machine is not responding to pings"

        fi

        if grep -qs '/mnt/NAS' /proc/mounts; then

                # It's mounted so carry on.
                echo "NAS is mounted"

                if [ "$type" = "daily" ]; then

                        echo "Performing daily backup"
                        nice -n 17 rsnapshot daily
                        echo "Daily backup finished"

                        ## 
                        ##  FINISHED - SWITCH OFF
                        ## 

                        umount -l /mnt/NAS
                        echo "NAS unmounted"
                        ssh root@nas 'poweroff -pih';
                        echo "NAS powered down"
                else
                        echo "Performing $type backup"
                        rsnapshot $type
                        echo "$type backup finished"
                fi

                action="101"

        else

                # It's not mounted so...
                  # 101 = "Cancel"
                  # 102 = "Try again"
                  # 1   = User clicked the 'X'
                  # 0   = Timeout occured

                echo "Displaying xmessage"

                xmessage -buttons "Cancel","Try again","Snooze 30min","Snooze 1hr" -default "Try again" -center -timeout 1200 -display :0 "The NAS is not mounted and your $type backup is due to run."

                action=$?
                echo "xmessage signal was = $action"

                if [ $action -eq 0 ] ; then
                        echo "User clicked 'Try again'"
                        action="102"

                elif [ $action -eq 1 ] ; then
                        echo "User clicked 'x'"
                        action="101"

                elif [ $action -eq 103 ] ; then
                        echo "Snooze for 30m"
                        sleep 30m
                        # Set "Try again"
                        action="102"

                elif [ $action -eq 104 ] ; then
                        echo "Snooze for 1hr"
                        sleep 1hr
                        action="102"
                fi

                echo "final adjusted = $action"

        fi
        echo "End of loop"
done

The key was this line xauth merge /var/lib/kdm/:0.Xauth which copies the current 'x' users authorisation to write to his display into THIS users Xauthority file.

The previous solutions were working from a command-line run as root, but not when the script was run by anacron (also running as root).This solves that and it all works perfectly now.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.