mutex and semaphore like in shell script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite
1












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







share|improve this question






















  • 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















up vote
1
down vote

favorite
1












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







share|improve this question






















  • 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













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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







share|improve this question














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









share|improve this question













share|improve this question




share|improve this question








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 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

















  • 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
















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











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.






share|improve this answer






















  • 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 -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











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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.






share|improve this answer






















  • 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 -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















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.






share|improve this answer






















  • 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 -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













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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 30 '17 at 18:48

























answered Oct 30 '17 at 16:02









Kusalananda

105k14209326




105k14209326











  • 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 -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

















  • 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 -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
















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


















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Popular posts from this blog

Peggy Mitchell

The Forum (Inglewood, California)

Palaiologos