通过阻塞顺序启动Linux脚本

我想要创build一个脚本来依次启动两个不同的脚本。

第一个脚本启动一个应用程序服务器,虽然进程已经启动(而且我回到了提示符),但它只会在其日志中的“特定”消息之后接受连接。 服务器Blah Blah开始了!

第二个脚本必须连接到服务器,并做一些额外的东西。

我怎样才能创build一个启动脚本,第二个脚本只会在第一个脚本之后启动?

你将如何做你想要在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; 

使用上面的代码,只有在第一个程序完成打印输出时find该单词,才会运行第二个程序。

更新:如果程序不输出你想要的,但有一个日志文件,它会写你需要的信息,你可以在Perl中使用它,所以你没有任何限制。

./script1 && ./script2
&&表示“只有在第一部分完成成功后才执行第二部分,不同于:
./script1; ./script2
第一部分是什么,然后是第二部分。 本质上,虽然没有分叉或线程(有些方法具有这种内在的特性),但几乎所有的编程都是必不可less的,一次只做一件事(到最后)。 如果你需要保持,用你的语言编写一个循环,直到(无论条件)成立为止。

 #!/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.