1

I'm trying to find out whether there is a command that allows calling another script and treating these calls in FIFO order.

E.G.: script A works in an infinite loop. In this loop, 10 successive requests are made to execute script B. However, script B may only be executed when its previous request was ended. Script A has to continue working while the 10 requested scripts B are successively executed.

  • Very ambiguous question IMHO. Can you explain a bit more what exactly you want ? Each script is executed in their own subshell, and for each subshell a separate process is created, so inherently it is FIFO order. – Sergiy Kolodyazhnyy Oct 18 '16 at 3:43
3

The wait builtin of shell is designed to solve exactly this purpose, to wait for some process (or processes) to finish and collect it's exit status.

You can:

  • Run script_A in an infinite loop or exit if it spawns and finishes executing 10 instances of script_B

  • With script_A, use a looping construct, within the loop, send the script_B to background, get its PID using $!

  • Wait for the process to finish by wait PID

  • Do this 10 times

  • Exit from script_A, or continue with something else

So you can make script_A as:

#!/bin/bash
for ((i=1; i<=10; i++)); do
    echo "Run $i :: script_B"
    /path/to/script_B &
    pid=$!
    wait "$pid"
done

If you want to continue doing something else, append them after the for construct.

Just for the sake of completeness, if you want to continue/break out based on the exit status of the last scrip_B, add a short check for the exit status too:

#!/bin/bash                                                                  
for ((i=1; i<=10; i++)); do
    echo "Run $i :: script_B"
    /path/to/script_B &
    pid=$!
    wait "$pid" && continue
    break
done
  • Better then mine! +1 – Fabby Oct 20 '16 at 6:59
1

What I use in a script to prevent it from being executed twice is a status file, but you can easily adapt that same system to allow scripts being executed in FIFO mode:

E.g. in below example, the script will just exit if it's already running:

#!/bin/bash 

if [ -f /var/tmp/backup.bsy ]; then 
    # backup already running, log and exit
    exit; 
fi

# Set backup status to "Busy"
mv /var/tmp/backup.ok /var/tmp/backup.bsy

# perform backup here

# Set backup status to "Done"
mv /var/tmp/backup.bsy /var/tmp/backup.ok

and it's trivial to change the exit to a loop that sleeps a few seconds/minutes (depending on your usage requirements) and looks whether script.1, script.2, script.3, script.4, ... exists and executes them in the correct order...

;-)

1

Maybe I'm misunderstanding, but can't you just place the invocations of scriptB within a command group (or subshell), and background that so that the group is executed asynchronously e.g.

#!/bin/bash

echo "Submiting B command group at: $(date)"

{
  ./scriptB
  ./scriptB
  ./scriptB
  ./scriptB
  ./scriptB
  ./scriptB
  ./scriptB
  ./scriptB
  ./scriptB
  ./scriptB
} &

echo "Continuing to work at: $(date)"

exit

where for the sake of illustration, scriptB is just

#!/bin/bash

echo "Starting B at: $(date)"
sleep $((RANDOM/8192))

then

$ ./scriptA
Submiting B command group at: Mon Oct 17 19:50:07 EDT 2016
Continuing to work at: Mon Oct 17 19:50:07 EDT 2016
user@host$ Starting B at: Mon Oct 17 19:50:07 EDT 2016
Starting B at: Mon Oct 17 19:50:10 EDT 2016
Starting B at: Mon Oct 17 19:50:11 EDT 2016
Starting B at: Mon Oct 17 19:50:12 EDT 2016
Starting B at: Mon Oct 17 19:50:13 EDT 2016
Starting B at: Mon Oct 17 19:50:15 EDT 2016
Starting B at: Mon Oct 17 19:50:15 EDT 2016
Starting B at: Mon Oct 17 19:50:18 EDT 2016
Starting B at: Mon Oct 17 19:50:18 EDT 2016
Starting B at: Mon Oct 17 19:50:18 EDT 2016
  • I think OP means synchronously with FIFO, but we'll probably never know as it looks like a drive-by question. ;-) – Fabby Oct 20 '16 at 7:01
  • As a matter of fact, when script A wants to start script B, script A adds a line to a file, (PUSH). T this line contains all information for script B. Script B retrieves the first line of this file and when the retrieved line is not empty then script B executes what is asked in the retrieved line. the file acts as a FIFO and – L. De Smedt Oct 22 '16 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.