Nohup appending output and error instead of overwrite - java

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.

Related

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

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.

Redirect standard output to a file + ANT + No output on console

I'd like to redirect my standard output that gets printed in command prompt to be printed in a file instead of the console.
I used the Recorder task. And it partially answers my question.
It does help print the output to the file but I can't prevent the output from being printed on the command prompt as well.
Most questions have been answered with the recorder task itself but nothing specific for my query.
Question:
Is there any way in which I can only have the output printing take place in the file and NOT on my console?
You can achieve by running ant script like this(Assumed you running in Windows)
ant <targetname> >\path\to\out\file.txt
This command redirects everything to test file.

command does not execute successfully when run through java code using runtime.getruntime command

I have a command which executes successfully when run directly in a command prompt but when same command is run through java code using runtime.getruntime.exec("command"); it does not give the desired output. why is it not running properly??
The command i am executing is to change the admin password and the command output i want to redirect into a file. The command is as follows:
Process p=Runtime.getRuntime().exec("net user administrator 1234 > yjs.txt 2>&1");
if i directly run "net user administrator 1234 > yjs.txt 2>&1" in my command prompt it executes correctly i.e. the text file yjs.txt is created and the ouptut is redirected into the file..
but when the same command i execute through a java code the file yjs.txt is not created at all.. It does not even give an error message when compiling. Any idea why??
Your command prompt does much more than just run an executable, which is what Runtime.exec() does. For example, the output-redirection (> yjs.txt 2>&1) is a feature of your command prompt, but not a feature of Java.
If you want to redirect the output to a file, you have two options:
put the full command including the output-redirects into a batch-file (or shell-script, depending on your OS) and execute that batch-file from Java.
use the Process object returned by exec() and write the output- and error-stream you receive from that object to a file yourself.
Actually, you should do the second case anyway. If the command you start generates lots of output, it may start blocking if that output isn't consumed by your code. Have a look at http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html, it explains in large details the possible pitfalls of Runtime.exec() and also provides solutions for how to deal with it (for example by using the StreamGobbler in listing 4.5 of the article).
This is because the default sub-process created by the Runtime.getRuntime.exec() command does not have its own terminal or console. All its standard I/O (i.e., stdin, stdout, stderr) will be redirected to the parent process and they can be accessed via getInputStream/getOutputStream/getErrorStream.
You may try reading the output of the command "net user administrator 1234" through p.getOutputStream and write it explicitly to yjs.txt file using File I/O
Also, ProcessBuilder is the preferred way of executing commands unless you are stuck with ancient JVM. Refer http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html for examples and documentation

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

Running a Command Prompt from a Java program in Windows

I currently have the following batch script I want to run from my Java program:
"C:\Program
Files\Java\jdk1.6.0_25\bin\java.exe"
-classpath "D:..."
Main >
"...\result.out"
Now, I've done a simple
Runtime.getRuntime().exec(command);
where command is that string I have shown above. The problem is that it is simply calling java.exe with the shown arguments, instead of calling the console with the given arguments. The difference is subtle, for if it is calling directly java.exe it will ignore the redirect of the output stream!
Is there a easy way to do this? I've tried prefixing command with "cmd " but that didn't seem to help.
I'd like to stay away from having to read the output stream and then having to manually save this to a file.
Thanks
To solve the issue,
cmd /c "command"
is enough.
Process proc = Runtime.getRuntime().exec("acpi -b");
Now you can use proc.getInputStream() and proc.getOutputStream() like any normal input and output streams.
You can then write the contents to your output file.
This is the method I mostly use.

Categories

Resources