cron 日志显示作业正在运行,但实际上作业失败了

问题描述 投票:0回答:1

我在

aws ec2 instance
有一个 cron 作业,定义为:

0 2 * * * /home/ec2-user/scripts/backup.sh
0 3 * * 0 /home/ec2-user/scripts/cleanup.sh
0 * * * * /home/ec2-user/scripts/health_check.sh

我检查了作业是否正在运行:

$ tail -f /var/log/cron
Jun  3 02:00:01 ip-172-31-0-123 CROND[1234]: (ec2-user) CMD (/home/ec2-user/scripts/backup.sh)
Jun  3 03:00:01 ip-172-31-0-123 CROND[1235]: (ec2-user) CMD (/home/ec2-user/scripts/cleanup.sh)
Jun  3 04:00:01 ip-172-31-0-123 CROND[1236]: (ec2-user) CMD (/home/ec2-user/scripts/health_check.sh)
Jun  3 05:00:01 ip-172-31-0-123 CROND[1237]: (ec2-user) CMD (/home/ec2-user/scripts/health_check.sh)

看起来作业正在运行。

但是在手动运行其中一项作业时,我发现该作业实际上失败并抛出错误:

$ /home/ec2-user/scripts/clean_up.sh
Traceback (most recent call last):
  File "/home/ec2-user/scripts/worker_script.py", line 15, in <module>
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
  File "/usr/local/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 360, in __init__
    self._impl = self._create_connection(parameters, _impl_class)
  File "/usr/local/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 451, in _create_connection
    raise self._reap_last_connection_workflow_error(error)
pika.exceptions.AMQPConnectionError: Connection to 127.0.0.1:5672 failed: [Errno 111] Connection refused

有什么方法可以知道作业是否运行成功?并相应地记录错误?

python linux bash amazon-ec2 cron
1个回答
0
投票

您可能想通过检查退出状态变量“$?”来捕获脚本中的错误。 像这样的东西:

command
if [ $? -ne 0 ]; then
  echo "command failed"
  exit 1
fi

此外,您还可以像这样记录脚本的输出:

0 2 * * * /home/ec2-user/scripts/backup.sh 2>&1 | logger -t cron_backup
0 3 * * 0 /home/ec2-user/scripts/cleanup.sh 2>&1 | logger -t cron_cleanup
0 * * * * /home/ec2-user/scripts/health_check.sh 2>&1 | logger -t cron_HC

“cron_backup/cron_cleanup/cron_HC”可以替换为系统日志中您想要的任何标签。这样,脚本的输出(stdout/stderr)将记录在“syslog”中。

© www.soinside.com 2019 - 2024. All rights reserved.