简体   繁体   中英

Calling cat from a shell which is in turn called by java exec

I have a shell script test.sh that does:

cat /home/tomcat/temp/tempLogFile.log > /home/tomcat/temp/logFile_test.log

I call test.sh from java by Runtime.getRuntime().exec("test.sh")

It creates the logFile_test.log but nothing written to it.

If I run the script directly from shell, it works fine. What could be going wrong?

Thanks,

UPDATE: Interstingly, it works fine if I do

echo 'cat /home/tomcat/temp/tempLogFile.log > /home/tomcat/temp/logFile_test.log' | at now

But I cant use at now since I need to wait for cat to finish

Maybe your command path is messed up? That command will create logFile_test.log even if it can't run the cat program. Try adding something like:

set > /tmp/dump.txt

to the shell script. This will write a copy of the process's environment variables to /tmp/dump.txt . Inspecting those might help you figure out the problem.

Another possibility is that the disk is full. In that case, you'd be able to create empty files, but not write any data to them.

Once you invoke the getRuntime().exec("ANY_COMMAND"); , it returns you an instance of Process, using process object you can get Input and Output Streams and then pass any other command to the same process or read the output , for that purpose you have three streams connected

  1. Output Stream :- Gives you Control to send more arguments or commands to the same process
  2. Input Stream :- Gives you Control to read the values from the stdout of the process
  3. Error Stream :- Gives you all the errors that were occured during execution of any command.

even if you have any errors, you can check the error stream and see whats going wrong.

you may refer Java process.getInputStream() has nothing to read, deadlocks child

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM