如何将这 2 个脚本和 运行 多个 cron 作业结合起来用于同一脚本以获得不同的输出?
How to combine these 2 scripts and run multiple cron jobs for the same script to get different outputs?
第一个脚本每天上午 11 点运行一次,并通过“成功”消息通知一切正常。
if [ $Status == "200" ]; then
echo "success"
else
echo "error"
fi
第二个脚本每 30 分钟运行一次,除非出现错误,否则不会打印任何内容。基本上它会每 30 分钟检查一次是否一切正常,并且只有在出现故障时才通知我错误。
if [ $Status != "200" ]; then
echo "error"
fi
这两个脚本的代码几乎完全相同。但是由于我在不同时间需要不同的输出,所以我使用了 2 个不同的脚本。那么有没有办法结合这两个脚本?
更清楚地说,如果可能的话,我想要一个脚本,它可以每天上午 11 点发送一次成功消息,同时还可以 运行 每 30 分钟发送一次成功消息,而不会一次又一次地发送成功消息,然后只发送错误消息如果有任何问题,请留言。
在 crontab 中是这样的:
0 11 * * * /home/username/script1.sh
*/30 * * * * /home/username/script2.sh
每 30 分钟编写一次脚本 运行。成功时,检查是否是上午11点再输出成功。
if [ $Status == "200" ]; then
if [ $(date +%H%M) == "1100" ]; then
echo "success"
fi
else
echo "error"
fi
注意:这会检查 11:00 上午。如果您的脚本可能需要超过 1 分钟才能到达这一点,请将时间保存在开头的某处 time="$(date +%H%M)"
并在之后检查 if [ "$time" == "1100" ]
.
或者,如果您想在脚本之外保留实际行为不同的时间,您可以引入一个仅在上午 11 点(或以一种方式)设置的参数 运行,但是不(或以另一种方式)在另一个 运行s.
这要求您仍然有多个 cron 作业,但对同一脚本使用不同的参数。
0 11 * * * /home/username/script.sh yes
30 11 * * * /home/username/script.sh no
*/30 0-10,12-23 * * * /home/username/script.sh no
在脚本中您需要检查提供的参数。
printsuccess=""
if [ $Status == "200" ]; then
if [ $printsuccess == "yes" ]; then
echo "success"
fi
else
echo "error"
fi
Both the scripts have almost exactly the same code. But I'm using 2 different scripts because of different outputs I want at different times. So is there a way to combine both the scripts?
您应该将获取状态代码的所有复杂性放在一个脚本中。如果需要,在包装器脚本中获取它:. ~/get-code.bash
.
而不是包装脚本,我会这样做:
0,30 0-10,12-23 * * * ~/get-code || echo error
30 11 * * * ~/get-code || echo error
0 11 * * * if ~/get-code; then echo success; else echo error; fi
为了return成功或失败基于Status
,把这个放在~/get-code
的末尾:
if [ "$Status" = 200 ]; then
exit 0
else
exit 1
fi
第一个脚本每天上午 11 点运行一次,并通过“成功”消息通知一切正常。
if [ $Status == "200" ]; then
echo "success"
else
echo "error"
fi
第二个脚本每 30 分钟运行一次,除非出现错误,否则不会打印任何内容。基本上它会每 30 分钟检查一次是否一切正常,并且只有在出现故障时才通知我错误。
if [ $Status != "200" ]; then
echo "error"
fi
这两个脚本的代码几乎完全相同。但是由于我在不同时间需要不同的输出,所以我使用了 2 个不同的脚本。那么有没有办法结合这两个脚本?
更清楚地说,如果可能的话,我想要一个脚本,它可以每天上午 11 点发送一次成功消息,同时还可以 运行 每 30 分钟发送一次成功消息,而不会一次又一次地发送成功消息,然后只发送错误消息如果有任何问题,请留言。
在 crontab 中是这样的:
0 11 * * * /home/username/script1.sh
*/30 * * * * /home/username/script2.sh
每 30 分钟编写一次脚本 运行。成功时,检查是否是上午11点再输出成功。
if [ $Status == "200" ]; then
if [ $(date +%H%M) == "1100" ]; then
echo "success"
fi
else
echo "error"
fi
注意:这会检查 11:00 上午。如果您的脚本可能需要超过 1 分钟才能到达这一点,请将时间保存在开头的某处 time="$(date +%H%M)"
并在之后检查 if [ "$time" == "1100" ]
.
或者,如果您想在脚本之外保留实际行为不同的时间,您可以引入一个仅在上午 11 点(或以一种方式)设置的参数 运行,但是不(或以另一种方式)在另一个 运行s.
这要求您仍然有多个 cron 作业,但对同一脚本使用不同的参数。
0 11 * * * /home/username/script.sh yes
30 11 * * * /home/username/script.sh no
*/30 0-10,12-23 * * * /home/username/script.sh no
在脚本中您需要检查提供的参数。
printsuccess=""
if [ $Status == "200" ]; then
if [ $printsuccess == "yes" ]; then
echo "success"
fi
else
echo "error"
fi
Both the scripts have almost exactly the same code. But I'm using 2 different scripts because of different outputs I want at different times. So is there a way to combine both the scripts?
您应该将获取状态代码的所有复杂性放在一个脚本中。如果需要,在包装器脚本中获取它:. ~/get-code.bash
.
而不是包装脚本,我会这样做:
0,30 0-10,12-23 * * * ~/get-code || echo error
30 11 * * * ~/get-code || echo error
0 11 * * * if ~/get-code; then echo success; else echo error; fi
为了return成功或失败基于Status
,把这个放在~/get-code
的末尾:
if [ "$Status" = 200 ]; then
exit 0
else
exit 1
fi