output for nohup following bash command

110 Views Asked by At

nohup can run a command immune to hangups, with output to a non-tty. Let's run a simple command with nohup following bash command:

nohup ls -al &

enter image description here I have two issues here:
1.process 10539
For the first output line,it is [1] 10539 ,means that there is a process 10539.

I open other terminal window to execute:

sudo lsof -i:10539    
#nothing in the output

Which command result in the process number 10539?nohup ls -al & or ls -al & or ls -al?

2.Why nohup: ignoring input and appending output to 'nohup.out' as a new command in the terminal,and cursor is waiting for something?

With a long bash command to test:

debian@debian:~$ nohup  sleep 100000 &
[1] 12021
debian@debian:~$ nohup: ignoring input and appending output to 'nohup.out'

And execute in a new terminal:

sudo lsof -i:12021
#nothing for the command
1

There are 1 best solutions below

0
Gordon Davisson On

1: You're using the wrong option to lsof. Its -i option selects "files" that're actually internet ports, not processes (and -i:10539 selects just those bound to TCP or UDP port number 10539). You want the -p (process ID) option: sudo lsof -p 10539. Check man lsof for more details.

2: nohup: ignoring input and appending output to 'nohup.out' is not a new command, it's just output that's landed where you expect your command entry to be. And the only thing it's waiting for is for you to type another command in.

With nohup somecommand &, the & sort of happens before the nohup. That is, it's running the command nohup somecommand in the background, not nohupping somecommand &.

So what happens is that your shell fires off nohup somecommand in the background, and then immediately prints the prompt (debian@debian:~$ ) to indicate that it's ready for you to enter another command. Then after that, the nohup command in the background gets its bearings, figures out that it needs to redirect I/O, does the redirection, and then prints a message (nohup: ignoring input...) saying that it's done the redirection.

nohup doesn't know that this message will be printed after a shell prompt (and therefore look like it's a command), it's just sending the text to the terminal, and the terminal is just printing it like it's been told to.

Also, since the message ends with a newline, the terminal moves the cursor to the beginning of the next line, so it looks like you're no longer at the shell's command prompt. But this is just superficial; as far as your shell is concerned, it's just printed the prompt, and is ready for you to enter a new command. Try it -- just type in somet ordinary command and hit return, and it'll be executed normally.

If you want the screen to look less confusing, add a short delay to the command, like nohup ls -al & sleep 1. This'll make your shell wait a second before printing its prompt, giving time for nohup to print its message in a less confusing spot.