从cron作业中的shell脚本启动服务(upstart) [英] Starting a service (upstart) from a shell script, from a cron job
问题描述
我想使用cron作业来调用我编写的healthcheck脚本来检查我编写的网络应用程序(api)的状态(一个url调用不足以测试完整的功能,因此自定义健康检查)。 healthcheck应用程序有几个端点,它们从shell脚本(见下文)调用,这个脚本重新启动我们正在检查的更大的web应用程序。自然,我有麻烦。
I'm trying to use a cron job to call a healthcheck script I've written to check the status of a web app (api) I've written (a url call doesn't suffice to test full functionality, hence the custom healthcheck). The healthcheck app has several endpoints which are called from a shell script (see below), and this script restarts the bigger web app we are checking. Naturally, I'm having trouble.
它如何工作:
1)cron作业每60秒运行
2)healthcheck脚本由cron job
3)healthcheck脚本检查url,如果url返回非200响应,则停止并启动服务
How it works: 1) cron job runs every 60s 2) healthcheck script is run by cron job 3) healthcheck script checks url, if url returns non-200 response, it stops and start a service
有什么作用:
1)我可以运行脚本(healthcheck.sh)作为ec2-user
2)我可以运行脚本作为root
3)cron作业调用脚本,它运行,但它不停止/启动服务(我可以通过观察/tmp/crontest.txt和ps aux看到这个)。
What works: 1) I can run the script (healthcheck.sh) as the ec2-user 2) I can run the script as root 3) The cron job calls the script and it runs, but it doesn't stop/start the service (I can see this by watching /tmp/crontest.txt and ps aux).
它看起来像一个权限问题或一些非常基本的linux东西
It totally seems like a permissions issue or some very basic linux thing that I'm not aware of.
当我以root或ec2-user身份运行时的日志(/ tmp / crontest .txt):
The log when I run as root or ec2-user (/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作业运行时的日志:
The log when the cron job runs:
Fri Nov 23 00:27:01 UTC 2012
healthcheck.sh: api not running, restarting service! <--- no restart
Cron文件(在/etc/cron.d):
Cron file (in /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调用的端点:
Upstart script (/etc/init/healthcheck.conf)- this is for the healthcheck app, which provides endpoints which we call from the shell script 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脚本权限:
Shell script permissions:
-rwxr-xr-x 1 ec2-user ec2-user 529 Nov 23 00:16 /srv/checkout/healthcheck/healthcheck.sh
Shell脚本(healthcheck.sh):
Shell script (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
推荐答案
为脚本添加开始/停止命令的路径:
Add path to start/stop command to your script:
#!/bin/bash
PATH=$PATH:/sbin/
命令:
/sbin/stop api
您可以使用whereis查看他们的路径:
you can check path to them using whereis:
$ whereis start
/sbin/start
这篇关于从cron作业中的shell脚本启动服务(upstart)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!