Synchronization between 2 shell scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Synchronization between 2 shell scripts
# 1  
Old 03-12-2011
Synchronization between 2 shell scripts

Hello All,

I want to run 2 shell scripts(bash shell) concurrently, both of which are of the form,

Code:
#######Script 1 & Script 2##########
while [ condition ]
do
      do something
      #some task T
      do something
done

I want when to perform task T in mutually exclusive manner.

I tried using Peterson's method and put loops like the one shown below to enter critical region(task T)

Code:
FLAG_1=1
TURN=0
while [[ $FLAG_2 -eq 1 && $TURN -eq 0 ]]
do
    DUMMY=1
done

I ran the scripts as
Code:
. ./script1.sh &
. ./script2.sh &

But later realized that this wont work !

Any ideas how I can do this?

Last edited by rbatte1; 08-31-2016 at 07:31 AM.. Reason: Added CODE tags
# 2  
Old 03-12-2011
You should create a flag file.
You should ensure the atomicity of this creation (this will ensure that even if the scripts are launched in a concurrent manner, only 1 will succeed in creating the flag).

You could set the noclobber option (depending on your OS set option may vary: man set and look for noclobber).
you could also use the mkdir command which is atomic but use it without the -p option, otherwise, the handling of error on creation may be messed up.

---------- Post updated at 01:47 AM ---------- Previous update was at 12:52 AM ----------

Read the good example here :
A Script Template and Useful Techniques for ksh Scripts - BigAdmin - wikis.sun.com
# 3  
Old 03-12-2011
Thank you ctsgnb !

The information is really helpful

I used ln -s as given in the tutorial as an atomic operation.

In may case each script does an ln -s until it is successful in creating the link
After exiting the critical section it removes the symbolic link.

Thank you once again!

Last edited by rbatte1; 08-31-2016 at 07:32 AM.. Reason: Added ICODE tags
# 4  
Old 03-13-2011
Watch out : as specified on the suggested link :

--------------------------------------------------

WPollock says:

Your createLockFile routine uses ln -s to create the lockfile. While it is common practice to do that, as far as I can tell from checking the POSIX docs at OpenGroup.orgImage this is not guaranteed to create a file atomically.
Dave Korn taught me the correct way to do this:
Code:
set -C  # or: set -o noclobber
: > ${__LOCKFILE} 2>/dev/null
LN_RC=$?


--------------------------------------------------

Last edited by rbatte1; 08-31-2016 at 07:32 AM.. Reason: Added CODE & ICODE tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Synchronization process

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: it is assumed that the five processes are available according to the graph (cycle is formed of five process). ... (1 Reply)
Discussion started by: mariam40
1 Replies

2. Solaris

I need to synchronization by rsync

Hi everyone! I wonder about the rsync. The purpose I just want: synchronization from server 1 to server 2 by rsync, example: Node 1: /app/logs/* Node 2: /app/logs/* in Node 1 when I used rsync -aru /app/logs/ node2:/app/logs/, then everything will be copied and update to node 2. But,... (1 Reply)
Discussion started by: trantuananh24hg
1 Replies

3. Shell Programming and Scripting

calling 'n' number of shell scripts based on dependency in one shell script.

Hello gurus, I have three korn shell script 3.1, 3.2, 3.3. I would like to call three shell script in one shell script. i m looking for something like this call 3.1; If 3.1 = "complete" then call 3.2; if 3.2 = ''COMPlete" then call 3.3; else exit The... (1 Reply)
Discussion started by: shashi369
1 Replies

4. Shell Programming and Scripting

FTP Synchronization

I want to do a synchronization from local to ftp. local: name modified time a.txt 10:04 c.txt 10:05 ftp: b.txt 10:00 c.txt 10:05 final result would be a.txt copy to ftp b.txt deleted c.txt nothing to do Is there a good way to do so? Thanks. (3 Replies)
Discussion started by: uativan
3 Replies

5. UNIX for Dummies Questions & Answers

directory synchronization

Can anybody help me on how to do directory synchronization. i have been reading about rsync and filesync but apparently it seems to me that synchronization is from a source directory to a destination directory only. how about if vice versa - wherein i need to synchronize both directories, updating... (3 Replies)
Discussion started by: splakang25
3 Replies

6. UNIX for Dummies Questions & Answers

Server Synchronization

I am new on UNIX system. I am administrating a server belongs to a small department. It has Debian installed, and used as mail, web server. I am trying to synchronize to a different machine which has Fedora installed. Can anybody tell me how to copy exact image of one server to a different for ... (3 Replies)
Discussion started by: kumarrana
3 Replies

7. UNIX for Advanced & Expert Users

Synchronization of 2 directories

I have 2 hosts (server and client), on the client side I mount remote directory (through NFS). How can I synchronize content of 2 directories (one on the client, and one on the server, mounted to the client)? i.e. when client is connected to the server synchronization process is automatically... (5 Replies)
Discussion started by: Hitori
5 Replies

8. AIX

Difference between writing Unix Shell script and AIX Shell Scripts

Hi, Please give me the detailed Differences between writing Unix Shell script and AIX Shell Scripts. Thanks in advance..... (0 Replies)
Discussion started by: haroonec
0 Replies

9. UNIX for Advanced & Expert Users

time synchronization

i have an HP UNIX box w/c acts as ntp server... I tried to change the time plus 8 minutes... the problem is that the other HP UNIX ntp client did not follow the time... when I tried to restart ntp client... using stop start it only sync to the server once... when I issue the command "ntpq -p", w/c... (2 Replies)
Discussion started by: inquirer
2 Replies

10. UNIX for Dummies Questions & Answers

Date synchronization

Hi all Is there an easy way to synchronize the time of a Linux server with another server (Linux or Windows). I need to do this on a daily basis so that my clock does is insync. Is synchronizing with an eternal server hazardous to the security of the box? Thanks in advance KS (2 Replies)
Discussion started by: skotapal
2 Replies
Login or Register to Ask a Question