0

I got a problem with the upstart files of Ubuntu in /etc/init/. I am trying to run a process if it does not already exist. So I wrote a conf file and start with "start"

The following two files are mostly same except for the process being searched. The init process in the first file is always found and a few repetitions of "1 2 4" are output into $TMPFILE as a result.

start on filesystem and net-device-up IFACE!=lo
stop on runlevel [016]
respawn
script
TMPFILE=/var/log/tmp.log
echo 1 >> $TMPFILE
PID=$(pgrep -f "init" -o) 
echo 2 >> $TMPFILE
if [ -z $PID ] ; then
    echo 3 >> $TMPFILE
fi
echo 4 >> $TMPFILE
end script 

For the second file, doesnotexist is not found by pgrep. However, only repetitions of "1 " is output into $TMPFILE. I also tried running the script part separately, and that outputs "1 2 3 4" just as expected.

start on filesystem and net-device-up IFACE!=lo
stop on runlevel [016]
respawn
script
TMPFILE=/var/log/tmp.log
echo 1 >> $TMPFILE
PID=$(pgrep -f "doesnotexist" -o) 
echo 2 >> $TMPFILE
if [ -z $PID ] ; then
    echo 3 >> $TMPFILE
fi
echo 4 >> $TMPFILE
end script 
  1. How did this happen? Did upstart receive the result of pgrep as an exception?
  2. How can I fix this issue?
  3. Why would there be a few repetitions of the output instead of one? The output is not growing indefinitely and does not seem like re-spawning.

1 Answer 1

0

The problem is that script sections in upstart are run with the set -e flag. This means the shell with exit if any command errors (like pgrep). THe solution is to add a || true after the pgrep command.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .