[Asterisk-Users] starting asterisk with nice -5

Tzafrir Cohen tzafrir at cohens.org.il
Sat Aug 6 07:26:52 MST 2005


On Fri, Aug 05, 2005 at 06:30:30PM -0600, Joseph wrote:
> Is there any script guru on the list that can help me.
> I'm trying to start asterisk with nice -5.
> Normally the command would be:
> nice -5 asterisk  
> 
> but asterisk start from the scrip on Gentoo as -U asterisk -G asterisk
> Here is the script:
> ===============
> depend() {
>         need net
>         use zaptel
> }
> 
> start() {
>         local OPTS USER GROUP

Note: 'local' is bash-specific. Either make your script #!/bin/bash or
make note not to use a non-bash shell as /bin/sh .

There are some other bash-specific constructs in the script.

> 
>         if [[ -n "${ASTERISK_NICE}" ]]; then
>                 if [[ ${ASTERISK_NICE} -ge -20 ]] && \

in plain (non-"buggy") bourne-shell the above is written as:

  if [ "$ASTERISK_NICE" != '' ]
  then
    ...
  fi

If you want to be fully compliant, you'd use:
  
  if [ "x$ASTERISK_NICE" != 'x' ]
  then
    ...
  fi

And anyway, it still throws very ugly syntax error at non-number values
of ASTERISK_NICE .

You could use something like:

  ASTERISK_NICE=`echo $ASTERISK_NICE | cut -d -c 0-9`

Anyway, is a nice level a good-enough boost? It will be good enough if
Asterisk almost does not use the CPU. But if it spends some time at the
CPU, I speculate that the schedualer will soon erode the small
atvantage.

Anyway, I believe 5 is not enough. Something like 10 or 15 is probably
more appropriate. (And -P is even more aggressive :-) )

>                    [[ ${ASTERISK_NICE} -le  19 ]]; then
>                         OPTS="--nicelevel ${ASTERISK_NICE}"
>                 else
>                         eerror "Nice value must be between -20 and 19"
>                 fi
>         fi
> 
>         if [[ -n "${ASTERISK_USER}" ]]; then
>                 USER=${ASTERISK_USER/:*/}
>                 GROUP=$(echo $ASTERISK_USER | awk -F: '/.*:.*/ { print
> $2 }')
>                 if [[ -n "${USER}" ]]; then
>                         ASTERISK_OPTS="${ASTERISK_OPTS} -U ${USER}"
>                 fi
>                 if [[ -n "${GROUP}" ]]; then
>                         ASTERISK_OPTS="${ASTERISK_OPTS} -G ${GROUP}"
>                         GROUP=":${GROUP}"       # make it look nice...
>                 fi
>                 ebegin "Starting asterisk PBX (as ${USER}${GROUP})"
>         else
>                 ebegin "Starting asterisk PBX (as root)"
>         fi
>                 start-stop-daemon --start --exec /usr/sbin/asterisk \
>                         ${OPTS} -- ${ASTERISK_OPTS}
>         eend $?
> }
> 
> stop() {
>         ebegin "Stopping asterisk PBX"
>         start-stop-daemon --stop
> --pidfile /var/run/asterisk/asterisk.pid
>         eend $?
> }
> ======================
> 
> I was under impression that modifying the line:
> start-stop-daemon --start --exec /usr/sbin/asterisk \
> 
> to:
> start-stop-daemon --start --exec /usr/bin/nice -n -5 /usr/sbin/asterisk \
> 
> would to the trick but it doesn't.  If I do that asterisk runs at "nice
> 10" and that wasn't my intension.
> How to start it at nice -5?

The following is according to Debian's start-stop-daemon. I hope
Gentoo's is not too different.

This is a fine point. start-stop-daemon doesn't simply run the script.
It first checks that the program doesn't already run. The argument of
exec is the actual exeutable that will be running. On linux: the target
of /proc/PID/exec . E.g: for a perl script this is actually
/usr/bin/perl . 

So you have a case that is similar to a script: the command used for
running it is not the same as the executable. For that the option
--startas of start-stop-daemon is used.

The following is edited from an init.d  script of a debian package of a 
certain perl script daemon:

  DAEMON=/usr/sbin/op_server
  NAME=op-panel
  USER=$NAME
  EXEC=/usr/bin/perl
  OPTIONS="--daemon --pidfile $PIDFILE"

  case "$1" in
  start)
    start-stop-daemon --chuid $USER --start --pidfile $PIDFILE \
      --exec $EXEC --startas $DAEMON -- $OPTIONS
    ;;
  stop)
    start-stop-daemon --stop --exec $EXEC --pidfile $PIDFILE
    ;;
  reload)
    start-stop-daemon --stop --exec $EXEC --pidfile $PIDFILE --signal HUP
    ;;
  restart|force-reload)
    start-stop-daemon --stop --quiet --oknodo --exec $EXEC --pidfile $PIDFILE
    ;;
  esac

> 
> With default asterisk run at the same nice level "0" as apache and when
> a fax comes in sometimes I get a lot of bad lines.
> So, adjusting nice level to -5 for asterisk might help I would think.
> Asterisk intercept faxes and forwards it to hylafax fax extension.

Ever heard of the magic word "renice"?

Still I'm not sure it will help. But it's the fastest way here. Sneking
a '-P' to the parameters is the second fastest and even more radical.

-- 
Tzafrir Cohen         | tzafrir at jbr.cohens.org.il | VIM is
http://tzafrir.org.il |                           | a Mutt's  
tzafrir at cohens.org.il |                           |  best
ICQ# 16849755         |                           | friend



More information about the asterisk-users mailing list