Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

recently I encountered a problem in ubuntu upstart and trap in bash shell.

I use ubuntu 12.04

the upstart script(/etc/init/player.conf) is:

start on (local-filesystems and runlevel [2345])
stop on runlevel [06]

script
    /tmp/test.sh
end script

the /tmp/test.sh is:

#!/bin/bash

TOKEN="token"
TOKEN_FILE="/tmp/token"
OUTPUT_FILE="/tmp/test_exit"
func()
{
    grep "$TOKEN" "$TOKEN_FILE"; echo $? >> "$OUTPUT_FILE"; exit 0
}

trap 'trap "" INT TERM QUIT EXIT; func' INT TERM QUIT EXIT

rm -f "$OUTPUT_FILE"
echo "$TOKEN" > "$TOKEN_FILE"

grep "$TOKEN" "$TOKEN_FILE"; echo $? >> "$OUTPUT_FILE";

sleep 1000

exit 0

the test procedure is:

$sudo service player start
$sudo service player stop
$cat /tmp/test_exit
0
2

my question is, why the second result is 2, since the file(/tmp/token) is not changed, the grep should find the string("token") in that file("/tmp/token").

share|improve this question
1  
You can enable debug mode using ` bash -x /tmp/test.sh >/tmp/debug.log 2>&1` so that you can trace that.. –  Rahul Patil Sep 17 '13 at 4:01
    
If I execute /tmp/test.sh and wait until it's finished or terminate it with SIGKILL or SIGINT, the result is "0 0", only when I use upstart service to start/stop it the result will be different –  layka Sep 17 '13 at 4:13
    
what happens if you change the grep to grep -q or redirect the output? –  cas Sep 17 '13 at 5:29
    
both grep -q and redirection give "0 0", but why is that?? –  layka Sep 17 '13 at 5:43
    
my suspicion/theory is that the exit code of 2 (error) is because when the script runs from upstart there's no stdout to write the output of grep to –  cas Sep 17 '13 at 5:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.