使 Web 服务器进程保持活动状态的终端应用程序
Terminal Application to Keep Web Server Process Alive
是否有应用程序可以在给定命令和选项的情况下在进程的生命周期内执行并在特定时间间隔无限期地 ping 给定 URL?
如果没有,是否可以在终端上以 bash script
的形式完成?我几乎肯定它可以通过终端实现,但我不够流利,无法在几分钟内启动它。
找到 this post 的部分解决方案,减去 ping
位。 ping
无限期地在 linux 上运行;直到它被主动杀死。我如何在两次 ping 之后从 bash 杀死它?
假设你想用 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 和睡眠
- 退出
这导致以下脚本:
#!/bin/bash
# execute command, use '&' at the end to run in background
<command here> &
# store pid
pid=$!
while ps | awk '{ print }' | grep $pid; do
ping <address here>
sleep <timeout here in seconds>
done
请注意,<>
中的内容应替换为实际内容。无论是命令还是 IP 地址。
跳出循环
要回答你的第二个问题,这取决于循环。在上面的循环中,只需使用变量跟踪循环计数。为此,在循环内添加一个 ((count++))
。并这样做:[[ $count -eq 2 ]] && break
。现在,当我们第二次 ping 时,循环将中断。
像这样:
...
while ...; do
...
((count++))
[[ $count -eq 2 ]] && break
done
ping 两次
要仅 ping 几次,请使用 -c
选项:
ping -c <count here> <address here>
示例:
ping -c 2 www.google.com
使用 man ping
获取更多信息。
更好的做法
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 请求。这导致一个相当简单的脚本只包含一行:
#!/bin/bash
curl <address here> > /dev/null 2>&1
可以将此脚本添加为 cron job. Leave a comment if you desire more information how to set such a scheduled job. Special thanks to hek2mgl 以分析问题并提出合理的解决方案。
一个不错的通用小工具是 Daemonize。其相关选项:
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>.
下面是 starting/killing 的使用示例:flickd
为了更复杂,您可以将 ping 脚本变成 systemd service,现在是许多最新 Linux 的标准。
是否有应用程序可以在给定命令和选项的情况下在进程的生命周期内执行并在特定时间间隔无限期地 ping 给定 URL?
如果没有,是否可以在终端上以 bash script
的形式完成?我几乎肯定它可以通过终端实现,但我不够流利,无法在几分钟内启动它。
找到 this post 的部分解决方案,减去 ping
位。 ping
无限期地在 linux 上运行;直到它被主动杀死。我如何在两次 ping 之后从 bash 杀死它?
假设你想用 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 和睡眠 - 退出
这导致以下脚本:
#!/bin/bash
# execute command, use '&' at the end to run in background
<command here> &
# store pid
pid=$!
while ps | awk '{ print }' | grep $pid; do
ping <address here>
sleep <timeout here in seconds>
done
请注意,<>
中的内容应替换为实际内容。无论是命令还是 IP 地址。
跳出循环
要回答你的第二个问题,这取决于循环。在上面的循环中,只需使用变量跟踪循环计数。为此,在循环内添加一个 ((count++))
。并这样做:[[ $count -eq 2 ]] && break
。现在,当我们第二次 ping 时,循环将中断。
像这样:
...
while ...; do
...
((count++))
[[ $count -eq 2 ]] && break
done
ping 两次
要仅 ping 几次,请使用 -c
选项:
ping -c <count here> <address here>
示例:
ping -c 2 www.google.com
使用 man ping
获取更多信息。
更好的做法
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 请求。这导致一个相当简单的脚本只包含一行:
#!/bin/bash
curl <address here> > /dev/null 2>&1
可以将此脚本添加为 cron job. Leave a comment if you desire more information how to set such a scheduled job. Special thanks to hek2mgl 以分析问题并提出合理的解决方案。
一个不错的通用小工具是 Daemonize。其相关选项:
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>.
下面是 starting/killing 的使用示例:flickd
为了更复杂,您可以将 ping 脚本变成 systemd service,现在是许多最新 Linux 的标准。