3

Is it possible to initiate 10 of the same shell scripts which also initiate more copies, and have them all use the same env variable?

For example:

script.sh:

#!/bin/bash
export COUNTER=$((COUNTER+1));
echo $COUNTER;
sleep 5;
/script.sh

using xargs -P 10 script.sh results in each instance creating its own scope, rather than sharing the same $COUNTER variable

I'm essentially trying to deploy script.sh 10 initial times, and then when each one completes, it automatically executes script.sh again, only this time with the newly incremented $COUNTER variable.

I've asked a similar question here, and now understand why my previous method does not work, I'm extremely interested in learning what the best way to actually accomplish this will be.

d-_-b
  • 1,177
  • No, a process can't alter a variable in the parent process. – jordanm Dec 29 '13 at 06:39
  • Yea, I've (unfortunately) been made aware of this. I'm really interested to see what the best way to accomplish a task like this will be! – d-_-b Dec 29 '13 at 06:40
  • 1
    Use a language that supports shared variables in threads. Even then, you will have a ton of race conditions. What are you actually trying to accomplish? – jordanm Dec 29 '13 at 06:43
  • I'm cycling through a list, and want to pass one line to a script. each time the script finishes it should start itself up again using the "next" line. – d-_-b Dec 29 '13 at 06:44

2 Answers2

2

You should take a look at GNU parallel. It can limit the number of jobs spawned in varying ways ( --jobs 10 for your application) and also has a job number ({#}) that can be passed as an argument to the script.

As Ole commented, the following should work:

find . -type f | parallel -j10 my_script {#} {}
Zelda
  • 6,262
  • 1
  • 23
  • 28
1

IPC might be another approach. You can create a shared memory segment that will provide you with a locking mechanism where each process can then update the value stored here. This is not a file on the filesystem, it's a section of memory that is designated for this purpose.

You can read more about IPC via the man ipcs man page.

References

slm
  • 369,824