终端应用程序,以保持Web服务器进程活着 [英] Terminal Application to Keep Web Server Process Alive

查看:185
本文介绍了终端应用程序,以保持Web服务器进程活着的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有可能,给定一个命令和选项的应用程序,无限期地执行该进程的寿命和ping一个给定的URL在一个特定的时间间隔?

Is there an app that can, given a command and options, execute for the lifetime of the process and ping a given URL indefinitely on a specific interval?

如果不是,这哪是在终端上做一个 bash脚本?我几乎可以肯定的是,通过终端可行的,但我不足够流利几分钟内掀起不起来。

If not, could this be done on the terminal as a bash script? I'm almost positive it's doable through terminal, but am not fluent enough to whip it up within a few minutes.

这个帖子拥有的部分解决方案,减去位。 运行在Linux,无限期;直到它的积极杀害。我怎么会杀了它之后庆典比方说,2个ping?

Found this post that has a portion of the solution, minus the ping bits. ping runs on linux, indefinitely; until it's actively killed. How would I kill it from bash after say, two pings?

推荐答案

正如其他人所建议的,在伪code使用:

General Script

As others have suggested, use this in pseudo code:


  1. 执行命令并保存 PID

  2. ,而 PID 是积极的,ping和睡眠

  3. 退出

  1. execute command and save PID
  2. while PID is active, ping and sleep
  3. exit

这将导致以下脚本:

#!/bin/bash

# execute command, use '&' at the end to run in background
<command here> &

# store pid
pid=$!

while ps | awk '{ print $1 }' | grep $pid; do
    ping <address here>
    sleep <timeout here in seconds>
done

请注意这里面&LT的东西;&GT; 应与实际的东西替换。无论是命令或IP地址。

Note that the stuff inside <> should be replaces with actual stuff. Be it a command or an ip address.

要回答你的第二个问题,这取决于在循环。在上面的循环,只是跟踪使用可变循环计数。要做到这一点,在循环内部添加((计数++))。而做到这一点: [$算-eq 2]和放大器;&安培;突破。现在,环路将突破当我们执行ping一秒钟的时间。

To answer your second question, that depends in the loop. In the loop above, simply track the loop count using a variable. To do that, add a ((count++)) inside the loop. And do this: [[ $count -eq 2 ]] && break. Now the loop will break when we're pinging for a second time.

事情是这样的:

...
while ...; do
    ...
    ((count++))
    [[ $count -eq 2 ]] && break
done

平两次

要ping只有几次,使用 -c 选项:

ping -c <count here> <address here>

例如:

ping -c 2 www.google.com

使用人平了解更多信息。

正如 hek2mgl 在下面评论指出,当前的解决方案可能不足以解决问题。在回答这个问题,核心问题将仍然存在。为了帮助这一问题,一个的cron作业的问题建议在一个简单的 wget的或的卷曲 HTTP请求定期发送相对=标签,类=标签后称号=节目的问题。这导致包含一个相当简单的脚本,但一行:

As hek2mgl noted in a comment below, the current solution may not suffice to solve the problem. While answering the question, the core problem will still persist. To aid to that problem, a cron job is suggested in which a simple wget or curl http request is sent periodically. This results in a fairly easy script containing but one line:

#!/bin/bash
curl <address here> > /dev/null 2>&1

该脚本可以被添加为 cron作业。发表评论,如果您希望了解更多信息如何设置这样的计划作业。特别感谢 hek2mgl 分析问题并提出完善的解决方案。

This script can be added as a cron job. Leave a comment if you desire more information how to set such a scheduled job. Special thanks to hek2mgl for analyzing the problem and suggesting a sound solution.

这篇关于终端应用程序,以保持Web服务器进程活着的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆