Skip to content

Instantly share code, notes, and snippets.

@msankhala
Created February 24, 2025 05:44
Show Gist options
  • Save msankhala/5e3923d7481cd13a5dc5fd8e5e0a132a to your computer and use it in GitHub Desktop.
Save msankhala/5e3923d7481cd13a5dc5fd8e5e0a132a to your computer and use it in GitHub Desktop.
Validate Use Flock to run only one instance of process with Cron
#!/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
#!/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
# 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
# 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'
# 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
# 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