Вы должны использовать таймер, установить интервал в 5000 миллисекунд, что составляет 5 секунд, и ввести этот код под событием Timer_Tick
'Declare a variable for storing the current index
Dim index As Integer = 0
Private Sub Timer1_Tick_1(sender As Object, e As EventArgs) Handles Timer1.Tick
If (Not index >= RichTextBox2.Lines.Length- 1) Then
txt_myip.Text = RichTextBox2.Lines(index)
index += 1
Else
index = 0
Timer1.Stop()
End If
End Sub
. добавьте столбец exec_status
кому: myhappytable
(возможно, также time_started
и time_finished
, см. псевдокод),
запустите следующий скрипт крона каждый x минуты
псевдокод сценария крона:
[create/check pid lock (optional, but see "A potential pitfall" below)]
get number of rows from myhappytable where (exec_status == executing_now)
if it is > 0, exit
begin loop
get one row from myhappytable
where (exec_status == not_yet_run) and (scheduled_time <= now)
order by scheduled_time asc
if no such row, exit
set row exec_status to executing_now (maybe set time_started to now)
execute whatever command the row contains
set row exec_status to completed
(maybe also store the command output/return as well, set time_finished to now)
end loop
[delete pid lock file (complementary to the starting pid lock check)]
Таким образом, сценарий сначала проверяет, не выполняет ли ни одна из команд, затем сначала еще не выполняет команду выполнения, пока больше нет команд, которые будут выполнены в данный момент. Кроме того, Вы видите то, что команда выполняет путем запросов базы данных.
Потенциальная ловушка: если сценарий крона будет уничтожен, то запланированная задача останется в состоянии "executing_now". Это - то, для чего изодромная с предварением блокировка вначале и конец: видеть если сценарий крона, завершенный правильно. псевдокод создает/проверяет pidlock:
if exists pidlockfile then
check if process id given in file exists
if not exists then
update myhappytable set exec_status = error_cronscript_died_while_executing_this
where exec_status == executing_now
delete pidlockfile
else (previous instance still running)
exit
endif
endif
create pidlockfile containing cron script process id
Можно использовать в (1) команда в сценарии для планирования его следующего запуска. Прежде чем это выйдет, это может проверить myhappyschedule в течение времени следующего запуска. Вам не нужен крон вообще, действительно.
I came across this question while researching for a solution to the queuing problem. For the benefit of anyone else searching here is my solution.
Combine this with a cron that starts jobs as they are scheduled (even if they are scheduled to run at the same time) and that solves the problem you described as well.
ie. We need a pipeline to the script.
Create a pipeline to any script. Done using a small bash script (further down).
The script can be called as
./pipeline "
Example:
./pipeline sleep 10 &
./pipeline shabugabu &
./pipeline single_instance_script some arguments &
./pipeline single_instance_script some other_argumnts &
./pipeline "single_instance_script some yet_other_arguments > output.txt" &
..etc
The script creates a new named pipe for each command. So the above will create named pipes: sleep
, shabugabu
, and single_instance_script
In this case the initial call will start a reader and run single_instance_script
with some arguments
as arguments. Once the call completes, the reader will grab the next request off the pipe and execute with some other_arguments
, complete, grab the next etc...
This script will block requesting processes so call it as a background job (& at the end) or as a detached process with at
(at now <<< "./pipeline some_script"
)
#!/bin/bash -Eue
# Using command name as the pipeline name
pipeline=$(basename $(expr "$1" : '\(^[^[:space:]]*\)')).pipe
is_reader=false
function _pipeline_cleanup {
if $is_reader; then
rm -f $pipeline
fi
rm -f $pipeline.lock
exit
}
trap _pipeline_cleanup INT TERM EXIT
# Dispatch/initialization section, critical
lockfile $pipeline.lock
if [[ -p $pipeline ]]
then
echo "$*" > $pipeline
exit
fi
is_reader=true
mkfifo $pipeline
echo "$*" > $pipeline &
rm -f $pipeline.lock
# Reader section
while read command < $pipeline
do
echo "$(date) - Executing $command"
($command) &> /dev/null
done