Created
February 24, 2025 05:44
-
-
Save msankhala/5e3923d7481cd13a5dc5fd8e5e0a132a to your computer and use it in GitHub Desktop.
Validate Use Flock to run only one instance of process with Cron
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
for i in {2..100}; do | |
# Your code to be executed in each iteration goes here | |
echo "Script 1 Iteration: $i" | |
sleep 1 | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
for i in {2..100}; do | |
# Your code to be executed in each iteration goes here | |
echo "Script 2 Iteration: $i" | |
sleep 1 | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run the script1-loop.sh script in terminal 1 to run script1-loop.sh with a lock using flock. | |
flock -n /tmp/mylock.lock bash script1-loop.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run the following command in another terminal to watch the output of ps command to print the any script that is using | |
# script.*.sh files. | |
watch 'ps -ef | grep -v grep | grep script.*.sh' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run the script2-loop.sh script in terminal 3 to run script1-loop.sh with a lock using flock. | |
flock -n /tmp/mylock.lock bash script2-loop.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Now in terminal 3 run the script2-loop.sh without flock. | |
bash script2-loop.sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment