【发布时间】:2021-06-06 13:31:28
【问题描述】:
在 Linux 系统上,我正在运行运行良好的 bash 脚本,但现在我想在 AIX 系统上使用相同的脚本。
只有一小部分无法运行,因为在 AIX 系统上“date -d”命令不起作用。
下面是在 linux 服务器上运行的代码,在这种情况下基于主机名 system1 和 system2 脚本将仅在给定时间的星期日运行。
declare -A myservertime
myservertime["system1"]="02:00"
myservertime["system2"]="02:10"
for keys in "${!myservertime[@]}";
do
if [[ "$keys" == "$HOSTNAME" ]]; then
mylaunchtime=${myservertime["$keys"]}
fi
done
if [ -z "$mylaunchtime" ]; then
echo "Server not found."
exit
fi
timenow=$(date +"%s")
weekday=$(date +"%a" -d @$timenow )
timerun=$(date +"%s" -d ${mylaunchtime} )
if [ $timerun -lt $timenow ]; then
timerun=$(( timerun + 86400 ))
fi
sleep_time=$(( timerun - timenow ))
#Check is weekday = Sunday
if [ $weekday == "Sun" ]; then
#If Sunday then wait until starttime is reached, until then sleep
sleep $sleep_time
#If starttime is reached execute startprogram
startprogram
fi
我已经修复了声明部分,这在 AIX 上也不起作用,如下:typeset -A myservertime=(["pvm00066"]="02:30" ["pvm00100"]="01:30")
但是我很难让变量 $weekday 和 $timerun 工作,因为那个“date -d $variable”
这里的任何人都可以帮助我,我尝试并搜索了所有类型的东西,但我无法找到一个工作。
【问题讨论】:
-
你能用
crontab或at吗? -
脚本本身开始使用 crontab。应用程序的停止和启动将在脚本本身中完成。这样我就不必使用脚本的多个版本,但我可以简单地在所有需要的 AIX 服务器上使用这个脚本。
-
我建议使用 perl,因为它可以跨平台使用并且可以进行日期数学运算。