最近我在ubuntu upstart中遇到了一个问题,并在Bash shell中陷入困境。
我用的是ubuntu 12.04。新贵脚本(/etc/init/player.conf)是:
start on (local-filesystems and runlevel [2345])
stop on runlevel [06]
script
/tmp/test.sh
end script
是/tmp/test.sh
:
#!/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
测试程序:
$sudo service player start
$sudo service player stop
$cat /tmp/test_exit
0
2
我的问题是,为什么第二个结果是2,因为文件(/tmp/token)没有改变,grep应该在该文件(“/tmp/token”)中找到字符串(“token”)。