Using nohup in Ubuntu - how to make it not show the job number?

0

1

I'm running this command:

$ nohup command > foo.out 2> foo.err < /dev/null &

My problem is that even though nohup makes my command run in the background, it prints out something like this to my terminal:

[1] 27918

How do I make it not output the job number? I just want it to do it in the background without telling me anything. On Mac OS X, that's exactly what happens, so I'm a little annoyed that it works differently on Ubuntu...

Thanks for the help!

hora

Posted 2011-03-01T08:45:23.140

Reputation: 156

2The [1] 27918 is coming from the & not the nohup – Majenko – 2011-03-01T08:50:53.760

Thanks Matt, that did it. Now how do I close this question... – hora – 2011-03-01T08:53:52.803

The way to do that is mark an answer as accepted using the checkmark next to it. – Paused until further notice. – 2011-03-01T10:06:09.177

See http://superuser.com/faq section "How do I ask questions"

– user1686 – 2011-03-01T10:10:12.863

Answers

4

Job information is displayed by your shell, not nohup.

You could try this alternative:

(yourcommand&)

( spawns a subshell, which handles job control differently.

(I have nh() { ("$@" &); } in my ~/.bashrc, so I can type nh command to do the same thing.)

Another, different way:

setsid yourcommand

Edit: It seems that (setsid yourcommand &) is the best combination, since it detaches from the tty and works equally in interactive and script modes.

user1686

Posted 2011-03-01T08:45:23.140

Reputation: 283 655

(command &) worked for me where I was having ioctl error with nohup. – Shahzada Hatim – 2012-05-10T12:22:25.150

1

The [1] 27918 is coming from the & not the nohup.

Put the command you want to run in the background inside a script file:

#!/bin/sh
/path/to/command & >/dev/null 2>/dev/null

and then call that with nohup

nohup sh /path/to/my/script > foo.out 2> foo.err < /dev/null

The output of & is then redirected to /dev/null inside the script.

Majenko

Posted 2011-03-01T08:45:23.140

Reputation: 29 007

& is only a command terminator, it doesn't output anything and you cannot redirect it. In interactive mode, the job line is printed by the shell; in non-interactive (including scripts), it remains quiet. Your inner redirects apply to output of the command itself, and I do not think the outer redirects will override them. – user1686 – 2011-03-01T09:05:44.243

1

Just as the one above mentioned (Thanks, by the way).

Within a script named test.ksh:

#!/bin/ksh
./test2.ksh >/dev/null
nohup ksh test2.ksh < /dev/null
echo  "whatever"

Where test2.ksh is another script (or may be a command):

#!/bin/ksh
max=10
for ((i=2; i<=$max; ++i )) ; do
    echo -e "\n Hello world "
done

Then, run:

~> ksh test.ksh

and the output is:

nohup: appending output to `nohup.out'
whatever
~>

It worked for me

If you keep the & in the redirection

./test2.ksh & >/dev/null

It will print both in the tty and the file .out

user293847

Posted 2011-03-01T08:45:23.140

Reputation: 11