我有下面的脚本,当从命令行运行时它似乎工作得很好/root/.scripts/peter-laptop-backup-launcher.sh daily
(它显示一个 xmessage 询问我想要做什么),但是当它通过 anacron 运行时,它的行为有所不同,说显示了 xmessage 并且用户单击了角落的“x”,而实际上它似乎根本没有显示。
从命令行运行时,它会显示消息并根据用户的选择正确采取行动。
剧本:
#!/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
这是 /etc/anacrontab 中的条目
1 4 rsnapshot.daily /root/.scripts/peter-laptop-backup-launcher.sh daily > /var/log/backup/rsnapshot.daily.log
这是 /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
这是从命令行运行时终端中的输出:
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}
该脚本的目的是每天启动我的 rsnapshot 备份,但如果我用于存储备份的 NAS 未打开并安装,它会询问我想要做什么,让我有机会在继续之前将其打开。如果我不在身边回复消息,它会稍后再试一次。
答案1
您的xmessage
无法显示,因为anacron
没有权限写入您的 X 显示器;出错时返回 1;来自手册页:
EXIT STATUS
If it detects an error, xmessage returns 1, so this value should not be
used with a button.
你可能需要设置DISPLAY=:0
为允许里面的 X 命令anacron
访问您的服务器。
答案2
我终于在以下的帮助下完成了这项工作此主题。
修正后的脚本为:
#!/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
关键是这一行xauth merge /var/lib/kdm/:0.Xauth
,它将当前“x”用户的授权复制到他的显示中到此用户的 Xauthority 文件中。
以前的解决方案是通过以 root 身份运行的命令行来工作的,但是当脚本由 anacron(也以 root 身份运行)运行时则不行。这个解决了这个问题,现在一切都完美了。