Bug in startup scripts, CLI debugging appears to run twice

I am posting this here and on the trixbox foums.

I have found a rather disturbing bug in the startup scripts for asterisk.

If I restart my asterisk (TrixBox 1.2.3) with amportal restart and then run asterisk -r, enable debug with set debug 10 and set verbose 10 everything is displayed twice. It even looks like everything is running twice.

After some debugging I found the reason for this.

After running amportal restart I did a ps aux | grep asterisk to see the arguments for asterisk. Here is a part of the output:

[code:1]
/usr/sbin/asterisk -U asterisk -G asterisk -v -g -p -U asterisk -G asterisk
[/code:1]

Arguments for -U and -G are listed twice.
If I only restart asterisk with /etc/init.d/asterisk restart the output is this:

[code:1]
/usr/sbin/asterisk -v -g -p -U asterisk -G asterisk
[/code:1]

I have found out that the safe_asterisk script is to blame.
On line 12 in the script there is this:

[code:1]
ASTARGS="-v -g -p -U asterisk -G asterisk"
[/code:1]

On line 57 and on line 60 there is this line

[code:1]
${ASTSBINDIR}/asterisk ${CLIARGS} ${ASTARGS}
[/code:1]

$CLIARGS is the passed arguments to safe_asterisk coming from amportal containing -U asterisk -G asterisk, but this in not needed as we are passing the same parameters in ASTARGS.

So to conclude:
If I run amportal restart I will run everything twice when looking at the CLI, if I restart asterisk with /etc/init.d/asterisk it will start asterisk correct.

Quick and Dirty (Q&D) solution:
Remove ${CLIARGS} from row 57 and 60 in safe_asterisk, do an amportal restart and you should be running a more stable TrixBox.

I upgraded to FreePBX 2.2 beta 2 and the result is the same.

Mikael