Run commands with synchronization in shell.

Domain/Linux 2016.04.20 22:43
Environment : Ubuntu 14.04
Tools : sem, flock(recommend)

> $ cat a.sh
#!/bin/bash

str=0123456789
for i in {1..7}; do
    str=$str$str
done

for i in {1..10}; do
    echo $str >> out                   # (*a)
#    sem "echo $str >> out"            # (*b)
#    flock out -c "echo $str >> out"   # (*c)
done


> $ cat runa
#!/bin/bash
id=$1
for i in {1..10}; do
    ./a.sh&
done


> $ cat runa2
#!/bin/bash

for i in {1..10}; do
    ./runa&
done

> $ ./runa2


check
---------------
$ cat parout | uniq | wc -l
1
---------------
if 1 => OK, otherwise not-syncronized!!

(*a) ==> race condition!! :
(*c) sem ==> error!(sem bug!!)
(*b) flock ==> OK

신고
Trackback 0 : Comment 0
◀ PREV : [1] : [1] : [2] : [3] : [4] : [5] : [6] : [7] : [8] : [9] : [···] : [299] : NEXT ▶

티스토리 툴바