Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

problem with exit while using nohup

16 views
Skip to first unread message

Sumir

unread,
Jan 20, 2009, 3:47:56 PM1/20/09
to
Hi,

I am kinda confused with this, am not sure what is happening

i have a script say test.sh
----------
cat myfile | while read line
do
exit 2
done

echo "out of loop"
-----------

as it is evident, the exit should cause the script to terminate ,hence
producing no output for the script.

now when i run the script as "./test.sh", the behaviour is as
expected.
but when i run it with nohup as "nohup ./test.sh &", the "out of loop"
statement appears in the output, indicating that the exit just gets it
outside the loop but does not exit the program.

any ideas on this ?

Mark Wooding

unread,
Jan 20, 2009, 6:04:52 PM1/20/09
to
Sumir <sumir...@gmail.com> writes:

How odd. None of the shells I've tried have exited the shell from
inside the loop.

What's happening is that the `while' loop is being run in a subprocess;
so `exit' is exiting that subshell. You can verify this by printing
`$?' in the final `echo'. The POSIX spec allows this behaviour (IEEE
1003.1 Shell and Utilities 2.12):

: Additionally, each command of a multi-command pipeline is in a
: subshell environment; as an extension, however, any or all commands in
: a pipeline may be executed in the current environment.

> any ideas on this ?

My guess is that you're using different shells in the two cases.

-- [mdw]

Dawood Sangameshwari

unread,
Jan 21, 2009, 12:58:18 AM1/21/09
to
when you run any command ending with &, it runs in background, that
shell does not wait till it gets over and shell prompt is displayed
immediately. have a look at following output

1. dawood-ss-macbook:junk dawood$ nohup ./try.sh &
2. [1] 438
3. dawood-ss-macbook:junk dawood$ appending output to nohup.out

on line 2, it displays job id([1]) and pid (438). you get prompt
immediately as on line 3. now shell is waiting to get next input. if you
want prompt again hit return. :)

Andre Majorel

unread,
Jan 21, 2009, 4:44:57 AM1/21/09
to
On 2009-01-20, Mark Wooding <m...@distorted.org.uk> wrote:

> Sumir <sumir...@gmail.com> writes:
>
>> cat myfile | while read line
>> do
>> exit 2
>> done
>> echo "out of loop"
>>
>> as it is evident, the exit should cause the script to terminate ,hence
>> producing no output for the script. when i run it with nohup

>> as "nohup ./test.sh &", the "out of loop" statement appears
>> in the output, indicating that the exit just gets it outside
>> the loop but does not exit the program.
>
> What's happening is that the `while' loop is being run in a subprocess;
> so `exit' is exiting that subshell.

The OP could rewrite the loop as :

while read line
do
exit 2

done <myfile

That would not only get rid of the pipe, but also avoid creating
an unnecessary cat process.

--
André Majorel <URL:http://www.teaser.fr/~amajorel/>
"No meetings were held, nor secret handshakes created, to allow a Seria-
list Elite to disenfranchise the (tonal) non conformist. None had to be."
-- Wendy Carlos

Sumir

unread,
Jan 21, 2009, 10:39:55 AM1/21/09
to

Thanks all for your comments.
Well the concept behind nohup is pretty much clear, but i am still not
able to get through with the problem

I check that i have the same shell (type) in/out of the while loop.
tried removing cat myfile | with the > operator, but that didn't help
either
works the same way as cat when using nohup
Is there a more elegant/different way of reading a file line by line
which would problably do it in this case ?

Andre Majorel

unread,
Jan 22, 2009, 4:18:42 AM1/22/09
to
On 2009-01-21, Sumir <sumir...@gmail.com> wrote:
> On Jan 21, 4:44 am, Andre Majorel <che...@halliburton.com> wrote:
>> On 2009-01-20, Mark Wooding <m...@distorted.org.uk> wrote:
>> > Sumir <sumirme...@gmail.com> writes:
>>
>> >> cat myfile | while read line
>> >> do
>> >> exit 2
>> >> done
>> >> echo "out of loop"
>>
>> >> as it is evident, the exit should cause the script to
>> >> terminate ,hence producing no output for the script. when
>> >> i run it with nohup as "nohup ./test.sh &", the "out of
>> >> loop" statement appears in the output, indicating that the
>> >> exit just gets it outside the loop but does not exit the
>> >> program.
>>
>> > What's happening is that the `while' loop is being run in a
>> > subprocess;
>>
>>   while read line
>>   do
>>     exit 2
>>   done <myfile
>
> I check that i have the same shell (type) in/out of the while loop.
> tried removing cat myfile | with the > operator, but that didn't help
> either
> works the same way as cat when using nohup
> Is there a more elegant/different way of reading a file line by line
> which would problably do it in this case ?

You could always switch to awk. But exit inside a while loop is
supposed to work. What's the output of this ?

cat <<\EOF >/tmp/truc
#!/bin/sh
echo "before (\$0 = '$0', \$\$ = '$$')"
ps | grep "$$"
while read line
do
echo "during (\$0 = '$0', \$\$ = '$$')"
ps | grep "$$"
exit 2
done </etc/resolv.conf
echo "after (\$0 = '$0', \$\$ = '$$')"
ps | grep "$$"
EOF
chmod +x /tmp/truc
nohup /tmp/truc &
echo '$!' = $!

Sven Mascheck

unread,
Jan 22, 2009, 8:37:27 AM1/22/09
to
Andre Majorel wrote:
> On 2009-01-21, Sumir <sumir...@gmail.com> wrote:

>>>   while read line
>>>   do
>>>     exit 2
>>>   done <myfile
>>

>> tried removing cat myfile | with the > operator, but that didn't help

> But exit inside a while loop is supposed to work. [...]

There is an exception for traditional Bourne shells: redirecting
loops and other control structures also results in a subshell.
You can work around this by putting the structure into a function,
or with a global redirection,

exec 5<&0 <myfile
while read line [...]
done
exec <&5 5<&\-
--
http://code.dogmap.org./lintsh/
http://www.in-ulm.de/~mascheck/bourne/common.html

Ed Morton

unread,
Jan 22, 2009, 9:13:46 AM1/22/09
to

What do you want to do with each line? If the answer is to modify it
and write it to a file or aggregate it or otherwise to manipulation on
the text, then use sed/awk/perl/... but if the answer is to find a
file named based on that line, or do something else related to file or
process manipulation then stick to a shell script.

Ed.

0 new messages