Loading...
Searching...
No Matches
Janus as a daemon/service

By default, Janus starts in foreground, and as such works as a server application that you start normally and displays output on the console. That said, there are several reasons why you may not want to keep Janus in the foreground, while still being interested in checking the console to see what's happening.

There are different ways to "daemonize" it and have it run as a service, though. This page tries to summarize a few ways to do so, starting from "dumb" approaches like sending to background and/or using screen/tmux, to more sophisticated approaches involving `systemd`, `upstart` and others.

# Running Janus as a daemon

Since version `0.1.0`, you can run Janus as a daemon application. To do so, just pass either `-b` or `--daemon` as a command line argument, and Janus will be daemonized. Just beware, though, that since this results in stdout/stdin/stderr being closed, you MUST specify a log file for Janus to use, either via command line (`-L` or `--log-file` ) or in `janus.jcfg`.

# Running in background

Another simple way to run Janus in the background is to just append the `&` character to the command line. Anyway, this will still "flood" the console with output from Janus. While there are ways to handle it (e.g., as explained here), a nice and easy way to handle this is redirecting the output to a separate file, e.g., a dedicated log:

    /opt/janus/bin/janus -d 5 -6 >/path/to/mylogfile 2>&1 &

This is especially useful in case you want to keep a log of what happened when Janus was running, and can also be used as a simple and effective way to watch the console "live" using `tail:`

    tail -f /path/to/mylogfile

# Terminal multiplexers

Another easy way to run Janus in the background is using terminal multiplexers like `screen` or `tmux`. If you're not familiar with such applications, you can find a quick overview here.

The following is a simple example with `screen:`

    screen -S janus -d -m
    screen -r janus -X stuff $'/opt/janus/bin/janus -d 5 -6\n'

This will create a session called "janus" and launch Janus in it with a few command line options (in this case, just the option to enable IPv6 support and set the debug to verbose). Janus will then be running in the background: accessing the console is just a matter of attaching to the "janus" screen:

    screen -r janus
    [CTRL+A+D to detach again]

Terminal multiplexers usually allow for logging the output to file as well, if you want to keep an history of what happened during the Janus lifetime.

# systemd

This section shows how you can add Janus as a service to systemd.

    [Unit]
    Description=Janus WebRTC Server
    After=network-online.target
    Wants=network-online.target

    [Service]
    Type=simple
    ExecStart=/opt/janus/bin/janus -o
    Restart=on-abnormal
    LimitNOFILE=65536

    [Install]
    WantedBy=multi-user.target

Note
  Remember to adjust the path in ExecStart to match the Janus binary path.

Warning
  Please beware that, per the default `RateLimitInterval` and and `RateLimitBurst` values in the default sytemd configuration, logger messages are dropped if they arrive faster than ~33 per second. You may want to configure them accordingly, or otherwise Janus log messages may be missing. To fix this, setting `RateLimitInterval=1s` and `RateLimitBurst=2000` in `/etc/systemd/journald.conf` is usually enough.

Note
  systemd example provided by @saghul

# upstart

This section shows how you can add Janus as a daemon to upstart, which is typically available on Ubuntu systems.

    description "janus"

    start on filesystem or runlevel [2345]
    stop on runlevel [!2345]
    limit nofile 50000 50000
    limit core unlimited unlimited

    respawn
    respawn limit 10 5

    exec /opt/janus/bin/janus

Note
  upstart example provided by @ploxiln

Warning
  In case starting Janus depends on some external conditions, you may need to modify the `start` and `stop` lines accordingly. Here you can find an example, provided by @stormbkk87, showing how you can wait, for instance, for RabbitMQ to start before starting Janus too.

# sysvinit

This section shows how you can add Janus as a daemon to SysVinit based systems.

    #!/bin/sh

    ### BEGIN INIT INFO
    # Provides:          Janus
    # Required-Start:    $remote_fs $syslog
    # Required-Stop:     $remote_fs $syslog
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: Janus WebRTC Server
    # Description:       Janus WebRTC Server
    ### END INIT INFO

    DAEMON=/usr/bin/janus
    DAEMON_NAME=janus

    # Add any command line options for your daemon here
    DAEMON_OPTS="-D -o"

    # This next line determines what user the script runs as.
    # Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.
    DAEMON_USER=root

    # The process ID of the script when it runs is stored here:
    PIDFILE=/var/run/$DAEMON_NAME.pid

    . /lib/lsb/init-functions

    do_start () {
        log_daemon_msg "Starting system $DAEMON_NAME daemon"
        start-stop-daemon --start --background --no-close --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON -- $DAEMON_OPTS >> /var/log/$DAEMON_NAME.log 2>&1
        log_end_msg $?
    }
    do_stop () {
        log_daemon_msg "Stopping system $DAEMON_NAME daemon"
        start-stop-daemon --stop --pidfile $PIDFILE --retry 10
        log_end_msg $?
    }

    case "$1" in

        start|stop)
            do_${1}
            ;;

        restart|reload|force-reload)
            do_stop
            do_start
            ;;

        status)
            status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
            ;;

        *)
            echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"
            exit 1
            ;;

    esac
    exit 0

Note
  sysvinit example provided by @saghul

# supervisor

This section shows how you can add Janus to supervisor, which is typically available on Ubuntu systems.

    [program:janus]
    command=/opt/janus/bin/janus
    user=root
    autostart=true
    autorestart=true
    stderr_logfile=/var/log/janus.err.log
    stdout_logfile=/var/log/janus.out.log

Note
  The above configuration file should be added to `/etc/supervisor/conf.d/janus.conf`. Then the following commands should be run:

    sudo supervisorctl reread
    sudo supervisorctl update

# Others

TODO.