I need the launch jar
process and sent request to him:
java -jar parser-0.0.1-SNAPSHOT.jar
curl -d '{"query":"'"$query"'", "turnOff":true}' -H "Content-Type: application/json" -X POST http://localhost:8080/explorer > FQ
But, when the .jar
file is successfully launched - the system stops and not moved to next step (curl...
)
How can I completely wait for the process to complete and send request?
There are a few problems here:
exit
, or it could mean the java process fails for some reason. The "system" has not stopped, but the currently running shell process is waiting for thatjava
process to return. Maybe that Java code never callsexit
, in which case your script will wait forever.&
for example) then the next command may try to connect before the Java program has fully started and is ready to serve requests. Your Java program and the curl command are "racing" on a startup condition. Now your script may have to have a retry mechanism that will always be rather brittle.kill
, for example. Restarting would require a lot of complicated scripting. This means you have to script up a way to collect the PID, and maybe even maintain a "semaphore" file so you can handle the start/stop state.One solution is to install the Java program launcher as a separate "service" which is controlled by the Ubuntu service startup mechanism. I'm hand-waving here, but I'm positive this is discussed in other Q&A. But the idea is that the lifecycle of the Java program is maintained by the system, not a shell script you run at the terminal. Or, at least, not just as a shell script only you run at the terminal.
Once the service is running then you can script
curl
commands to your heart's content.But it is good design to decouple any server activity from any client activity. Though, your client requests should always handle the case that the server cannot be reached for any reason, even if that handling is an error message back to the user.
Finally, if all you want to do is start a Java service and let it startup so you can later issue commands at the command line or in a script to connect to that service, an easy way to test this scenario is to open two terminals. One is where you run your service and the other is where you run your client curl commands.