Background job not working as expected


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Background job not working as expected
# 8  
Old 09-09-2014
GoodMorning Everybody,

This is what i did. Right click and "Open in Terminal". I have only opened a single terminal.

Test 1 with infinite.sh
==================

Code:
Shellscript:ls
infinite.sh
Shellscript:cat infinite.sh 
#!/bin/sh

while true
do
    echo "While infinite loop"
done
Shellscript:ps -a
  PID TTY          TIME CMD
 6700 pts/0    00:00:00 ps
Shellscript:ps -ef | grep infinite.sh 
root      6711  6518  0 21:56 pts/0    00:00:00 grep infinite.sh
Shellscript:./infinite.sh > /dev/null &
[1] 6770
Shellscript:ps -a
  PID TTY          TIME CMD
 6770 pts/0    00:00:05 infinite.sh
 6777 pts/0    00:00:0
Shellscript:exit   [Here i pressed enter]

The screen vanished after the exit statement.

Then i opened a new terminal.

Code:
Shellscript:ls
infinite.sh
Shellscript:ps -a
  PID TTY          TIME CMD
 7032 pts/1    00:00:00 ps
Shellscript:ps -ef | grep infinite
root      6770     1 90 21:56 ?        00:03:03 /bin/sh ./infinite.sh
root      7061  6955  0 22:00 pts/1    00:00:00 grep infinite

The background job did not get killed. It is still running with the same PID but now the parent is INIT.

Test 2 with sleep
==============

Code:
Shellscript:sleep 200 &
[1] 7253
Shellscript:exit

Opened a new terminal

Code:
Shellscript:ps -ef | grep sleep
root      7253     1  0 22:02 ?        00:00:00 sleep 200
root      7341  7293  0 22:03 pts/2    00:00:00 grep sleep

Here too the background job did not get killed. The command "jobs" didnot fetch anything. I suppose it is now not a background process any more.

Test 3 with sleep
============
Code:
Shellscript:sleep 800 &
[1] 8007
Shellscript:

Clicked on the cross mark of the window to close the session.

Opened a new terminal once more
Code:
Shellscript:ps -ef | grep sleep
root      8088  8048  0 22:08 pts/1    00:00:00 grep sleep

Now i don't see the sleep job running.

Test 4 with infinite loop
=======================

Code:
Shellscript:ps -ef | grep infinite
root      8398  8305  0 22:12 pts/1    00:00:00 grep infinite
Shellscript:./infinite.sh > /dev/null &
[1] 8425
Shellscript:ps -ef | grep infinite
root      8425  8305 86 22:12 pts/1    00:00:03 /bin/sh ./infinite.sh
root      8433  8305  0 22:12 pts/1    00:00:00 grep infinite

Clicked on the cross mark to close the terminal.

Opened a new terminal once more.

Code:
Shellscript:ps -ef | grep infinite.sh 
root      8497  8446  0 22:13 pts/0    00:00:00 grep infinite.sh

So if we close the session with clicking on the cross mark , then the process gets killed. But if we logout by exit, the process is not getting killed.

All the above tests are done with only one session opened at a particular time.

It seems it goes for a hard kill[like pressing the shutdown button in windows] if we close the session with clicking on the cross mark.
# 9  
Old 09-09-2014
Quote:
The screen vanished after the exit statement
This sounds like the "exit" is being interpreted by the terminal emulator not the Linux O/S. You should be able to issue an "exit" command to Linux and it will log you out and then give you a login prompt.

This should be separate from the commands given to the terminal emulator.

As per my post#4 I reckon failure to logout of Linux is leaving the process running.
This User Gave Thanks to hicksd8 For This Post:
 
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