Skip to content

String array command with SystemCommandTasklet #752

Closed
@spring-projects-issues

Description

@spring-projects-issues

dgray16 opened BATCH-2863 and commented

Hi. Using this chance I want to thank you for Spring Batch, it is amazing :)

Now, directly to the topic.

 

I want to use

org.springframework.batch.core.step.tasklet.SystemCommandTasklet 

but there an issue.

 

There is method:

org.springframework.batch.core.step.tasklet.SystemCommandTasklet#setCommand

which accepts

java.lang.String

 

My task is simple: run network packet analyzer Wireshark before processing is started.

Bash command looks like this:

 

/bin/bash -c tshark -i enp1s0 -w ~/Documents/log.pcap -f "host 8.8.8.8"

 

The problem is that

java.util.StringTokenizer#StringTokenizer(java.lang.String)

is about to split provided String with Bash command into array, after it command is not working.

 

In this case helps a lot another method

java.lang.Runtime#exec(java.lang.String[], java.lang.String[], java.io.File)

So if I split manually command into this:

new String[] { "/bin/bash", "-c", "tshark -i enp1s0 -w ~/Documents/log.pcap -f \"host 8.8.8.8\""}

Works perfectly.

 

If there is tricky way to hack

java.util.StringTokenizer#StringTokenizer(java.lang.String)

to make it work with my example, I suggest to add JavaDoc above

org.springframework.batch.core.step.tasklet.SystemCommandTasklet#setCommand

 

If not, I suggest to provide possibility to use both forms, String[] + String.

 

And one more thing, there may be opportunity to run sh script by single String method (did not test it), but I am not interested in it. I am interested only in hardcoded bash command.

 

As a workaround for it right now I have written this code:

Tasklet tasklet = (contribution, chunkContext) -> {
    new ProcessBuilder("/bin/bash", "-c", "tshark -i enp1s0 -w ~/Documents/log.pcap -f \"host 8.8.8.8\"").start();
    return RepeatStatus.FINISHED;
};

 

In any case I am ready to create PR based on discussion here if that will be needed.

 


Affects: 4.2.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions