延迟正在运行的bash命令

时间:2016-03-15 01:44:13

标签: linux bash loops delay sleep

我试图通过将一个php命令插入到暂停和恢复命令而不停止/再次启动它的bash脚本中来延迟它。我需要命令运行大约10秒,然后永远休眠5秒,直到命令完成(将需要整晚) 例如:

#!/bin/bash
php /my/command/here & sleep 5 & resume/my/command

我不是bash专家,但我确定这是一个可以在某处使用的“while”命令。

2 个答案:

答案 0 :(得分:0)

您可以使用以下脚本中注释的以下方法:

#!/bin/bash
php /my/command/here & #run your command in background 
P="$!" # keep the pid of your command
sleep 10
kill -TSTP "$P" # send signal to suspend your command
sleep 5
fg "$P" # resume command

答案 1 :(得分:0)

在后台运行php命令,然后运行一个循环,不断向该进程发送SIGSTOPSIGCONT

使用两个后台进程,一个用于运行php,另一个用于运行重复暂停和恢复php脚本的脚本。 (sleep在后​​台运行,以便php完成后暂停可以暂停暂停。一旦启动了两个后台作业,我们只需等待php完成,然后杀死仍然在后台运行的任何内容。

php /my/command/here & php_id=$!

while :; do
    sleep 10 & wait
    kill -s STOP "$php_id"
    sleep 5 & wait
    kill -s CONT "$php_id"
done &

wait "$php_id"
kill $(jobs -p)