Martin Majer
Martin Majer

Reputation: 3352

Linux daemon waiting for a socket connection

I would like to create a simple Linux daemon which will...

Is it possible to write this in bash? (And how?)

Also, I would like to be able to start and stop the script with service my-foobar-daemon start / stop (on Ubuntu), how can I do that?

Thanks :)

Upvotes: 1

Views: 2517

Answers (2)

nezabudka
nezabudka

Reputation: 1

#! /bin/bash
coproc nc -l -p 8080
while true; do
    if read -u "${COPROC[0]}" line; then
        case "$line" in
            foo)
                bar
                break
                ;;
            *)
                echo "$line: unknown command" >&2
                ;;
        esac
    fi
done
kill "$COPROC_PID"

Upvotes: 0

nosid
nosid

Reputation: 50074

The following snippet uses bash and nc to implement the requirements:

#! /bin/bash
while true; do
    coproc nc -l -p 8080
    if read -u "${COPROC[0]}" line; then
        case "$line" in
            foo)
                bar
                ;;
            *)
                echo "$line: unknown command" >&2
                ;;
        esac
    fi
    kill "$COPROC_PID"
    wait "$COPROC_PID"
done

Upvotes: 1

Related Questions