Export to GitHub

webrtc2sip - issue #80

Feature request: run webrtc2sip as a daemon


Posted on Apr 15, 2013 by Happy Giraffe

I'd like to run webrtc2sip as a real daemon in the background. Unfortunately webrtc2sip does not support this. And you can't run webrtc2sip in the background with start-stop-daemon, &, nohup or disown because then it will start consuming all CPU. So the only way to run webrtc2sip in the background seems to be screen, tmux or byobu. I'm fine with this but it would be nice if I could run webrtc2sip as a daemon directly. Or maybe someone could take a look at why webrtc2sip starts consuming all CPU when put into the background.

Thanks in advance,

Jeremy

Comment #1

Posted on Apr 22, 2013 by Happy Rhino

When I use a system cron scheduled job with @reboot to launch the webrtc2sip, and reboot the server with out logging in, the system seems to work fine.

I attempted the following init script, but it did not work for me.

Attachments

Comment #2

Posted on Apr 24, 2013 by Happy Giraffe

Here's my attempt to create an init script. It assumes a webrtc2sip system user which can be created with: adduser --quiet --system --group --disabled-password --shell /bin/false --gecos "webrtc2sip" --home /var/run/webrtc2sip webrtc2sip

It also assumes that screen is installed. This script works for me but it's not heavily tested and needs some more work.

Jeremy

Attachments

Comment #3

Posted on Apr 24, 2013 by Happy Giraffe

And you need a file /etc/default/webrtc2sip. I've attached an example.

Attachments

Comment #4

Posted on May 15, 2013 by Happy Rhino

Comment deleted

Comment #5

Posted on May 15, 2013 by Happy Rhino

Jeremy I just started to observe the high CPU consumption issue your indicated. Your script seems to work MUCH better :) THANK YOU!

HOWEVER, its still not starting up on server reboot... I ended up putting again into the crontab @reboot service webrtc2sip start and now it works.

I was under the impression it should work automatically if it was in the /etc/init.d/ folder, but it does not seem to start up on its own .

Comment #6

Posted on May 15, 2013 by Happy Giraffe

Hello Steve,

In order for services to start you need to tell them when to start (i.e. in which runlevel). On Ubuntu this is done with the update-rc.d command:

update-rc.d webrtc2sip defaults

This will set up the webrtc2sip init script to run at boot time.

Jeremy

Comment #7

Posted on May 15, 2013 by Happy Rhino

Thanks Jeremy! I will try it now. Also, I wonder if there is some way to enhance the startup script to make sure that there are no left over TCP connections.... I find that between restarting the webrtc2sip software, I need to wait a few mins for the sessions to close.... webrtc2sip throws many errors when you try to start it if there are left over connections.

Perhaps adding a loop and wait for the results of netstat -t | grep 10060

to be non could help? thanks again

Comment #8

Posted on May 16, 2013 by Happy Giraffe

Isn't this solved with the 2.5.0 release of webrtc2ip (r85)? That release fixes issue 13: http://code.google.com/p/webrtc2sip/issues/detail?id=13 Or is that a different issue?

Jeremy

Comment #9

Posted on May 17, 2013 by Happy Rhino

I am currently running with 2.5.0. How rapidly should these connections be brought down after termination of webrtc2sip? I still receive these messages and observe connections still waiting to time out.... If I check netstat -t | grep 10060, I can see several connections still active after quitting the application... if I wait long enough, the connections time out, and I can restart without issue. I usually need to wait less than a minute.... but when you are testing different configurations ...typically starting and stopping frequently, its annoying :) ... in a real usage scenario, I assume you would not be starting and stopping and starting again in such rapid succession however. Still this small check could be helpful on startup. thanks!

***ERROR: function: "tnet_socket_create_2()" file: "src/tnet_socket.c" line: "157" MSG: bind to [173.254.255.48:10060]have failed ***ERROR: function: "tnet_socket_create_2()" file: "src/tnet_socket.c" line: "157" MSG: (SYSTEM)NETWORK ERROR ==>Address already in use ***ERROR: function: "tnet_socket_create_2()" file: "src/tnet_socket.c" line: "184" MSG: Invalid socket. ***ERROR: function: "tnet_socket_create_2()" file: "src/tnet_socket.c" line: "184" MSG: (SYSTEM)NETWORK ERROR ==>Address already in use ***ERROR: function: "tnet_transport_create()" file: "src/tnet_transport.c" line: "155" MSG: Failed to create master socket ***ERROR: function: "_tnet_transport_ssl_init()" file: "src/tnet_transport.c" line: "61" MSG: Invalid parameter ***ERROR: function: "tnet_transport_create()" file: "src/tnet_transport.c" line: "160" MSG: Failed to initialize TLS and/or DTLS caps ***ERROR: function: "tsk_runnable_set_priority()" file: "src/tsk_runnable.c" line: "180" MSG: Invalid parameter ***ERROR: function: "tsip_stack_start()" file: "src/tsip.c" line: "728" MSG: Failed to add new transport *INFO: SIP STACK::run -- STOP ***ERROR: function: "start()" file: "mp_engine.cc" line: "537" MSG: Failed to start SIP stack

Comment #10

Posted on May 17, 2013 by Happy Rhino

Jeremy

"update-rc.d webrtc2sip defaults"

Yes that resolved my statup issue with the init script

thanks!

Comment #11

Posted on May 17, 2013 by Happy Giraffe

Hello Steve,

If there are no active TCP connections on 10060, immediately. If there are active connections it could take up to minute.

I always make sure I kill all active connections (so any browsers connected via WebSocket to webrtc2sip) before I restart webrtc2sip. But I'll see if I can add an extra check to the init script.

Jeremy

Comment #12

Posted on May 17, 2013 by Happy Giraffe

You can actually adjust the length of the TIME_WAIT state. By default it's 60 seconds: cat /proc/sys/net/ipv4/tcp_fin_timeout 60

You could try setting tcp_fin_timeout to a lower setting to shorten the length of the TIME_WAIT state: echo -n 20 > /proc/sys/net/ipv4/tcp_fin_timeout

There are also tools like tcpkill and cutter that allow you to terminate active connections but afaik then you'll still have to deal with the TIME_WAIT state in the case of webrtc2sip.

Jeremy

Comment #13

Posted on May 17, 2013 by Happy Giraffe

An option would be to issue a tcpkill or cutter command before restarting webrtc2sip. I'm going to test this. So basically you terminate the WebSocket connections on the server side and then you restart webrtc2sip with no active connections so webrtc2sip should start immediately again.

Jeremy

Comment #14

Posted on May 17, 2013 by Happy Rhino

Thanks Jeremy! Your init.d script (the one attached to this incident) is working very well for me. I made a small enhancement by adding a function to wait for the ports to be available on restart. See the function below and the fully modified script attached. Perhaps you may consider to add this to your package?

Also, I attempted to use your latest PPA update with the init script using SCREEN... however, I was not successful ... perhaps I do not really understand how the SCREEN function works.

thanks again!! Steve

----------------------------------------------------------------------------------------------------------------------------

SLEEP_TIME=15 DEFAULT_FILE=/etc/default/webrtc2sip DEFAULT_CONFIG=/etc/webrtc2sip/config.xml CMD_OUTPUT="/usr/bin/logger -s" CMD_GREP=/bin/grep CMD_SLEEP=/bin/sleep CMD_CUT=/usr/bin/cut

----------------------------------------------------------------------------------------------------------------------------

WaitForPorts () { strConfigFile=${CMD_GREP} -v "#" ${DEFAULT_FILE} | ${CMD_GREP} "OPTIONS=--config=" | ${CMD_CUT} -f3 -d = if [ -z "${strConfigFile}" ]; then
strConfigFile=${DEFAULT_CONFIG} fi ${CMD_OUTPUT} "$0>...Default=[${DEFAULT_FILE}] Config=[${strConfigFile}]" strTransports=${CMD_GREP} "<transport>" ${strConfigFile} if [ -n "${strTransports}" ]; then for Transport in ${strTransports} do Port=echo ${Transport} | ${CMD_CUT} -f2 -d '>' | ${CMD_CUT} -f1 -d '<' | ${CMD_CUT} -f3 -d ';' # ${CMD_OUTPUT} "$0>...Testing port=${Port}" while [ -n "netstat -t | ${CMD_GREP} ":${Port}"" ] do ${CMD_OUTPUT} "$0>...Starting webRTC2SIP... Waiting another ${SLEEP_TIME} seconds for port: ${Port} to be available" ${CMD_SLEEP} ${SLEEP_TIME} done done fi ${CMD_OUTPUT} "$0>...STARTING...." }

----------------------------------------------------------------------------------------------------------------------------

Attachments

Comment #15

Posted on May 21, 2013 by Quick Wombat

Thanks, this looks great. I've been trying to work on an upstart version but the results were mixed.

Quick question: does this init config also handle respawning? I've seen webrtc2sip crashing on some random occasions (albeit rarely).

Thanks!

Comment #16

Posted on May 22, 2013 by Happy Giraffe

It doesn't handle respawning. You could use a tool like monit for this.

Comment #17

Posted on May 24, 2013 by Swift Cat

(No comment was entered for this change.)

Comment #18

Posted on May 25, 2013 by Happy Rhino

When checking the process status with service webrtc2sip status I was having an issue with sometimes running into the condition of Status of webrtc2sip: webrtc2sip: is not running but /var/run/webrtc2sip/webrtc2sip.pid exists.

To improve this condition, I updated the case section to remove the PID file and update it if it is actually running. See the update below and the full file attached. Thanks!

status)
    ${CMD_OUTPUT} "$0>...Status of ${DESC}: ${NAME} "
    if [ ! -r "${PIDFILE}" ]; then
        ${CMD_OUTPUT} "$0>...is not running."
        exit 3
    fi

    if read pid < "$PIDFILE" ; ps -p "$pid" > /dev/null 2>&1; then
        ${CMD_OUTPUT} "$0>...is running."
        exit 0
    else
        ${CMD_OUTPUT} "$0>...${PIDFILE} exists...But does not contain valid PID...REMOVING ${PIDFILE}"
        rm ${PIDFILE}
        strActualPID=`${CMD_PIDOF} ${NAME}`
        if [ -n "${strActualPID}" ]; then      
            ${CMD_OUTPUT} "$0>...${NAME} is running with PID = ${strActualPID}... so updating PID file ${PIDFILE}"
            echo  ${strActualPID} > ${PIDFILE}
            exit 0
        fi
        exit 1
    fi  
;;
Attachments

Comment #19

Posted on May 28, 2013 by Happy Rhino

Small updates to the init.d script attached

Attachments

Comment #20

Posted on Oct 22, 2013 by Helpful Giraffe

Hi Jeremy and Steve,

Follownig this thread, I have created the system user by: sudo adduser --system --group --disabled-password --shell /bin/false --gecos "webrtc2sip" --home /var/run/webrtc2sip webrtc2sip

and deployed your corresponding files as /etc/init.d/webrtc2sip and /etc/default/webrtc2sip

Unfortunately, I did not manage to have the script /etc/init.d/webrtc2sip running successfully. At the command line, webrtc2sip can work via:

$ sudo start-stop-daemon --start --verbose --chuid root:root --exec /usr/bin/screen -- -dmS webrtc2sip /usr/local/sbin/webrtc2sip --config=/usr/local/etc/webrtc2sip/config.xml Starting /usr/bin/screen... $ ps aux | grep webrtc2sip root 28711 0.0 0.0 31268 1360 ? Ss 15:19 0:00 /usr/bin/SCREEN -dmS webrtc2sip /usr/local/sbin/webrtc2sip --config=/usr/local/etc/webrtc2sip/config.xml root 28712 0.1 0.2 669668 5676 pts/2 Ssl+ 15:19 0:00 /usr/local/sbin/webrtc2sip --config=/usr/local/etc/webrtc2sip/config.xml jchan 28727 0.0 0.0 9392 916 pts/1 S+ 15:19 0:00 grep --color=auto webrtc2sip

However, if I replace root with webrtc2sip, the process is not running via the command line interface:

$ sudo start-stop-daemon --start --verbose --chuid webrtc2sip:webrtc2sip --exec /usr/bin/screen -- -dmS webrtc2sip /usr/local/sbin/webrtc2sip --config=/usr/local/etc/webrtc2sip/config.xml Starting /usr/bin/screen... $ ps aux | grep webrtc2sip jchan 28708 0.0 0.0 9388 920 pts/1 S+ 15:18 0:00 grep --color=auto webrtc2sip

Wondering if my webrtc2sip user and group properties are set correctly, or I should not debug this issue via the command line at all?

Thanks for your help.

Comment #21

Posted on Jan 27, 2014 by Happy Elephant

Hi,

has anybody managed to have a working init.d script on Centos6? I compiled start-stop-daemon on Centos, after starting the init.d script it shows:

"webrtc2sip is running with PID = 27972... so updating PID file /var/run/webrtc2sip/webrtc2sip.pid"

but no screen nor webrtc are running. May it be related to the webrtc2sip user?

Thank you

Comment #22

Posted on May 17, 2015 by Happy Giraffe

Hi,

I am running on Ubuntu 12.04 and this init.d script is not working for me. It tries to start, then either says it failed to start or that the pid file already exists. The process never actually get's a PID or starts. Not sure what is wrong. The server runs fine when I manually execute it. How can this script be debugged?

thank you

Status: Accepted

Labels:
Type-Defect Priority-Medium