LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-02-2012, 05:58 PM   #1
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Question Wait for a remote script to finish before executing another


Dear All,

I am sitting on box1 and want to zip (tar.bz2) multiple directories on box2 and then fire another command to scp the tar balls from box2 to box1.

I have set up ssh keys but still using 'perl expect' as I am using sudo commands (which asks for an extra password).

My problem is that scp starts before the first script (to create tar balls) finishes

This is a piece of my code:
Code:
 
echo "ssh -t $host \"sudo bash /tmp/tar_template\"" > cmd 
pass
 
echo "scp -r $host:$RSERVER_WORKING_DIR/$dt1 $NFS_MOUNT_POINT/$host/" > cmd
pass
Here, /tmp/tar_template is the script which is creating tar balls and pass is the function which is providing the password.

Any ways to execute the second command only after the first finishes. I can put sleeps but that will not be appropriate since time to zip (tar.bz2) the directory varies.

Another way would be to put a indefinite loop to check a lock file on the remote server and run only when it is deleted by the first command/script, BUT I dont want that since it will unnecessary have multiple login entries.

I tried some combination of wait command, but that did not worked for me.

Please help. Thanks in advance.

Last edited by vikas027; 08-02-2012 at 06:01 PM. Reason: made post more readable
 
Old 08-04-2012, 04:05 AM   #2
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,350

Rep: Reputation: 127Reputation: 127
Put && after the first script.
Script 2 will then be executed only if script 1 finishes without failure.
 
Old 08-04-2012, 01:21 PM   #3
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by pingu View Post
Put && after the first script.
Script 2 will then be executed only if script 1 finishes without failure.
Hi Pingu,

Putting && will not work since the commands (or scripts) will run on another server and on the same server.

To elaborate, suppose I create two functions abc1 and abc2

Code:
abc1 ()
{
echo "ssh -t server2 \"sudo bash /tmp/tar_template\"" > cmd 
pass
}
 
abc2 ()
{
echo "scp -r server2:$RSERVER_WORKING_DIR/$dt1 $NFS_MOUNT_POINT/$host/" > cmd
pass
}
Now, if I run abc1 && abc2 on server1, abc2 will be executed immediately after abc1 since it is running on a different server (server2) and server1 has no way to know that the command/script has been completed or not on server1.

I hope I am clear
 
Old 08-04-2012, 02:58 PM   #4
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,350

Rep: Reputation: 127Reputation: 127
Ah, sorry of course you're right.

So, on Box1 you need a way to run script-2 only when script-1 on Box2 is finished.
I can see 2 ways to do this. (Ok, it's ten-o-clock in the evening, I've worked hard and had 2 glasses of wine. I still know what I'm doing but please keep that in mind, I might miss some details.)

1) Scp the other way, meaning you put everything in one script on remote host.
You already have keys set up, so all you need do then is remove password prompting from box2 -> box1.

2) A bit more complicated, but also more elegant if you ask me:
a) Run script-1 on Box1, the one that starts a tarball-creating on Box2. Put the tarball in a dedicated directory (from now on called "DDIR") on Box2.
b) On Box2, use inotifywait to watch DDIR with "close write".
Make a script on Box2 that uses inotifywait so that when a file is written to DDIR a file is created with name "tarball_complete".
c) On Box1, create a script that watches for this file on Box2, and downloads the tarball when the "tarball-complete"-file exists.
Easiest is to make it a cron-job, or you could have a loop in a small script to check for that file.
Of course, you'll have to provide your password every time the script is checking. If I were you I'd run those scripts under a special, very restricted user-account and use only ssh-keys.
 
Old 08-07-2012, 01:48 AM   #5
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by pingu View Post
Ah, sorry of course you're right.

So, on Box1 you need a way to run script-2 only when script-1 on Box2 is finished.
I can see 2 ways to do this. (Ok, it's ten-o-clock in the evening, I've worked hard and had 2 glasses of wine. I still know what I'm doing but please keep that in mind, I might miss some details.)

1) Scp the other way, meaning you put everything in one script on remote host.
You already have keys set up, so all you need do then is remove password prompting from box2 -> box1.

2) A bit more complicated, but also more elegant if you ask me:
a) Run script-1 on Box1, the one that starts a tarball-creating on Box2. Put the tarball in a dedicated directory (from now on called "DDIR") on Box2.
b) On Box2, use inotifywait to watch DDIR with "close write".
Make a script on Box2 that uses inotifywait so that when a file is written to DDIR a file is created with name "tarball_complete".
c) On Box1, create a script that watches for this file on Box2, and downloads the tarball when the "tarball-complete"-file exists.
Easiest is to make it a cron-job, or you could have a loop in a small script to check for that file.
Of course, you'll have to provide your password every time the script is checking. If I were you I'd run those scripts under a special, very restricted user-account and use only ssh-keys.
Many thanks for your time and suggestions Pingu.

I am not authorized to do much changes on the servers , I have to manage from current setup only.

I have managed it to by creating a lock file in the start of the script-1 and delete it when it finished. I am checking the lock file at a particular interval and this solved my issue. But frankly, I am not happy with it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
executing shell script in background on remote machine aggrishabh Linux - Newbie 3 12-12-2011 09:14 PM
[SOLVED] bash wait for one line to finish before going to the next line Mike_V Programming 6 01-10-2011 06:20 PM
How to create a bash script to wait for wget to finish and then continue danlee Linux - General 14 05-30-2008 11:21 AM
bash-script won't wait for application to finish TLV Linux - Software 24 09-30-2004 11:18 PM
Pppd does not wait for chat script to finish ruchika Programming 1 08-28-2003 11:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration