Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script exit too early due to docker pull, while running through ProcessBuilder

I have the following problem which seems to be caused by the "docker pull" in my shell script, as the pull works concurrently

#!/bin/bash   
#VARIABLES
    NAME="my-app"
    IMAGE="my-image:latest"
#DOCKER
    docker stop $NAME
    docker rm $NAME
    docker pull -q $IMAGE
    docker run --name $NAME -d -p 1234:8080 --log-opt fluentd-address=localhost:2233 $IMAGE

Running the script through the terminal works just fine everything works as expected. But when I run it with the Java's ProcessBuilder the script exits much quicker and it seems that it skips the "docker pull" step. As i am not a Java developer and I am not very well familiar with the Language I have the feeling that is something related to the multi-concurrent nature of the docker pull command and the way how the Java Process Builder executes the shell script

The Java class that runs the shell script is this

try {

    Collection<Task> tasks = taskService.getProjectTasksByProjectKey(projectId);

    Task findTask = findTaskByTaskId(tasks, taskId);
    if (findTask.getTaskId() != null) {
        ProcessBuilder pb = new ProcessBuilder(findTask.getCmdPath());
        Process process = pb.start();

        String output;

        try (InputStream in = process.getInputStream();
             InputStream err = process.getErrorStream();
             OutputStream closeOnly = process.getOutputStream()) {
            while (process.isAlive()) {
                long skipped = in.skip(in.available())
                        + err.skip(err.available());
                if(skipped == 0L) {
                    process.waitFor(5L, TimeUnit.MILLISECONDS);
                }
            }
            output = loadStream(in);
        } finally {
            process.destroy();
        }

//                String error  = loadStream(process.getErrorStream());
//                int rc = process.waitFor();

//                log.debug("exit code ->>> " + rc);
//                StringBuilder output = new StringBuilder();
//                BufferedReader reader = new BufferedReader(
//                        new InputStreamReader(process.getInputStream()));
//
//                String line;
//
//                while ((line = reader.readLine()) != null) {
//                    output.append(line + "\n");
//                }
//
//                int exitVal = process.waitFor();
//                if (exitVal == 0) {
//                    System.out.println(output);
//
//                    return output.toString();
//                } else {
//                    //abnormal...
//                }

        return output;
    }
    else {
        throw new InvalidTaskModelException(taskId);
    }
} catch (InvalidModelException e) {
    throw new InvalidModelException(projectId);
} catch (IOException e) {
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

return null;
}

private static String loadStream(InputStream s) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(s));
StringBuilder sb = new StringBuilder();
String line;
while((line=br.readLine()) != null)
    sb.append(line).append("\n");
return sb.toString();
}

The commented lines are different ways I tried to do it.

If anyone encountered a similar problem any help would be much appreciated!

like image 669
Atanas Kovachev Avatar asked Feb 05 '21 12:02

Atanas Kovachev


1 Answers

It is good that you already take care for the processes' STDOUT and STDIN. But rather than skipping copy them to System.out so you can see what is going on. I suspect something is not going as per your expectations.

Looking at the bash script you posted and the fact you are trying to run several processes: Is it possible your java code is running the bash script line by line? Be aware your java program it is not a BASH interpreter, so e.g. variable substitution should not work.

like image 134
Hiran Chaudhuri Avatar answered Nov 11 '22 04:11

Hiran Chaudhuri