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?
-
./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.Prix : you're missing the important part of what he wants ...n002213f : `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.From James Lawrie -
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.
n002213f : thanks, check my response based on you answer, if its OK, some kind of reviewn002213f : arggghhh, can't upvote :-(Prix : there is a 5 seconds limit between marking and being able to upvote or something like that... but thanks :)From Prix -
#!/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.Prix : updated my answer with your TODO but this should work fine are you having any trouble with it ?From n002213f
0 comments:
Post a Comment