在终止前一个命令之前不执行第二个命令

时间:2020-02-16 20:24:09

标签: bash neo4j

我是bash的新手。我正在运行bash脚本。应该启动Neo4j,然后执行名为“ cypher.ex1”的文件中的一系列查询。这是代码:

#!/bin/bash
./bin/neo4j console
./bin/cypher-shell -u neo4j -p 123456 --file cypher.ex1

为了使用Cypher-shell,我们应该首先启动Neo4j服务。因此,这一行:

./bin/neo4j console

启动Neo4j,以便可以使用以下行来使用cypher-shell:

./bin/cypher-shell -u neo4j -p 123456 --file cypher.ex1

问题在于,由于./bin/neo4j console启动了Neo4j服务,除非我按下“ Ctrl + C”,否则将不执行下一个命令(./bin/cypher-shell -u neo4j -p 123456 --file cypher.ex1)。如果按“ Ctrl + C”,Neo4j服务将停止,并且以下命令也将不会执行(出现“拒绝连接”错误)。为了启动Neo4j服务,然后在此bash脚本中运行cypher shell,我应该怎么做?

我尝试了Run a command in background and another command in frontground in the same line中给出的解决方案。他们都没有为我工作。例如,当我使用“((command1&); command2”)执行代码时(如建议的主题中所建议),我的脚本将自动执行2次。第一次执行command2,由于未执行command1,因此出现“连接被拒绝”错误;第二次执行command1而未执行command2。

1 个答案:

答案 0 :(得分:0)

正如在问答中第一个答案中提到的那样,您应该以这种方式执行命令:

#!/bin/bash
./bin/neo4j console &
./bin/cypher-shell -u neo4j -p 123456 --file cypher.ex1

这将在后台运行./bin/neo4j console。因此,您需要注意此过程并在需要时停止它:

PID=$(jobs -l |awk '{print $2}')
kill $PID