Background job not working as expected


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Background job not working as expected
# 1  
Old 09-08-2014
Background job not working as expected

Hello Unix Buddies,


Code:
cat infinite.sh

#!/bin/sh
while true
do
      echo "Infinite loop"
done

I am running it in the background
Code:
$ ./infinite.sh > /dev/null &
[1] 31634

Then i do an exit or click on the cross mark to close the session.

Now my expectation is that when i open up a new session, the process should not be running.

But to my surprise if i do a grep , then i see that the process is still running. I did not expect this result for the background job. I thought that when we exit from the shell the background job dies.

Code:
$ ps -ef | grep infinite
root     30496     1 90 11:47 ?        00:00:39 /bin/sh ./infinite.sh
root     30578 30559  0 11:47 pts/0    00:00:00 grep infinite

I am using Linux.

Please through some light here.

Thanks,

Last edited by shellscripting; 09-08-2014 at 01:21 PM..
This User Gave Thanks to shellscripting For This Post:
# 2  
Old 09-08-2014
What is your system? What is your shell? I would have expected it to get killed too, along with everything else attached to that terminal.
# 3  
Old 09-08-2014
Below is the system information that i get
Code:
Shellscript:echo $SHELL
/bin/bash
Shellscript:uname -a
Linux was85host 2.6.27.45-0.1-vmi #1 SMP 2010-02-22 16:49:47 +0100 i686 i686 i386 GNU/Linux

# 4  
Old 09-08-2014
I completely agree with Coron688; everything should be killed.

First question is, How are you logging out? Are you using a terminal emulator? Is the interactive session also still running?
# 5  
Old 09-08-2014
When you put that job in the background, that job received the process id 31634.
In your ps output there is no pid 31634, so the job is gone.

The job with the pid 30496 must come from somewhere else...

Maybe you tested it from several terminals (which may be still active).
If this is the case, run following command in each terminal to detect background jobs: jobs
This User Gave Thanks to junior-helper For This Post:
# 6  
Old 09-08-2014
I did the following test with bash:

Terminal 1:
Code:
$ while :; do sleep 1; done &
[1] 46950
$

Terminal 2:
Code:
$ shopt -s huponexit
$ while :; do sleep 1; done &
[1] 46988

Terminal 3:
Code:
$ ps -ef | grep -e 46950 -e 46988
  501 47102 43149   0  9:20PM ttys000    0:00.00 grep -e 46950 -e 46988
  501 46950 46940   0  9:19PM ttys001    0:00.03 -bash
  501 47100 46950   0  9:20PM ttys001    0:00.00 sleep 1
  501 46988 46944   0  9:19PM ttys002    0:00.02 -bash
  501 47099 46988   0  9:20PM ttys002    0:00.00 sleep 1

Terminal 1 & 2:
Code:
$ logout

[Process completed]

Terminal 3:
Code:
$ ps -ef | grep -e 46950 -e 46988
  501 46950     1   0  9:19PM ??         0:00.03 -bash
  501 47122 46950   0  9:20PM ??         0:00.00 sleep 1
  501 47124 43149   0  9:20PM ttys000    0:00.00 grep -e 46950 -e 46988


Last edited by Scrutinizer; 09-09-2014 at 01:55 PM..
These 3 Users Gave Thanks to Scrutinizer For This Post:
# 7  
Old 09-08-2014
Quote:
Originally Posted by Scrutinizer
I did the following test with bash ...:
Very interesting. I could reproduce this Smilie
I tested with simply closing the window, and it "worked" Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Background job issue

How to bring a backgroud job say sample_script.sh to foreground (4 Replies)
Discussion started by: rafa_fed2
4 Replies

2. Shell Programming and Scripting

Background Job

Hello Everyody, Having a doubt. sort file1 & when we sent a job to the background it returns Job Number PID again if we want to ... (1 Reply)
Discussion started by: knroy10
1 Replies

3. UNIX for Advanced & Expert Users

Background job when completed

Hello - I submitted one background job last night and it completed today morning.I want to know exact time the job completed. I submitted backgroung job like this nohup cp -Rp /opt/apps/prod/proddb/proddata . & I want to know when above job completed on UNIX server.Above command... (9 Replies)
Discussion started by: Mansoor8810
9 Replies

4. Linux

resume the suspended background job

Hi, I was running a job in background and because of some immediate work to be done on my machine, I stopped the background job. The real problem comes when I try to restart the same job in the background. To stop I used stop PID and to resume the same I use bg PID when I do this the... (2 Replies)
Discussion started by: baloo_mia
2 Replies

5. Shell Programming and Scripting

Cannot submit a background job

Hi all, I am currently facing a problem when i am submitting a script to run in the background to collect statistics round the clock on an AIX box. I don't have root authority nor can I set it in cron. So when i submit the job, it runs fine, but won't let me signoff. It prompts me that... (2 Replies)
Discussion started by: tansha
2 Replies

6. Shell Programming and Scripting

how to get background job to foreground

hi, i am just wondering that wen we give the following code we make a process run in background...can the viceversa be performed?i.e can this be made foreground again # sleep 75& 21751 # (4 Replies)
Discussion started by: sandilya
4 Replies

7. UNIX for Dummies Questions & Answers

background job

on gnome i open a terminal and run wget http://soommmething & in the background. because wget shows me downloading progress percentage and download speed continuously, I exit the gnome-terminal after a while i want to see the download percentage but dont know how. my ps -u myname shows that... (3 Replies)
Discussion started by: babayeve
3 Replies

8. UNIX for Dummies Questions & Answers

Background job

Hiya, Recently I've run a few scripts in the foreground, but have realised later they should of been better nohup'd and placed in the background. I understand how to change a foreground job into a background one, but how would put the job into the nohup state? Thanks (1 Reply)
Discussion started by: rdbooth
1 Replies

9. UNIX for Dummies Questions & Answers

background job

I try to run a script as background job. script: #!/usr/bin/csh /usr/bin/date +20%y-%m-%d > ~/datsql.txt If I start it I got this output: tac> ./datermitteln& 293 + Stopped (SIGTTOU) ./datermitteln& I insert the following line inside my script, but without any... (3 Replies)
Discussion started by: joerg
3 Replies
Login or Register to Ask a Question