从cron作业中的shell脚本启动服务(upstart)

编程入门 行业动态 更新时间:2024-10-15 22:31:18
本文介绍了从cron作业中的shell脚本启动服务(upstart)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用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 (upstart.ubuntu/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="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)

本文发布于:2023-11-28 18:16:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643445.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:作业   脚本   cron   upstart   shell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!