Reputation: 3352
I would like to create a simple Linux daemon which will...
\n
)foo
, it will run command bar
bar
command is finished (or if the line wasn't foo
), the daemon will wait for another connection and do the same (in infinite loop)...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
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
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