mutex and semaphore like in shell script

Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
proc1.sh
#!/bin/sh
touch /tmp/proc1.signature.mutex
#do something for long time
sleep 100
rm -rf /tmp/proc1.signature.mutex
proc2.sh
#!/bin/sh
touch /tmp/proc2.signature.mutex
#do something for long time
sleep 100
rm -rf /tmp/proc2.signature.mutex
proc3.sh
#!/bin/sh
touch /tmp/proc3.signature.mutex
#do something for long time
sleep 100
rm -rf /tmp/proc3.signature.mutex
core.sh
Now is there a way to wait for deleting /tmp/proc[?][*].signature.mutex all such file using loop or something
and then continue further execution
How to achieve objective of core.sh
scripting semaphore
add a comment |Â
up vote
1
down vote
favorite
proc1.sh
#!/bin/sh
touch /tmp/proc1.signature.mutex
#do something for long time
sleep 100
rm -rf /tmp/proc1.signature.mutex
proc2.sh
#!/bin/sh
touch /tmp/proc2.signature.mutex
#do something for long time
sleep 100
rm -rf /tmp/proc2.signature.mutex
proc3.sh
#!/bin/sh
touch /tmp/proc3.signature.mutex
#do something for long time
sleep 100
rm -rf /tmp/proc3.signature.mutex
core.sh
Now is there a way to wait for deleting /tmp/proc[?][*].signature.mutex all such file using loop or something
and then continue further execution
How to achieve objective of core.sh
scripting semaphore
You can usetrapso that the file.mutexis deleted on exit i.e:trap 'rm -f /tmp/proc3.signature.mutex' EXIT.
â Valentin B
Oct 30 '17 at 15:04
Havecore.shstart the other scripts as background jobs, then usewaitto wait for them to finish. No need for lock files (these aren't mutexes).
â Kusalananda
Oct 30 '17 at 15:46
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
proc1.sh
#!/bin/sh
touch /tmp/proc1.signature.mutex
#do something for long time
sleep 100
rm -rf /tmp/proc1.signature.mutex
proc2.sh
#!/bin/sh
touch /tmp/proc2.signature.mutex
#do something for long time
sleep 100
rm -rf /tmp/proc2.signature.mutex
proc3.sh
#!/bin/sh
touch /tmp/proc3.signature.mutex
#do something for long time
sleep 100
rm -rf /tmp/proc3.signature.mutex
core.sh
Now is there a way to wait for deleting /tmp/proc[?][*].signature.mutex all such file using loop or something
and then continue further execution
How to achieve objective of core.sh
scripting semaphore
proc1.sh
#!/bin/sh
touch /tmp/proc1.signature.mutex
#do something for long time
sleep 100
rm -rf /tmp/proc1.signature.mutex
proc2.sh
#!/bin/sh
touch /tmp/proc2.signature.mutex
#do something for long time
sleep 100
rm -rf /tmp/proc2.signature.mutex
proc3.sh
#!/bin/sh
touch /tmp/proc3.signature.mutex
#do something for long time
sleep 100
rm -rf /tmp/proc3.signature.mutex
core.sh
Now is there a way to wait for deleting /tmp/proc[?][*].signature.mutex all such file using loop or something
and then continue further execution
How to achieve objective of core.sh
scripting semaphore
edited Oct 30 '17 at 17:57
Jeff Schaller
32.1k849109
32.1k849109
asked Oct 30 '17 at 14:33
user2642486
103
103
You can usetrapso that the file.mutexis deleted on exit i.e:trap 'rm -f /tmp/proc3.signature.mutex' EXIT.
â Valentin B
Oct 30 '17 at 15:04
Havecore.shstart the other scripts as background jobs, then usewaitto wait for them to finish. No need for lock files (these aren't mutexes).
â Kusalananda
Oct 30 '17 at 15:46
add a comment |Â
You can usetrapso that the file.mutexis deleted on exit i.e:trap 'rm -f /tmp/proc3.signature.mutex' EXIT.
â Valentin B
Oct 30 '17 at 15:04
Havecore.shstart the other scripts as background jobs, then usewaitto wait for them to finish. No need for lock files (these aren't mutexes).
â Kusalananda
Oct 30 '17 at 15:46
You can use
trap so that the file .mutex is deleted on exit i.e: trap 'rm -f /tmp/proc3.signature.mutex' EXIT.â Valentin B
Oct 30 '17 at 15:04
You can use
trap so that the file .mutex is deleted on exit i.e: trap 'rm -f /tmp/proc3.signature.mutex' EXIT.â Valentin B
Oct 30 '17 at 15:04
Have
core.sh start the other scripts as background jobs, then use wait to wait for them to finish. No need for lock files (these aren't mutexes).â Kusalananda
Oct 30 '17 at 15:46
Have
core.sh start the other scripts as background jobs, then use wait to wait for them to finish. No need for lock files (these aren't mutexes).â Kusalananda
Oct 30 '17 at 15:46
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
A mutex is a mutual exclusive lock. Your procN.sh scripts never test whether the mutex is held by another process before "locking".
If you let core.sh start the other scripts, it would be easy for it to wait for the completion of them:
#!/bin/sh
./proc1.sh &
./proc2.sh &
./proc3.sh &
wait
# other processing
This removes the need for the lock files altogether. If that's not possible, consider
#!/bin/sh
while [ -e "$HOME/locks/proc1.signature.mutex" ] ||
[ -e "$HOME/locks/proc2.signature.mutex" ] ||
[ -e "$HOME/locks/proc3.signature.mutex" ]
then
echo 'waiting...'
sleep 10
done
# other processing
To avoid leaving files behind by the procN.sh scripts if they die of unnatural causes, use a trap:
#!/bin/sh
lockfile="$HOME/locks/proc1.signature.mutex"
while [ -e "$lockfile" ]; do
echo 'Can not get lock. Waiting...' >&2
sleep 10
done
## alternatively:
# [ -e "$lockfile" ] && echo 'something is wrong' >&2; exit 1;
trap 'rm "$lockfile"; exit' EXIT INT TERM HUP
touch "$lockfile"
# etc.
# no need to rm the lock file at the end
Note that there is a space between the -e "$lockfile" test and the touch in which another process may lock the same file.
To avoid this, use a lock directory instead:
#!/bin/sh
lockdir="$HOME/locks/proc1.signature.mutex"
while ! mkdir "$lockdir"; do
echo 'Can not get lock. Waiting...' >&2
sleep 10
done
## alternatively:
# [ -e "$lockdir" ] && echo 'something is wrong' >&2; exit 1;
trap 'rmdir "$lockdir"; exit' EXIT INT TERM HUP
# etc.
# no need to rmdir the lock dir at the end
You may also use a symbolic link in a similar way.
Note that I've used a path under $HOME for the lock files/directories. If using /tmp, any user could potentially lock your script out of action by simply creating the correct file/directory.
There probably should be warnings about/tmpusage as a local attacker could denial of service the file version with a broken symlink (and the directory version might use a-dcheck...)
â thrig
Oct 30 '17 at 18:35
@thrig Good point.
â Kusalananda
Oct 30 '17 at 18:45
@thrig-eworks on both files and directories, but you need-hto test existence of a symlink (it doesn't matter if it's broken)
â Kusalananda
Oct 30 '17 at 20:54
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
A mutex is a mutual exclusive lock. Your procN.sh scripts never test whether the mutex is held by another process before "locking".
If you let core.sh start the other scripts, it would be easy for it to wait for the completion of them:
#!/bin/sh
./proc1.sh &
./proc2.sh &
./proc3.sh &
wait
# other processing
This removes the need for the lock files altogether. If that's not possible, consider
#!/bin/sh
while [ -e "$HOME/locks/proc1.signature.mutex" ] ||
[ -e "$HOME/locks/proc2.signature.mutex" ] ||
[ -e "$HOME/locks/proc3.signature.mutex" ]
then
echo 'waiting...'
sleep 10
done
# other processing
To avoid leaving files behind by the procN.sh scripts if they die of unnatural causes, use a trap:
#!/bin/sh
lockfile="$HOME/locks/proc1.signature.mutex"
while [ -e "$lockfile" ]; do
echo 'Can not get lock. Waiting...' >&2
sleep 10
done
## alternatively:
# [ -e "$lockfile" ] && echo 'something is wrong' >&2; exit 1;
trap 'rm "$lockfile"; exit' EXIT INT TERM HUP
touch "$lockfile"
# etc.
# no need to rm the lock file at the end
Note that there is a space between the -e "$lockfile" test and the touch in which another process may lock the same file.
To avoid this, use a lock directory instead:
#!/bin/sh
lockdir="$HOME/locks/proc1.signature.mutex"
while ! mkdir "$lockdir"; do
echo 'Can not get lock. Waiting...' >&2
sleep 10
done
## alternatively:
# [ -e "$lockdir" ] && echo 'something is wrong' >&2; exit 1;
trap 'rmdir "$lockdir"; exit' EXIT INT TERM HUP
# etc.
# no need to rmdir the lock dir at the end
You may also use a symbolic link in a similar way.
Note that I've used a path under $HOME for the lock files/directories. If using /tmp, any user could potentially lock your script out of action by simply creating the correct file/directory.
There probably should be warnings about/tmpusage as a local attacker could denial of service the file version with a broken symlink (and the directory version might use a-dcheck...)
â thrig
Oct 30 '17 at 18:35
@thrig Good point.
â Kusalananda
Oct 30 '17 at 18:45
@thrig-eworks on both files and directories, but you need-hto test existence of a symlink (it doesn't matter if it's broken)
â Kusalananda
Oct 30 '17 at 20:54
add a comment |Â
up vote
2
down vote
accepted
A mutex is a mutual exclusive lock. Your procN.sh scripts never test whether the mutex is held by another process before "locking".
If you let core.sh start the other scripts, it would be easy for it to wait for the completion of them:
#!/bin/sh
./proc1.sh &
./proc2.sh &
./proc3.sh &
wait
# other processing
This removes the need for the lock files altogether. If that's not possible, consider
#!/bin/sh
while [ -e "$HOME/locks/proc1.signature.mutex" ] ||
[ -e "$HOME/locks/proc2.signature.mutex" ] ||
[ -e "$HOME/locks/proc3.signature.mutex" ]
then
echo 'waiting...'
sleep 10
done
# other processing
To avoid leaving files behind by the procN.sh scripts if they die of unnatural causes, use a trap:
#!/bin/sh
lockfile="$HOME/locks/proc1.signature.mutex"
while [ -e "$lockfile" ]; do
echo 'Can not get lock. Waiting...' >&2
sleep 10
done
## alternatively:
# [ -e "$lockfile" ] && echo 'something is wrong' >&2; exit 1;
trap 'rm "$lockfile"; exit' EXIT INT TERM HUP
touch "$lockfile"
# etc.
# no need to rm the lock file at the end
Note that there is a space between the -e "$lockfile" test and the touch in which another process may lock the same file.
To avoid this, use a lock directory instead:
#!/bin/sh
lockdir="$HOME/locks/proc1.signature.mutex"
while ! mkdir "$lockdir"; do
echo 'Can not get lock. Waiting...' >&2
sleep 10
done
## alternatively:
# [ -e "$lockdir" ] && echo 'something is wrong' >&2; exit 1;
trap 'rmdir "$lockdir"; exit' EXIT INT TERM HUP
# etc.
# no need to rmdir the lock dir at the end
You may also use a symbolic link in a similar way.
Note that I've used a path under $HOME for the lock files/directories. If using /tmp, any user could potentially lock your script out of action by simply creating the correct file/directory.
There probably should be warnings about/tmpusage as a local attacker could denial of service the file version with a broken symlink (and the directory version might use a-dcheck...)
â thrig
Oct 30 '17 at 18:35
@thrig Good point.
â Kusalananda
Oct 30 '17 at 18:45
@thrig-eworks on both files and directories, but you need-hto test existence of a symlink (it doesn't matter if it's broken)
â Kusalananda
Oct 30 '17 at 20:54
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
A mutex is a mutual exclusive lock. Your procN.sh scripts never test whether the mutex is held by another process before "locking".
If you let core.sh start the other scripts, it would be easy for it to wait for the completion of them:
#!/bin/sh
./proc1.sh &
./proc2.sh &
./proc3.sh &
wait
# other processing
This removes the need for the lock files altogether. If that's not possible, consider
#!/bin/sh
while [ -e "$HOME/locks/proc1.signature.mutex" ] ||
[ -e "$HOME/locks/proc2.signature.mutex" ] ||
[ -e "$HOME/locks/proc3.signature.mutex" ]
then
echo 'waiting...'
sleep 10
done
# other processing
To avoid leaving files behind by the procN.sh scripts if they die of unnatural causes, use a trap:
#!/bin/sh
lockfile="$HOME/locks/proc1.signature.mutex"
while [ -e "$lockfile" ]; do
echo 'Can not get lock. Waiting...' >&2
sleep 10
done
## alternatively:
# [ -e "$lockfile" ] && echo 'something is wrong' >&2; exit 1;
trap 'rm "$lockfile"; exit' EXIT INT TERM HUP
touch "$lockfile"
# etc.
# no need to rm the lock file at the end
Note that there is a space between the -e "$lockfile" test and the touch in which another process may lock the same file.
To avoid this, use a lock directory instead:
#!/bin/sh
lockdir="$HOME/locks/proc1.signature.mutex"
while ! mkdir "$lockdir"; do
echo 'Can not get lock. Waiting...' >&2
sleep 10
done
## alternatively:
# [ -e "$lockdir" ] && echo 'something is wrong' >&2; exit 1;
trap 'rmdir "$lockdir"; exit' EXIT INT TERM HUP
# etc.
# no need to rmdir the lock dir at the end
You may also use a symbolic link in a similar way.
Note that I've used a path under $HOME for the lock files/directories. If using /tmp, any user could potentially lock your script out of action by simply creating the correct file/directory.
A mutex is a mutual exclusive lock. Your procN.sh scripts never test whether the mutex is held by another process before "locking".
If you let core.sh start the other scripts, it would be easy for it to wait for the completion of them:
#!/bin/sh
./proc1.sh &
./proc2.sh &
./proc3.sh &
wait
# other processing
This removes the need for the lock files altogether. If that's not possible, consider
#!/bin/sh
while [ -e "$HOME/locks/proc1.signature.mutex" ] ||
[ -e "$HOME/locks/proc2.signature.mutex" ] ||
[ -e "$HOME/locks/proc3.signature.mutex" ]
then
echo 'waiting...'
sleep 10
done
# other processing
To avoid leaving files behind by the procN.sh scripts if they die of unnatural causes, use a trap:
#!/bin/sh
lockfile="$HOME/locks/proc1.signature.mutex"
while [ -e "$lockfile" ]; do
echo 'Can not get lock. Waiting...' >&2
sleep 10
done
## alternatively:
# [ -e "$lockfile" ] && echo 'something is wrong' >&2; exit 1;
trap 'rm "$lockfile"; exit' EXIT INT TERM HUP
touch "$lockfile"
# etc.
# no need to rm the lock file at the end
Note that there is a space between the -e "$lockfile" test and the touch in which another process may lock the same file.
To avoid this, use a lock directory instead:
#!/bin/sh
lockdir="$HOME/locks/proc1.signature.mutex"
while ! mkdir "$lockdir"; do
echo 'Can not get lock. Waiting...' >&2
sleep 10
done
## alternatively:
# [ -e "$lockdir" ] && echo 'something is wrong' >&2; exit 1;
trap 'rmdir "$lockdir"; exit' EXIT INT TERM HUP
# etc.
# no need to rmdir the lock dir at the end
You may also use a symbolic link in a similar way.
Note that I've used a path under $HOME for the lock files/directories. If using /tmp, any user could potentially lock your script out of action by simply creating the correct file/directory.
edited Oct 30 '17 at 18:48
answered Oct 30 '17 at 16:02
Kusalananda
105k14209326
105k14209326
There probably should be warnings about/tmpusage as a local attacker could denial of service the file version with a broken symlink (and the directory version might use a-dcheck...)
â thrig
Oct 30 '17 at 18:35
@thrig Good point.
â Kusalananda
Oct 30 '17 at 18:45
@thrig-eworks on both files and directories, but you need-hto test existence of a symlink (it doesn't matter if it's broken)
â Kusalananda
Oct 30 '17 at 20:54
add a comment |Â
There probably should be warnings about/tmpusage as a local attacker could denial of service the file version with a broken symlink (and the directory version might use a-dcheck...)
â thrig
Oct 30 '17 at 18:35
@thrig Good point.
â Kusalananda
Oct 30 '17 at 18:45
@thrig-eworks on both files and directories, but you need-hto test existence of a symlink (it doesn't matter if it's broken)
â Kusalananda
Oct 30 '17 at 20:54
There probably should be warnings about
/tmp usage as a local attacker could denial of service the file version with a broken symlink (and the directory version might use a -d check...)â thrig
Oct 30 '17 at 18:35
There probably should be warnings about
/tmp usage as a local attacker could denial of service the file version with a broken symlink (and the directory version might use a -d check...)â thrig
Oct 30 '17 at 18:35
@thrig Good point.
â Kusalananda
Oct 30 '17 at 18:45
@thrig Good point.
â Kusalananda
Oct 30 '17 at 18:45
@thrig
-e works on both files and directories, but you need -h to test existence of a symlink (it doesn't matter if it's broken)â Kusalananda
Oct 30 '17 at 20:54
@thrig
-e works on both files and directories, but you need -h to test existence of a symlink (it doesn't matter if it's broken)â Kusalananda
Oct 30 '17 at 20:54
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f401415%2fmutex-and-semaphore-like-in-shell-script%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
You can use
trapso that the file.mutexis deleted on exit i.e:trap 'rm -f /tmp/proc3.signature.mutex' EXIT.â Valentin B
Oct 30 '17 at 15:04
Have
core.shstart the other scripts as background jobs, then usewaitto wait for them to finish. No need for lock files (these aren't mutexes).â Kusalananda
Oct 30 '17 at 15:46