Redirecting output using the screen command

This may be a simple problem, but I am running the CentOS 5.4 command line remotely. I want to redirect the output of a simple Java file, let's say a loop to print one hundred thousand numbers in the console in a text file. The fact is that I have to use the "screen" command to be able to run it in the background, even if I lost a session from a remote computer, and this command does not write to the desired file.

I tried the screen java MyClass >& log.txt method screen java MyClass >& log.txt also screen java MyClass > log.txt , but it does not write to the file. Why is this happening and is there any solution?

Thanks!

+7
source share
2 answers

You can do this with the nohup command. Here is an example.

 $ cat Foo.java public class Foo { public static void main(String[] args) throws InterruptedException { for(int i = 0 ; i < 1000 ; i++) { System.out.println(i); Thread.sleep(1000); } } } $ javac Foo.java $ nohup java Foo > foo.txt & [3] 29542 $ cat foo.txt 0 1 2 3 4 5 $ exit 

<<restart shell β†’

 $ cat foo.txt 0 1 ... 29 30 

The reason this does not work with screen is because screen does not interpret your arguments like the shell does. If you did this with screen , it would work:

 screen /bin/bash -c 'java Foo > foo.txt' 
+15
source

sample_script.sh

 #!/bin/bash #start screen in detached mode with session name 'default_session' screen -dmS "default_session" #redirect output to abc.log screen -S default_session -X stuff "script -f /tmp/abc.log\n" #execute your command screen -S default_session -X stuff "your command goes here...\n" 
+1
source

All Articles