我编写了一个shell脚本,用于检查是否可以访问远程服务器。如果它是可访问的,它将通过ssh在不同的服务器上运行一个命令。之后,它执行一些rsync任务。
一切都很好,所以我写了一个plist文件,每4小时运行一次全体员工。
工作原理:
- 脚本按预期触发(我在stdout文件中看到它(
- launchctl启动myjob也很好(我在stdout文件中看到了它(
我的问题:
如果作业在macbook睡眠期间启动,则ping工作正常,但ssh命令不会返回。该命令在服务器上执行,但脚本在那里停止。如果我用ssh命令终止进程,脚本会很好地完成。
我能做些什么来获得更多信息。可能是什么错误?还有什么解决方案(Timemachine不是一个选项,因为我想要一个通用的可读备份(?
Mac OS BigSur 11.4 M1芯片
这里是我的plist文件
?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>de.stanetz.backup</string>
<key>Program</key>
<string>/usr/local/sbin/remoteBackup.nsh</string>
<key>RunAtLoad</key>
<false/>
<key>StandardOutPath</key>
<string>/var/log/launch-stdout.log</string>
<key>StandardErrorPath</key>
<string>/var/log/launch-stderr.log</string>
<key>Debug</key>
<true/>
<key>StartCalendarInterval</key>
<array>
<dict>
<key>Hour</key>
<integer>9</integer>
<key>Minute</key>
<integer>15</integer>
</dict>
</array>
<key>ExitTimeOut</key>
<integer>360</integer>
<key>TimeOut</key>
<integer>360</integer>
</dict>
</plist>
更新:我将ssh命令更改为ssh -vv pooh 'echo "this runs remotely"' >/tmp/so-debug.log 2>&1
。这很好用。如果我通过ssh调用debug1: Sending command: /usr/local/sbin/handleBackupPlatte.nsh open
(运行10秒(,完整的shell脚本就完成了,但我在日志中发现:
debug2: channel 0: send eof
debug2: channel 0: input drain -> closed
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 2 clearing O_NONBLOCK
Killed by signal 15.
远程脚本完成之前的持续时间似乎是个问题。
#!/bin/bash
echo "this runs remotely"
sleep 10
echo "for 10 seconds"
显示了效果。但我无法使脚本更快://除了将ssh命令发送到后台之外,还有什么想法吗?
更新2:如果mac book处于睡眠状态,似乎每一个需要10秒的网络呼叫都会挂起,并且永远不会完成。
我切换到另一种方法。我安装了睡眠观察程序,如果macbook被唤醒,它会发出通知。此外,我将其与一个脚本结合在一起,该脚本确保并非每次唤醒时都在运行备份。这里的脚本
#!/bin/bash
#set -x
set -eu
## RETURN_CODES
# 11 -> BACKUP_ALREADY_RUNNING
# 12 -> NIGHT -> No Backup
# 13 -> Last backup is too young.
#VARIABLES
LOCK_FILE="/tmp/.backup.lock"
LAST_SUCCESS_FILE="/var/log/.backup.last_success"
RUN_CONTROL="/var/log/throttle.log"
MIN_BACKUP_AGE=240
cleanup() {
exitCode=$?
date +"Exit Throttle at %d.%m.%Y %H:%M:%S with $exitCode" >> ${RUN_CONTROL}
if [[ "$exitCode" -ne 11 ]]; then
rm ${LOCK_FILE}
fi
echo "$(tail -n 100 ${RUN_CONTROL})" > ${RUN_CONTROL}
exit $exitCode
}
trap cleanup EXIT
test -e ${LOCK_FILE} && echo "${LOCK_FILE} exists" && exit 11
touch ${LOCK_FILE}
hour=$(date +%H)
if [ "$hour" -gt 22 ] || [ "$hour" -lt 7 ]; then
exit 12
fi
test -e "$(find ${LAST_SUCCESS_FILE} -mmin -${MIN_BACKUP_AGE})" && exit 13 || true
/usr/local/sbin/remoteBackup.nsh
echo "$(tail ${LAST_SUCCESS_FILE})" > ${LAST_SUCCESS_FILE}
date >> ${LAST_SUCCESS_FILE}
和/Library/LaunchDaemons/de.bernhard-baehr.sleepwatcher.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>de.bernhard-baehr.sleepwatcher</string>
<key>ProgramArguments</key>
<array>
<string>/opt/homebrew/sbin/sleepwatcher</string>
<string>-V</string>
<string>-w /usr/local/sbin/throttledBackup.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/var/log/sleepwatcher-stdout.log</string>
<key>StandardErrorPath</key>
<string>/var/log/sleepwatcher-stderr.log</string>
</dict>
</plist>