Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String array command with SystemCommandTasklet [BATCH-2863] #752

Open
spring-issuemaster opened this issue Dec 12, 2019 · 2 comments
Open

String array command with SystemCommandTasklet [BATCH-2863] #752

spring-issuemaster opened this issue Dec 12, 2019 · 2 comments

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Dec 12, 2019

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

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Dec 12, 2019

Mahmoud Ben Hassine commented

SystemCommandTasklet#setEnvironmentParams is what you are looking for:

SystemCommandTasklet systemCommandTasklet = new SystemCommandTasklet();
systemCommandTasklet.setCommand("/bin/bash");
systemCommandTasklet.setEnvironmentParams(new String[] {"-c", "tshark -i enp1s0 -w ~/Documents/log.pcap -f \"host 8.8.8.8\""});

The array of arguments will be passed as parameters to the command when calling java.lang.Runtime#exec behind the scene.

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Dec 12, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.