如何包括bash脚本编程定时器吗? [英] How to include a timer in Bash Scripting?

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

问题描述

美好的一天!有什么办法,包括使用一个计时器(时间戳?或者是什么的术语)在脚本中的庆典?例如像;每60秒,具有特定功能的检查,如果互联网是向下,如果是,则它连接到无线设备,而不是反之亦然。总之,程序检查不时互​​联网连接。

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.

任何建议/答案将更加AP preciated。 =)

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).

如果我们谈论的几分钟或几小时的时间间隔,cron的可能会做得更好,因为蒙特克里斯托指出。

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

这篇关于如何包括bash脚本编程定时器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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