我正在测试一些可靠性方案,我想如何编写脚本或小型应用程序来实现2个交替的(活/死)进程的运行时间表,例如:
First 30s: ProcessA - start, run
Next 30s: ProcessB - start, run
Next 30s: ProcessA - kill -9, Process B run
Next 30s, ProcessB - kill -9, Process A start, run
Next 30s, ProcessB - start, run, Process A, run
等等。
条件是计划永远不会杀死进程(或者两个进程被杀死的时间间隔重叠)。 我们可以忽略故障,并出于此脚本的目的,假设在脚本中模拟的kill -9之外,将要运行的一台或多台机器不会失败,并且进程也不会失败。
所以我需要某种基于障碍的协调...但是不知道如何在bash中实现它。
这是我正在尝试做的事情,但是不用说这是行不通的。运行脚本后,实例化$ PROGRAM1,$ PROGRAM2和$ PROGRAM3(示例仅出于直觉),然后被永久卡住。 它不会替代,请通过此两步锁定步骤杀死-9并重新启动它们。换句话说,循环不会循环。
PROGRAM1 = "echo P1; sleep 100"
PROGRAM2 = "echo P2; sleep 100"
PROGRAM3 = "echo P3; sleep 600"
$PROGRAM1 &
echo Started $PROGRAM1
PID1=$!
$PROGRAM2 &
echo Started $PROGRAM2
PID2=$!
$PROGRAM3 &
echo Started $PROGRAM3
while 1; do
sleep 10
echo Killing $PROGRAM1
kill -9 $PID1
wait $PID1
sleep 10
echo Starting $PROGRAM1
$PROGRAM1 &
PID1=$!
sleep 10
echo Killing $PROGRAM2
kill -9 $PID2
wait $PID2
sleep 10
echo Starting $PROGRAM2
$PROGRAM2 &
PID2=$!
done
$ PROGRAM3是监视两者的一个。
答案 0 :(得分:0)
以下似乎很好用...
extension CloudObject where Self : NSManagedObject {
var recordID : CKRecordID {
return CKRecordID(recordName: self.recordName, zoneID: CloudManager.shared.zoneID)
}
}
:
program1
#!/bin/bash
echo PROGRAM1
sleep 100
:
program2
#!/bin/bash
echo PROGRAM2
sleep 100
:
master
示例输出
#!/bin/bash
./program1 &
echo Started program1
PID1=$!
./program2 &
echo Started program2
PID2=$!
while : ; do
sleep 10
echo Killing program1
kill -9 $PID1
wait $PID1
sleep 10
echo Starting program1
./program1 &
PID1=$!
sleep 10
echo Killing program2
kill -9 $PID2
wait $PID2
sleep 10
echo Starting program2
./program2 &
PID2=$!
done
在没有Started program1
Started program2
PROGRAM1
PROGRAM2
Killing program1
./master: line 13: 90499 Killed: 9 ./program1
Starting program1
PROGRAM1
Killing program2
./master: line 21: 90500 Killed: 9 ./program2
Starting program2
PROGRAM2
Killing program1
./master: line 13: 90505 Killed: 9 ./program1
Starting program1
PROGRAM1
Killing program2
./master: line 21: 90512 Killed: 9 ./program2
Starting program2
PROGRAM2
命令的情况下,其工作原理也相同。它绕着循环。我对问题是什么感到困惑。