Skip to content

Instantly share code, notes, and snippets.

@msankhala
Created February 24, 2025 05:44
Show Gist options
  • Select an option

  • Save msankhala/5e3923d7481cd13a5dc5fd8e5e0a132a to your computer and use it in GitHub Desktop.

Select an option

Save msankhala/5e3923d7481cd13a5dc5fd8e5e0a132a to your computer and use it in GitHub Desktop.

Revisions

  1. msankhala created this gist Feb 24, 2025.
    7 changes: 7 additions & 0 deletions script1-loop.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    #!/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
    7 changes: 7 additions & 0 deletions script2-loop.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    #!/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
    2 changes: 2 additions & 0 deletions terminal-1-flock-command
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    # 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
    3 changes: 3 additions & 0 deletions terminal-2-watch-command
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    # 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'
    2 changes: 2 additions & 0 deletions terminal-3-flock-command
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    # 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
    2 changes: 2 additions & 0 deletions terminal-3-without-flock
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    # Now in terminal 3 run the script2-loop.sh without flock.
    bash script2-loop.sh