LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   nohup process does not get killed !! (https://www.linuxquestions.org/questions/programming-9/nohup-process-does-not-get-killed-837728/)

smc2 10-13-2010 02:18 AM

nohup process does not get killed !!
 
hi ...
I call nohup in a script like follow but when I quite with ^c the stail program stil is in memory. as u can see I call a perl script within a shell script.

Code:

#!/bin/bash
if [ -z $1 ]; then
echo -e '\33[31m You Must enter a period time \33[0m'
exit 1
fi

nohup  ./stail1.1.pl sa 0 1 >/dev/null 2>&1 &
while [ true ]
do
 ...infinite loop...
 ....this loop just stop if user press ^c and stop the script----
done


Thanks in Advance..

druuna 10-13-2010 02:51 AM

Hi,

When you use nohup the program started (stail1.1.pl in this case) is detached from the terminal/shell and it will not react to any key combo's used in the original shell. Works as designed.

Without knowing what it is you are trying to do and if the stail1.1.pl program actually does need a nohup (and be put to the back ground) I can't give you a solution.

If stail1.1.pl finishes by itself you can probably remove the nohup (you might want to keep the &, depends....).
If stail1.1.pl is an endless loop that must be stopped by the user you need some other solution, which depends on what the main script and the perl script do/must do.

Hope this helps.

smc2 10-13-2010 03:33 AM

Quote:

Originally Posted by druuna (Post 4125773)
Hi,

When you use nohup the program started (stail1.1.pl in this case) is detached from the terminal/shell and it will not react to any key combo's used in the original shell. Works as designed.

Without knowing what it is you are trying to do and if the stail1.1.pl program actually does need a nohup (and be put to the back ground) I can't give you a solution.

If stail1.1.pl finishes by itself you can probably remove the nohup (you might want to keep the &, depends....).
If stail1.1.pl is an endless loop that must be stopped by the user you need some other solution, which depends on what the main script and the perl script do/must do.

Hope this helps.

Dear druuna, thanks. Actualy stail is an script that act like tail. it tail a growing log-file and and withdraw some useful values from pile of data and wite them in a file. stail has lots of output and it distract user ==> I use nohup .
stail should be run while the main script is running. it produeces values that I need in main script (I actually sum these values with some other values and show them to the user).
if stail not closed when the main script is closed, then it remain in memory and
update the log file. so it case miscalculation when the main script run again. becuase now there two process (the old stail and new stail) are updateing a log file.

I hope these details make the problem more clear.
I realty appropriate your help.

druuna 10-13-2010 03:47 AM

Hi,

The script you posted in post #1 only starts stail1.1.pl. Nothing is done with the output (all output is discarded due to the 1>/dev/null and 2>&1). Starting stail1.1.pl with nohup and putting it in the background (&) is probably not needed in your case.

You probably need something like this:
Code:

#!/bin/bash

./stail1.1.pl sa 0 1 | while read LINE
do
  ... do something with or check $LINE ....
done

Depending on what you need to check you might want to include 2>/dev/null (./stail1.1.pl sa 2>/dev/null) to get rid of the errors generated or add 1>/dev/null if you are only interested in the errors.

Hope this helps.

smc2 10-13-2010 04:25 AM

Sorry if I couldn't clarify completely.
stail is like tail and has some built in functions that deal with output theselfs. the importatn thing is that it should be ran as
the main script is running and close when the main is closed.
the main script uses the an output file that stail will create as it is working

druuna 10-13-2010 08:38 AM

Hi,

The example I gave does what you ask for.

When you start the script (the example given in post #4), stail1.1.pl is started and the output is given, a line at the time, to the while read loop. Inside the while read loop you can do what you want/need with the individual lines.

If you stop the example script (ctrl-c) the stail1.1.pl is also killed.

Here's an example where stail1.1.pl is replaced by a tail -f on a log file. It looks for the word completed and prints the date/time and the process:
Code:

#!/bin/bash

tail -f /var/log/allmessages | \
while read LINE
do
#  echo $LINE
  awk '/completed/ { print $1, $2, $3, $5 }'
done

Example run:
Code:

first is example output generated by the tail -f process
$ tail -f /var/log/allmessages
Oct 13 15:20:00 exile fcron[21639]: Job /usr/lib/sa/sa1 & started for user admin (pid 21640)
Oct 13 15:20:02 exile fcron[21639]: Job /usr/lib/sa/sa1 & completed
Oct 13 15:25:00 exile fcron[21706]: Job /usr/lib/sa/sa1 & started for user admin (pid 21707)
Oct 13 15:25:02 exile fcron[21706]: Job /usr/lib/sa/sa1 & completed
Oct 13 15:30:00 exile fcron[21820]: Job /usr/lib/sa/sa1 & started for user admin (pid 21821)
Oct 13 15:30:02 exile fcron[21820]: Job /usr/lib/sa/sa1 & completed
Oct 13 15:35:00 exile fcron[21903]: Job /usr/lib/sa/sa1 & started for user admin (pid 21904)
Oct 13 15:35:02 exile fcron[21903]: Job /usr/lib/sa/sa1 & completed

running the script
$ ./tester.sh
Oct 13 15:20:02 fcron[21639]:
Oct 13 15:25:02 fcron[21706]:
Oct 13 15:30:02 fcron[21818]:
Oct 13 15:30:02 fcron[21820]:
Oct 13 15:35:02 fcron[21903]:

Hope this clears things up a bit.


All times are GMT -5. The time now is 10:56 AM.