Linux slightly different behaviour If we give '&' before io redirection '>' and after redirection

0

1

I was starting jboss recently in background in Linux and saw that if you run command as follows:

nohup ./startjboss.sh > server.log &

Output is :

[1] 18835
[root@cnt5-01b downloads]#

The terminal for the next command appears directly.

However, if I run the command as follows:

nohup ./startjboss.sh & > server.log

Then the output is:

[1] 19223
[root@cnt5-01b downloads]# nohup: appending output to `nohup.out'

Then, when I press enter, it returns to:

[root@cnt5-01b downloads]#

which is the terminal where I can write next command.

Why is there a difference in behavior (an extra Enter needed)? This is a very small thing, not even a problem; but I just want to know.

Gaurav Fotedar

Posted 2014-12-10T14:05:24.280

Reputation: 133

Answers

1

When you place the &, you're telling the shell to run what precedes it in the background and continue with a new command.

When you put it at the end of the line, there's no command after so the shell returns to interactive mode.

When you place it in the middle, the shell interprets the rest of the line as a new command. That command redirects the standard output of nothing to server.log. Since you're not redirecting the output of nohup, you now see it on the terminal. Since the shell had already redrawn its PS1 before, you see that line of output as if it were a command, but it's simply output from a background job. You can type your new command without even pressing enter before (though it's not that clear when you read it later).

user2313067

Posted 2014-12-10T14:05:24.280

Reputation: 2 160

0

Well basically, when you run a command with & after it, you are loosing some functionality.

Take this as an example, make a script that does nothing but returning 2 for example.

  • run the script normally, issue echo $? and you will see the output 2.
  • run the script with & after it, run echo $? and you will see the output 0.

Also, when running nohup ./startjboss.sh & > server.log, if you check the server.log file, you'll see that is empty, that's because the output of your background process will be 0 on successful termination, but because you didn't echo it into server.log, so nothing will be actually written to server.log.

MoonDrop legacy

Posted 2014-12-10T14:05:24.280

Reputation: 21