如何在 Bash 脚本中包含计时器? [英] How to include a timer in Bash Scripting?

查看:10
本文介绍了如何在 Bash 脚本中包含计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!有没有办法在使用 bash 的脚本中包含计时器(时间戳?或任何术语)?比如说;每 60 秒,一个特定的功能会检查互联网是否关闭,如果是,则它会连接到 wifi 设备,反之亦然.简而言之,该程序会不时检查互联网连接.

Good day! Is there any way to include a timer (timestamp?or whatever term it is) in a script using bash? Like for instance; every 60 seconds, a specific function checks if the internet is down, if it is, then it connects to the wifi device instead and vice versa. In short, the program checks the internet connection from time to time.

任何建议/答案将不胜感激.=)

Any suggestions/answers will be much appreciated. =)

推荐答案

钝版

while sleep 60; do
  if ! check_internet; then
    if is_wifi; then
       set_wired
    else
       set_wifi
    fi
  fi
done

将睡眠本身用作循环条件允许您通过终止睡眠来跳出循环(即,如果它是前台进程,则 ctrl-c 会这样做).

Using the sleep itself as loop condition allows you to break out of the loop by killing the sleep (i.e. if it's a foreground process, ctrl-c will do).

正如 Montecristo 所指出的,如果我们谈论的是几分钟或几小时的间隔,cron 可能会做得更好.

If we're talking minutes or hours intervals, cron will probably do a better job, as Montecristo pointed out.

这篇关于如何在 Bash 脚本中包含计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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