Bash 使Web服务器进程保持活动状态的终端应用程序

Bash 使Web服务器进程保持活动状态的终端应用程序,bash,terminal,zsh,cron,wget,curl,Bash,Terminal,Zsh,Cron,Wget,Curl,是否有一个应用程序可以在给定命令和选项的情况下,在进程的生命周期内执行,并在特定的时间间隔内无限期地ping给定的URL 如果没有,是否可以在终端上以bash脚本的形式执行此操作?我几乎可以肯定它可以通过终端完成,但我不够流利,无法在几分钟内完成 找到的具有部分解决方案,减去ping位ping在linux上无限期运行;直到它被主动杀死。在bash中,比如说,两次ping之后,我如何杀死它?假设您想使用wget开始下载,并且在它运行时,ping url: wget http://example.

是否有一个应用程序可以在给定命令和选项的情况下,在进程的生命周期内执行,并在特定的时间间隔内无限期地ping给定的URL

如果没有,是否可以在终端上以
bash脚本的形式执行此操作?我几乎可以肯定它可以通过终端完成,但我不够流利,无法在几分钟内完成



找到的具有部分解决方案,减去
ping
ping
在linux上无限期运行;直到它被主动杀死。在bash中,比如说,两次ping之后,我如何杀死它?

假设您想使用
wget
开始下载,并且在它运行时,ping url:

wget http://example.com/large_file.tgz & #put in background
pid=$!
while kill -s 0 $pid #test if process is running
do
    ping -c 1 127.0.0.1 #ping your adress once
    sleep 5 #and sleep for 5 seconds
done
总纲 正如其他人所建议的,在伪代码中使用:

  • 执行命令并保存
    PID
  • PID
    处于活动状态时,ping和sleep
  • 出口
  • 这将产生以下脚本:

    #!/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
    
    ping两次 要仅ping几次,请使用
    -c
    选项:

    ping -c <count here> <address here>
    
    有关详细信息,请使用
    manping

    更好的实践 如以下评论所述,目前的解决方案可能不足以解决问题。在回答这个问题时,核心问题仍然存在。为了解决这个问题,建议使用一个作业,在该作业中定期发送一个简单或http请求。这将生成一个相当简单的脚本,其中只包含一行:

    #!/bin/bash
    curl <address here> > /dev/null 2>&1
    
    #/bin/bash
    
    卷曲如果您希望了解有关如何设置此类计划作业的更多信息,请留下评论。特别感谢您分析问题并提出合理的解决方案。

    这是一个很好的通用工具。其相关选择:

    Usage: daemonize [OPTIONS] path [arg] ...
    
    -c <dir>       # Set daemon's working directory to <dir>.
    -E var=value   # Pass environment setting to daemon. May appear multiple times.
    -p <pidfile>   # Save PID to <pidfile>.
    -u <user>      # Run daemon as user <user>. Requires invocation as root.
    -l <lockfile>  # Single-instance checking using lockfile <lockfile>.
    
    用法:守护[OPTIONS]路径[arg]。。。
    -c#将守护进程的工作目录设置为。
    -E var=value#将环境设置传递给守护进程。可能出现多次。
    -p#将PID保存到。
    -u#以用户身份运行守护程序。需要作为根进行调用。
    -l#使用lockfile进行单实例检查。
    
    以下是使用中启动/终止的示例:


    为了变得更复杂,你可以把你的ping脚本变成许多最近的Linux上的标准。

    你熟悉
    while
    循环以及如何
    在程序/脚本中睡眠吗?是的,javascript
    设置间隔
    设置超时
    c
    线程。睡眠
    …没有意义。为什么要这样做?我强烈建议您使用存储在
    $中的
    PID
    而不是命令的名称。因为使用管道会失败。更好的是,我喜欢使用
    kill
    的方法!当主机关闭时,ping将永远挂起。(在大多数情况下)。Wget已经在从该服务器生成流量-那么为什么ping在循环中?@hek2mgl为什么你一直在问这个脚本的目的是什么,你应该在评论他的问题时问OP。此脚本的目的是回答OP的问题并提供他/她所需的功能。当我从终端执行
    ping[host]
    时,它将无限期执行。我只希望它执行一次(或两次),然后在整个sleep命令期间停止。您将如何“停止”ping
    ?此脚本的用途是什么?@cultureanomoly仅对少数ping,请使用
    ping-c
    。看看我的答案。
    ping
    在这里有什么帮助<代码>ping
    不会产生
    http
    流量。只需在cronjob中执行
    wget
    ,您就完成了。更好的做法是配置“服务器场”,使网站保持简单的活动状态。@这取决于是什么导致服务器死亡,如果有任何传入请求阻止服务器死亡,那么这将起作用。如果没有按照hek2mgl的建议或与hek2mgl的建议进行工作。-我会在答覆中详述这点。
    #!/bin/bash
    curl <address here> > /dev/null 2>&1
    
    Usage: daemonize [OPTIONS] path [arg] ...
    
    -c <dir>       # Set daemon's working directory to <dir>.
    -E var=value   # Pass environment setting to daemon. May appear multiple times.
    -p <pidfile>   # Save PID to <pidfile>.
    -u <user>      # Run daemon as user <user>. Requires invocation as root.
    -l <lockfile>  # Single-instance checking using lockfile <lockfile>.