Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to create a script to start two different scripts in sequence.

The first script start an application server, which although the process has started (and i'm back at the prompt), it will only accept connection after a 'certain' message in its log; 'Server Blah Blah started!'.

The second script essential connects to the server and does some extra stuff.

How can i create a startup script such that the second script will only start after the first?

share|improve this question
    
You could do that with perl and filter teh response to start the next app. –  Prix Aug 18 '10 at 13:35
    
Depending on what you want to do, you might also want to consider starting or triggering the second script by the application server. This could be on the same machine, or on another machine via ssh, or even by having a daemon (eg knockd) listening on the first server, and the app server sending a packet (eg knock), once it's ready. –  Chris Lercher Aug 18 '10 at 14:58
add comment

3 Answers

up vote 0 down vote accepted

A sample of how you would do what you want in perl.

#!/usr/bin/perl

use strict; 
use warnings;

# Start the first script
my $first_script = `/script1.sh`;

my $string_to_find = 'Server Blah Blah started';
my $string_not_found = 1;
my $counter = 0; # counter ofcourse start at 0
my $timer = 300; # since your sleep is set for 1 second this will 
                 # count to 300 before exiting

while($string_not_found){
    exit if ($counter > $timer); # this will make the application exit 
                                 # if the give timer is hit
                                 # you can aswell replace it by
                                 # $string_not_found = 0 if...
                                 # if you want it to run the 2nd code
                                 # as an attempt instead of only exiting
    my $last_line = `tail -1 /my/app/log/file.out`;
    if($last_line =~ /$string_to_find/ig) {
        $string_not_found = 0;
    }else {
        # sleep a little
     $counter++;
        sleep 1;
    }
}
# By the time we are here we are good to go with the next script.
my $second_script = `/script2.sh`;
print 'Finished';
exit;

With the above code it will only run the second program if the word was found once the first program finish printing it is output.

UPDATE: If the program does not output what you want but there is a log file where it will write the info you need, you can use it within perl aswell so your not limited at all.

share|improve this answer
    
thanks, check my response based on you answer, if its OK, some kind of review –  n002213f Aug 18 '10 at 14:49
    
arggghhh, can't upvote :-( –  n002213f Aug 19 '10 at 5:29
    
there is a 5 seconds limit between marking and being able to upvote or something like that... but thanks :) –  Prix Aug 19 '10 at 5:57
add comment

./script1 && ./script2
The && means "only execute the second part once the first part has completed successful, different to:
./script1; ./script2
Which does the first part, then the second part. Essentially though, without forking or threading (some methods have this inherently), pretty much all programming you do is imperative and does one thing (to the end) at a time. If you need to hold, write a WHILE in your language that loops until (whatever condition) is true.

share|improve this answer
    
you're missing the important part of what he wants ... –  Prix Aug 18 '10 at 13:47
    
script1 uses nohup to start a process and returns before the process has completed started up and ready for connections. Using both examples you gave means script2 will start irrespective of the status of process started by script1 I will check the while loop option. –  n002213f Aug 18 '10 at 13:48
add comment
#!/usr/bin/perl

# Start the first script
$first_script = `/script1.sh`;

$string_to_find = 'Server Blah Blah started';
$string_not_found = 1;
while($string_not_found){
    $last_line = `tail -1 /my/app/log/file.out`;
    if($last_line =~ /$string_to_find/ig) {
        $string_not_found = 0;
    }else {
        # sleep a little
        sleep 1;
    }
}
# By the time we are here we are good to go with the next script.
$second_script = `/script2.sh`;
print 'Finished';
exit;

#TODO: Add some sort of counter to abort incase we don't get the string we are searching.
share|improve this answer
    
updated my answer with your TODO but this should work fine are you having any trouble with it ? –  Prix Aug 18 '10 at 15:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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