0

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?

  • Possibly a duplicate of a sort: https://askubuntu.com/q/88091/459652 (Assuming that the jarfile you are running starts an HTTP server listening at port 8080, and you need that to be in the background so the next command actually gets run.) –  Nov 16 '18 at 21:45

1 Answers1

2

There are a few problems here:

  1. The java command will not return until the process it starts exits and returns control to the shell. This could mean it starts and runs the code in the jarfile, which then eventually calls 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 that java process to return. Maybe that Java code never calls exit, in which case your script will wait forever.
  2. If you put the JVM you are launching in a background shell (via & 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.
  3. There isn't an obvious way for you to control the lifecycle of the Java program you are running. Once in the background you have to manage stopping it (for example) with something like 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.