如何在 bash/linux 脚本中执行相同的循环 1 小时?

发布于 2024-12-16 16:21:23 字数 549 浏览 0 评论 0原文

我想以随机间隔打开和关闭一些网页一小时。

到目前为止,我已经写了内部结构

FILE=webpages
TIMES=0
while test $TIMES -lt 10
do
    #Picks random line(Website) from FILE
    TEMPWEB=`shuf -n 1 $FILE`
    echo "Opening" $TEMPWEB
    #Opens TEMPWEB
    xdg-open $TEMPWEB
    sleep 10
    #Kills firefox
    ps -e | grep firefox | sed 's/\(.[0-9]*\).*/\1/' | xargs kill
    TIMES=$(($TIMES+1))
done

,所以我缺少 while 条件。
我想要这样的东西:

TIMER = 0h 0m 0s
while( TIMER < 1 h)
   ....
done

这可能吗?

附言。随机数的命令是什么?

I want to open and close some web pages with random intervals for an hour.

So far i have written the innards

FILE=webpages
TIMES=0
while test $TIMES -lt 10
do
    #Picks random line(Website) from FILE
    TEMPWEB=`shuf -n 1 $FILE`
    echo "Opening" $TEMPWEB
    #Opens TEMPWEB
    xdg-open $TEMPWEB
    sleep 10
    #Kills firefox
    ps -e | grep firefox | sed 's/\(.[0-9]*\).*/\1/' | xargs kill
    TIMES=$(($TIMES+1))
done

So I'm am missing the while condition.
I want something like:

TIMER = 0h 0m 0s
while( TIMER < 1 h)
   ....
done

Is this possible?

PS. what's the command to random number?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静水深流 2024-12-23 16:21:23

一定。尝试:

#!/bin/bash
START=`date +%s`
while [ $(( $(date +%s) - 3600 )) -lt $START ]; do
    ....
done

date +%s 显示自 1970 年以来的当前时间(以秒为单位)。循环计算一小时前的当前日期并检查它是否超过开始时间。

Surely. Try:

#!/bin/bash
START=`date +%s`
while [ $(( $(date +%s) - 3600 )) -lt $START ]; do
    ....
done

date +%s shows the current time in seconds since 1970. The loop computes the current date of an hour ago and checks if it exceeds the start time.

薄凉少年不暖心 2024-12-23 16:21:23

实现此目的的一种方法是使用信号和陷阱。这是一个简单的例子:

#!/bin/bash

me=$

# Start a background process to signal us when the time has passed
(
        sleep 10
        kill -USR1 $me
) &

# When we recieve SIGUSR1, commit suicide
trap "kill $me" SIGUSR1

# Do stuff until we commit suicide
while [ 1 -eq 1 ]
do
        echo 'Hello!'
        sleep 1
done

评论是不言自明的(我希望)。第一部分启动一个后台进程,该进程仅休眠一段时间(本例中为 10 秒),然后当它醒来时,向原始进程发送 SIGUSR1。

shell 脚本捕获此情况(使用 trap),然后向其自己的 PID 发出 kill,停止进程。

其余部分只是一个无限循环——它是杀死脚本并终止循环的陷阱

从技术上讲,您不需要上面示例中的陷阱:后台进程可以直接发出 kill $me ,但为了完整起见,我将其包含在内,因为您可以将其用作钩子来执行其他操作(例如,如果您不想死,但设置一个标志并让循环自然终止,或者在终止之前需要进行一些清理工作)。

One way you can do this is with signals and traps. Here's a simple example:

#!/bin/bash

me=$

# Start a background process to signal us when the time has passed
(
        sleep 10
        kill -USR1 $me
) &

# When we recieve SIGUSR1, commit suicide
trap "kill $me" SIGUSR1

# Do stuff until we commit suicide
while [ 1 -eq 1 ]
do
        echo 'Hello!'
        sleep 1
done

The comments are self-explanetory (I hope). The first part kicks off a background process that just sleeps for a period of time (10 seconds in this example), and then when it wakes up sends SIGUSR1 to the originating process.

The shellscript catches this (with trap) and just issues a kill to its own PID, stopping the process.

The rest of it is just an infinite loop -- it's the trap that kills the script and terminates the loop.

Technically you don't need the trap in the example above: the background process could just issue kill $me directly, but I've included it for completeness as you can use it as a hook to do other things (e.g., if you don't want to die but set a flag and have the loop terminate naturally, or you have some clean-up to do before terminating).

错爱 2024-12-23 16:21:23

这是我的解决方案:

#!/bin/bash

# function to display a number in a range
randrange() { echo $(( ( RANDOM % ($2 - $1 +1 ) ) + $1 )); }

# sleep 1 hour and keep backgrounded PID in a variable
sleep 3600 & _suicide_pid=$!

# while the process is UP
while kill &>/dev/null -0 $_suicide_pid; do
    # ----->8--- do what you want here <---8<-----
    sleep $(randrange 10 15) # random sleep in 10-15 range
done

This is my solution :

#!/bin/bash

# function to display a number in a range
randrange() { echo $(( ( RANDOM % ($2 - $1 +1 ) ) + $1 )); }

# sleep 1 hour and keep backgrounded PID in a variable
sleep 3600 & _suicide_pid=$!

# while the process is UP
while kill &>/dev/null -0 $_suicide_pid; do
    # ----->8--- do what you want here <---8<-----
    sleep $(randrange 10 15) # random sleep in 10-15 range
done
撩心不撩汉 2024-12-23 16:21:23

我的解决方案基于 来自 thiton 的解决方案:

#!/bin/bash

STOP=`date -d 'next hour' +%s`
while [ $(date +%s) -lt $STOP ]; do
    date
    sleep 2
done

也可以很好地内嵌:

STOP=`date -d 'next hour' +%s`; while [ $(date  +%s) -lt $STOP ]; do date; sleep 2; done

My solution is based on the one from thiton:

#!/bin/bash

STOP=`date -d 'next hour' +%s`
while [ $(date +%s) -lt $STOP ]; do
    date
    sleep 2
done

Also works fine inline:

STOP=`date -d 'next hour' +%s`; while [ $(date  +%s) -lt $STOP ]; do date; sleep 2; done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文