How to print all jar results into a file using bash script - java

I've been trying to do this simple script that I wrote where it runs an executable jar file that I made. The command of the script are as follows:
#!/bin/bash
msisdn=$1
java -cp /home/support/phuzca/Migration/PostpaidXMigration_lib/ -jar /home/support/phuzca/Migration/PostpaidXMigration.jar $msisdn /home/support/phuzca/Migration/config.properties /opt/tomcat9/webapps/axis2/WEB-INF/classes/META-INF/PlanID.xml
The jar file works as expected and I receive the expected results:
The idea that I've been trying to figure out is how to prevent those texts from appearing when I run my script, and instead, print them in a file so it can be reviewed later. I hope you could open up some ideas for me. Thank you very much.

Redirect the output to a file:
migrateToPstopaidX.sh > output.log
if you want to redirect stderr use this
migrateToPstopaidX.sh &> output.log
you can use this >> to append the log instead of >

Redirect both stdout and stderr to the output file.
migrateToPstopaidX.sh > output.log 2>&1
You can use >> to append instead of overwriting your file.
Bash executes the redirects from left to right as follows:
>: Open output.log in overwrite mode and redirect stdout there.
2>&1: Redirect stderr to "where stdout is currently going". In this case, that is a file opened in append mode. In other words, the &1 reuses the file descriptor which stdout currently uses.

Related

how to disable print function in python

I have a python code that launches jar file
subprocess.call(['java', '-jar', 'jarfile.jar']
The jar file prints some errors to the console.
I can't modify jar file.
How can I block those warnings?
Still, I want my python output to be printed in console. Just those jar warnings.
This could be seen as more of a shell question.
You could throw the output into /dev/null
java -jar jarfile.jar > /dev/null

Windows batch file > appears to modify input to java command in the .bat file

I have a Windows .bat file which contains a java call. The java call looks something like this:
java -Dfile.encoding=Cp437 -jar name.jar -p "path\filename.prop"
The filename.prop file contains several parameters used by the java program.
I am running this .bat file directly from the command window.
The .bat file works fine as long as I allow it to output the log to the command window. However, if I add redirection of the log, it causes errors indicating that it is NOT looking for the file defined in the -p parameter as it should be.
So:
myfile.bat - works, but writes log information to the command window
myfile.bat > filename.log - stops immediately with errors written to command window.
myfile.bat > filename.log 2>&1 - stops immediately with errors written to the log file.
The error produced is this:
java.io.FileNotFoundException: path\filename.prop (The system cannot find the path specified)
where path\filename.prop is NOT the value of the -p parameter in the .bat file. (Note: I did not write the java program which is being called. I suspect that the value of path\filename.prop that I am seeing in the error is possibly one that is hardcoded in the program from testing times, but I am not certain.)
How can redirecting the log output have an effect on the parameters read by the java call inside the .bat file? Is it somehow preventing the java call from "seeing" the -p parameter? What am I missing?
What do I need to change to allow the java call to use the -p parameter in the .bat file?

How do I get java to output to a log when run from a batch?

I have a java file called "Ares.jar" that runs every 5 minutes via windows scheduled tasks, the batch calls:
java -jar Ares.jar >> Ares.log
I'm not seeing any error output, is there some way to make the errors (system.err.println / exception.printStackTrace(); )go to a file?
Thanks.
java -jar Ares.jar > Ares.log 2>&1
The second part of this command will redirect stderr to stdout, ensuring that both appear in the same file.
If you want regular logs and error logs in separate files, just use:
java -jar Ares.jar > Ares.log 2>Ares.error.log
You can get the full details of everything that is possible in the documentation, available from Microsoft: http://technet.microsoft.com/en-us/library/bb490982.aspx

How to redirect output by a diffrent process initiated by java

I have a java program running on Linux machine.
It invokes an ant target to execute.
I can see the logs on the screen printed but there is no log file, which was produced when i used to run it using shell script.
I want to redirect the screen logs into a file.
I tried "tee" to redirect but it redirects only the part that i am printing in java class.
Can some one help me with this.
Inside your build.xml file, you can start and stop recording to a log file:
<record name="file.log" action="start"/>
...
<record name="file.log" action="stop"/>
See the appropriate ant documentation for more details.
If you'd rather not edit the build.xml, it is probable that ant is using stderr rather than stdout. Find out what shell you are using and look up how to redirect stdout to stderr. For example, in bash, it would be ant 2>&1 | tee file.log.

Nohup appending output and error instead of overwrite

I am using nohup to append the java output and error to the same log file. The problem is it writes an output and then it overwrites the log file for error and output is erased.
The nohup command is
nohup java Daemon 1000 >logs/wrapper.log 2>logs/wrapper.log &
This is the message I want to log in wrapper.log from the Daemon.java
System.out.println("This is output that should go to the file");
System.err.println("This is error that should go to the file");
But only the last message is written in the file. The most reasonable answer is to know how to append the wrapper.log for outputs and errors and not the overwrite
Any ideas
Thanks
The most portable (and my preferred method) is:
cmd >>logs/wrapper.log 2>&1 &
the >>FD redirect opens with the O_APPEND flag. cello's answer is a bashism (and most kshes and zsh) to redirect both stdout and stderr at once, but doesn't solve the problem of opening in append mode.
See: http://mywiki.wooledge.org/BashPitfalls#somecmd_2.3E.261_.3Elogfile and associated links.
EDIT: I see this doesn't actually address the append problem. I'll edit the page. The links are still relevant.
most likely, the error-out overwrites the standard-out, as you both write to the same file.
try something like:
nohup java Daemon 1000 &> logs/wrapper.log &
&> works on some shells (bash in any case, not sure about tcsh/zsh/...), so try it out or let us know which shell you are using.

Categories

Resources