gvalkov / tailon-legacy

Webapp for looking at and searching through log files
http://tailon-legacy.rtfd.org/
Other
191 stars 45 forks source link

Add a daemon support #19

Closed sijis closed 7 years ago

sijis commented 8 years ago

Is there a daemon mode switch/option available? If not, it would be great if there is a way to run tailon under that mode.

I did try the pip version and I didn't see anything. I apologize if that feature isn't released there yet.

labynocle commented 8 years ago

Hi @sijis

I didn't find a daemon mode too so I write use the following init script (eg: /etc/init.d/tailon) to launch and manage tailon as a daemon:

#!/bin/sh

### BEGIN INIT INFO
# Provides: tailon
# Required-Start:    $network $remote_fs $local_fs
# Required-Stop:     $network $remote_fs $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start and stop tailon
# Description: script to launch the tailon
### END INIT INFO

TAILON_USER='customer'
TAILON_BIN="/usr/local/bin/tailon"
TAILON_CONFIG_FILE='/etc/tailon/tailon.cfg'
TAILON_OPTIONS=''

PID_FILE='/var/run/tailon.pid'
LOG_FILE='/data/logs/tailon/tailon.log'

# #############################################################################
#       Don't touch anything under this line!
#       You shall not pass - Gandalf is watching you
# #############################################################################

case "$1" in
    start)

        [ ! -d "`dirname $LOG_FILE`" ] && mkdir "`dirname $LOG_FILE`" && chown $TAILON_USER "`dirname $LOG_FILE`"

        echo "Starting tailon..."
        /sbin/start-stop-daemon --start --background --pidfile $PID_FILE --make-pidfile \
            --user $TAILON_USER --chuid $TAILON_USER \
            --startas /bin/bash -- \
            -c "$TAILON_BIN $TAILON_OPTIONS -c $TAILON_CONFIG_FILE >> $LOG_FILE 2>&1"

        EXIT_STATUS=$?
        [ $EXIT_STATUS -eq 0 ] && echo "tailon well started"
        [ $EXIT_STATUS -ne 0 ] && echo "tailon not well started"

        exit $EXIT_STATUS
    ;;

    stop)
        echo "Stopping tailon..."
        pkill -P `cat $PID_FILE`
    ;;

    restart)
        $0 stop
        sleep 2
        $0 start
    ;;

    status)
        /sbin/start-stop-daemon --verbose --status --pidfile $PID_FILE --user $TAILON_USER
        EXIT_STATUS=$?
        [ $EXIT_STATUS -eq 0 ] && echo "tailon is running"
        [ $EXIT_STATUS -eq 1 ] && echo "tailon is not running but $PID_FILE exists"
        [ $EXIT_STATUS -eq 3 ] && echo "tailon is not running"
        [ $EXIT_STATUS -eq 4 ] && echo "unable to say something about the tailon status"

        exit $EXIT_STATUS
    ;;

    *)
        echo "Usage: $0 {start|stop|restart|status}"

        exit 1
    ;;
esac

not the best script of the world but it could be a beginning :)

gvalkov commented 8 years ago

Hello @sijis,

It seems that the tendency nowadays is to leave daemonization out of the application logic (here's a blog post on the topic). I'm not very keen on adding this to tailon, even though it's mostly just bringing in one of the many daemonization packages as a dependency.

If plugging it into your init system is overkill, there is also always:

nohup tailon -f /var/log &

Thanks for the LSB compliant, sysv init-script, @labynocle. Between systemd (the init system parts) and freebsd's rc.d, I'm really glad I don't have to write them anymore :)

Kind regards, Georgi

sijis commented 8 years ago

@labynocle Thanks for the example.

I had already had the following for systemd and I was hoping there'd be a better way to do this.

file: start-tailon
#!/bin/bash
. /etc/rc.d/init.d/functions
daemon tailon -c /etc/tailon/tailon.yaml &

and then in tailon.service

[Unit]
Description=Tailon Service

[Service]
Type=forking
ExecStart=/etc/tailon/start-tailon.sh

[Install]
WantedBy=multi-user.target

@gvalkov I completely understand and appreciate your stand on this.

gvalkov commented 8 years ago

There is a simpler way to do it indeed. All that's needed for a tailon systemd service is:

[Unit]
Description=Tailon Service

[Service]
Type=simple  # this is the default - you can drop this line
ExecStart=/path/to/tailon -c /path/to/tailon.yaml

[Install]
WantedBy=multi-user.target
sijis commented 8 years ago

@gvalkov wow! that's awesome. I swore I tried that and it didn't work. I may had had Type=forking, now that I think of it.

Would you be opposed to adding these two examples into a contrib/ directory, in case someone else needs it as a service? I can put in a PR, if you'd prefer.

sijis commented 7 years ago

This is no longer needed, as this commit https://github.com/gvalkov/tailon/commit/6f75aab81477b2f32370df1307fb3e2e610bf8e8 added the example file.