-
Notifications
You must be signed in to change notification settings - Fork 5
/
lockProcedure
75 lines (73 loc) · 1.27 KB
/
lockProcedure
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
function sms ()
{
echo $@
}
function setlock()
{
echo $$ > $HOME/lock
sms "Lock set, enjoy"
return
}
function lock ()
{
#si lock n'existe pas, le créer puis rendre la main
test ! -f $HOME/lock && setlock && return
#le fichier existe
lockPID=$(cat $HOME/lock)
sms "System locked by $lockPID, checking"
if [ $lockPID -eq $$ ]
then
echo "What are you doing? Your lock is already set..."
return
fi
#verification du pid utilise
ps uhp $lockPID >/dev/null
ret=$?
if [ $ret -ne 0 ]
then
#process not running, lock
setlock
return
fi
#est ce quon doit attendre
if [ "x$1" != "x--wait" ]
then
sms "Still running and nowait. Exiting."
exit
else
#loop (waitlock)
ps uhp $lockPID >/dev/null
ret=$?
while [ $ret -eq 0 ]
do
sleepTime=$(($RANDOM%4+8))
sms "Waiting $sleepTime seconds for pid $lockPID."
sleep $sleepTime
#re-read lock if there is two waitlock in the same time
test -f $HOME/lock && lockPID=$(cat $HOME/lock)
ps uhp $lockPID >/dev/null
ret=$?
done
setlock
fi
}
function waitlock ()
{
lock --wait
}
function unlock ()
{
lockPID=$(cat $HOME/lock)
if [ $lockPID -ne $$ ]
then
sms "refusing to unlock other task"
return
fi
rm $HOME/lock
}
function forceunlock ()
{
sms "/!\ Forcing unlock !"
rm $HOME/lock
}