2014-05-16 215 views
-1

我有一个shell脚本run.shshell脚本无限运行

cd elasticsearch-1.1.0/ 
./bin/elasticsearch 
cd 
cd RBlogs/DataFetcher/ 
mvn clean install assembly:single; 
cd target/ 
java -jar DataFetcher-0.0.1-SNAPSHOT-jar-with-dependencies.jar 

在这里,如果第二线路(./bin/elasticsearch)执行它运行无限的时间,因此,下一个线将不会执行。所以我需要的是在10秒后执行下一行。但

cd elasticsearch-1.1.0/ 
./bin/elasticsearch 
sleep 10 
cd 
cd RBlogs/DataFetcher/ 
mvn clean install assembly:single; 
cd target/ 
java -jar DataFetcher-0.0.1-SNAPSHOT-jar-with-dependencies.jar 

这也不会因为./bin/elasticsearch将无法​​完成其在10秒执行而执行的下一行。那我该如何解决这个问题呢?请帮忙。

+1

您可以在后台运行命令,方法是在后台添加&./bin/elasticsearch& –

回答

0

./bin/elasticsearch的末尾添加&将导致进程在子shell中运行,从而释放当前shell以用于下一个命令。

./bin/elasticsearch & 

在脚本的第二个版本中进行更改,并且应该按照您希望的那样运行。

的更多信息可以从man bash

If a command is terminated by the control operator &, the shell 
executes the command in the background in a subshell. 
The shell does not wait for the command to finish, and the return status is 0. 
0

您可以尝试把它放在后台

&
可以为你

./bin/elasticsearch & 
0

做到这一点。如果你只是想elasticsearch在后台运行,被发现而剧本的其余部分进展,只需使用&

cd elasticsearch-1.1.0/ 
./bin/elasticsearch & 
sleep 10 
cd 
cd RBlogs/DataFetcher/ 

不过,如果你想为最多10秒跑完elasticsearch,如果有必要杀死它,然后用脚本的其余部分继续进行,你需要的东西更复杂一点:

cd elasticsearch-1.1.0/ 
./bin/elasticsearch & 
pid=$! 
sleep 10 
kill -0 $pid && kill $pid 
cd 
cd RBlogs/DataFetcher/ 
0

至于其他的答案所提到的,你可以

  1. 使用./bin/elasticsearch &在 后台运行的命令。
  2. 记录使用child_pid=$!,然后在后台 命令运行的进程ID使用kill $child_pid一段时间后实施 超时机制停止 过程。

同时,您还可以使用wait命令将其他操作与后台运行的命令同步。示例如下:

./bin/elasticsearch & 
# do something asynchronously here 
wait # wait for accomplishment of ./bin/elasticsearch 
# do something synchronously here