【发布时间】:2012-11-11 09:17:35
【问题描述】:
我正在尝试使用 cron 作业来调用我编写的运行状况检查脚本来检查我编写的 Web 应用程序 (api) 的状态(url 调用不足以测试完整的功能,因此自定义健康检查)。健康检查应用程序有几个从 shell 脚本调用的端点(见下文),这个脚本重新启动我们正在检查的更大的网络应用程序。当然,我遇到了麻烦。
它是如何工作的: 1) cron 作业每 60 秒运行一次 2)健康检查脚本由cron作业运行 3) healthcheck 脚本检查 url,如果 url 返回非 200 响应,则停止并启动服务
什么有效: 1) 我可以以 ec2 用户身份运行脚本 (healthcheck.sh) 2)我可以以root身份运行脚本 3) cron 作业调用脚本并运行,但它不会停止/启动服务(我可以通过观看 /tmp/crontest.txt 和 ps aux 看到这一点)。
这似乎完全是权限问题或我不知道的一些非常基本的 linux 问题。
我以 root 或 ec2 用户运行时的日志 (/tmp/crontest.txt):
Fri Nov 23 00:28:54 UTC 2012
healthcheck.sh: api not running, restarting service!
api start/running, process 1939 <--- it restarts the service properly!
cron作业运行时的日志:
Fri Nov 23 00:27:01 UTC 2012
healthcheck.sh: api not running, restarting service! <--- no restart
Cron 文件(在 /etc/cron.d 中):
# Call the healthcheck every 60s
* * * * * root /srv/checkout/healthcheck/healthcheck.sh >> /tmp/crontest.txt
Upstart 脚本 (/etc/init/healthcheck.conf) - 这是用于 healthcheck 应用程序的,它提供了我们从 shell 脚本 healthcheck.sh 调用的端点:
#/etc/init/healthcheck.conf
description "healthcheck"
author "me"
env USER=ec2-user
start on started network
stop on stopping network
script
# We run our process as a non-root user
# Upstart user guide, 11.43.2 (http://upstart.ubuntu.com/cookbook/#run-a-job-as-a-different-user)
exec su -s /bin/sh -c "NODE_ENV=production /usr/local/bin/node /srv/checkout/healthcheck/app.js" $USER
end script
Shell 脚本权限:
-rwxr-xr-x 1 ec2-user ec2-user 529 Nov 23 00:16 /srv/checkout/healthcheck/healthcheck.sh
Shell 脚本(healthcheck.sh):
#!/bin/bash
API_URL="http://localhost:4567/api"
echo `date`
status_code=`curl -s -o /dev/null -I -w "%{http_code}" $API_URL`
if [ 200 -ne $status_code ]; then
echo "healthcheck.sh: api not running, restarting service!"
stop api
start api
fi
【问题讨论】:
标签: linux shell cron centos upstart