Adrian
Adrian

Reputation: 10911

Why is nohup still causing a process being run in a shell script to terminate?

I am logging the data coming from top and putting it into a circular set of files. I am not executing top for one set of data and then rerunning for the next set, but instead using a read time out to specify when to go from one log file to the next. This is primarily done this way to remove the startup CPU load cost every time top is executed. The shell script file's name is toplog.sh and looks similar to this:

#!/data/data/com.spartacusrex.spartacuside/files/system/bin/bash

date
echo "  Logging started."

fileCmp()
{
  test `ls -lc "$1" | sed -n 's/\([^ ]* *\)\{4\}\([0-9]*\).*$/\2/;p'` $2 $3
}

oldest()
{
  ls -rc $1 2> /dev/null |head -1
}

file=`oldest /mnt/sdcard/toplog.\*.gz`
echo "  Oldest file is $file"
if [ -z "$file" ]; then
  x=0
else
  file=${file%%.gz}
  file=${file##*.}
  x=$file
fi
echo "  x=$x"

top -d 20 -b  | \
  while true; do
    file=/mnt/sdcard/toplog.$x.gz
    while read -t 5 line; do
      echo "$line"
    done | gzip -c > $file
    if fileCmp "$file" -le 300; then
      date
      echo "        Failure to write to file '$file'."
      exit
    fi
    x=$((($x+1)%10))
    sleep 14
  done

I execute this using nohup so that when the shell dies, this process still runs, like so:

$ nohup ./toplog.sh

But there's a problem. top terminates when I exit the shell session that executed that command, and I'm not exactly sure why. Any ideas?

To clarify, I'm logging on a Android phone. The tools are limited in functionality (i.e. lack some of these switches) and is why I am using top as it contains the output I want.

Version of busybox I'm using is:

BusyBox 1.19.2 (2011-12-12 12:59:36 GMT)

Installed when I installed Terminal IDE.

BTW, this phone is not rooted. I'm trying to track down a failure when my phone responds as if the CPU has spiked and won't go down.

Edit: Well, I found a workaround. But the reason is a bit hazy. I think it has to do with process management and smells of a bug in the busybox ver that I'm using that was missed during regression testing.

The workaround is to wrap top with a useless loop structure like this: while true; do top; done. Through testing, top never gets killed and never gets respawned, but by wrapping it up, it isn't killed.

Any insights on this?

Upvotes: 1

Views: 2959

Answers (2)

markjuggles
markjuggles

Reputation: 466

Running the bash internal command "disown" on your script's process before logging off may prevent it from being signaled.

Upvotes: 0

WhyteWolf
WhyteWolf

Reputation: 456

going to sound stupid, but change your startup command from

nohup ./toplog.sh

to

nohup ./toplog.sh &

the & makes it run as a background process further removing it from the terminal stack.

Upvotes: 1

Related Questions