[newbie] how to convert program to true service (in Debian Squeeze)

July 11th, 2012 - 05:25 am ET by Jean Dubois | Report spam
I'm trying to make a program run as a service under Debian Squeeze
(just to learn how to do that, I know crontab would be a better choice
for what I'm doing here, but I think it's ok as an example for
learning how to set up a service)
The service 'takepicture' just takes a picture from a camera every 15
minutes


This is how far I got:

### BEGIN INIT INFO
# Provides: takepicture
# Required-Start: $syslog
# Required-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
#!/bin/bash

PATH=/bin:/usr/bin

case "$1" in
start)
echo $$ > /tmp/takepicture.pid
while true
do
wget --http-user=root --http-passwd=bigsecret \
http://192.168.0.70/axis-cgi/jpg/image.cgi?resolutiond0x480
\
-O /home/jean/pictures/picture$(date +%F:%H:%M:%S).jpg
sleep 1500
done
;;
stop)
kill $(cat /tmp/takepicture.pid)
exit 0
;;
esac

I added it to the services running the command "insserv takepicture"

So far so good, but when I start it manually with the command "service
takepicture start"
I don't get my command prompt back as is usual when running other
services. How should the script be
modified to accomplish this.

thanks
jean

p.s.1 please don't answer "service take picture start &", that's not
what I want as other services in /etc/init.d don't have to be started
like that neither
p.s.2 I'm running Debian Squeeze
email Follow the discussionReplies 10 repliesReplies Make a reply

Similar topics

Replies

#1 Nigel Wade
July 11th, 2012 - 05:46 am ET | Report spam
On 11/07/12 10:25, Jean Dubois wrote:
I'm trying to make a program run as a service under Debian Squeeze
(just to learn how to do that, I know crontab would be a better choice
for what I'm doing here, but I think it's ok as an example for
learning how to set up a service)
The service 'takepicture' just takes a picture from a camera every 15
minutes


This is how far I got:

### BEGIN INIT INFO
# Provides: takepicture
# Required-Start: $syslog
# Required-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
#!/bin/bash

PATH=/bin:/usr/bin

case "$1" in
start)
echo $$> /tmp/takepicture.pid
while true
do
wget --http-user=root --http-passwd=bigsecret \
http://192.168.0.70/axis-cgi/jpg/image.cgi?resolutiond0x480
\
-O /home/jean/pictures/picture$(date +%F:%H:%M:%S).jpg
sleep 1500
done
;;
stop)
kill $(cat /tmp/takepicture.pid)
exit 0
;;
esac

I added it to the services running the command "insserv takepicture"

So far so good, but when I start it manually with the command "service
takepicture start"
I don't get my command prompt back as is usual when running other
services. How should the script be
modified to accomplish this.

thanks
jean

p.s.1 please don't answer "service take picture start&", that's not
what I want as other services in /etc/init.d don't have to be started
like that neither
p.s.2 I'm running Debian Squeeze




A service is a daemon which is started, and then runs in the background.
It is accompanied by a script which controls the service, start/stop
at the very least, and probably restart/reload as appropriate. This
control script should exit as soon as the operation on the service has
been completed.

Don't confuse the script which starts/stops/restarts the service with
the actual service itself. Your service startup is an infinite loop. The
script never completes, hence your prompt is never returned.

If you want your "service" to be this infinite loop then it needs to be
in a separate script. The startup script should then start that script,
run it in the background with an appended "&", and exit.


Nigel Wade
Replies Reply to this message
#2 J G Miller
July 11th, 2012 - 07:04 am ET | Report spam
On Wednesday, July 11th, 2012, at 10:46:23h +0100, Nigel Wade advised:

The startup script should then start that script,
run it in the background with an appended "&", and exit.



Or to keep in style with the "Debian way", use start-stop-daemon
to invoke the script that actually takes the pictures.

As an example, look at one of the scripts which come up from

grep -l 'start-stop-daemon' /etc/init.d/*
Replies Reply to this message
#3 Nigel Wade
July 11th, 2012 - 08:29 am ET | Report spam
On 11/07/12 12:04, J G Miller wrote:
On Wednesday, July 11th, 2012, at 10:46:23h +0100, Nigel Wade advised:

The startup script should then start that script,
run it in the background with an appended "&", and exit.



Or to keep in style with the "Debian way", use start-stop-daemon
to invoke the script that actually takes the pictures.

As an example, look at one of the scripts which come up from

grep -l 'start-stop-daemon' /etc/init.d/*





I don't use Debian, so am unfamiliar with the "Debian way".

Nigel Wade
Replies Reply to this message
#4 Jean Dubois
July 11th, 2012 - 09:14 am ET | Report spam
On 11 jul, 13:04, J G Miller wrote:
On Wednesday, July 11th, 2012, at 10:46:23h +0100, Nigel Wade advised:

> The startup script should then start that script,
> run it in the background with an appended "&", and exit.

Or to keep in style with the "Debian way", use start-stop-daemon
to invoke the script that actually takes the pictures.

As an example, look at one of the scripts which come up from

   grep -l 'start-stop-daemon' /etc/init.d/*


I took the lpd service as an example, I moved the original script
"takepicture" to /usr/sbin and put a new script called "takepictured"
in /etc/init.d with the following contents:
#!/bin/sh
#
### BEGIN INIT INFO
# Provides: takepicture
# Required-Start: $syslog
# Required-Stop: $syslog
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description:
# Description: take picture every 15 minutes
#
#
### END INIT INFO

PATH=/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=/usr/sbin/takepicture

OPTIONS=""


test -x $DAEMON -a -f /usr/sbin/pac || exit 0

case "$1" in
start)
echo -n "Starting takepicture"
if start-stop-daemon --quiet --stop --signal 0 --exec $DAEMON
then
echo " [already running]"
else
/sbin/start-stop-daemon --start --quiet --exec $DAEMON
echo "."
fi
;;
stop)
echo -n "Stopping takepicture"
if start-stop-daemon --quiet --stop --signal 0 --exec $DAEMON
then
start-stop-daemon --quiet --stop --exec $DAEMON
echo "."
else
echo " [not running]";
fi
;;
force-reload|restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "Usage: /etc/init.d/betterpicture {start|stop|restart|
force-reload}"
exit 1
esac

exit 0

I then gave the command "insserv takepictured" but when starting it as
"service takepictured start" nothing seems to happen...
Can anyone here help me further?

regards,
jean
Replies Reply to this message
#5 Jean Dubois
July 11th, 2012 - 09:36 am ET | Report spam
On 11 jul, 13:04, J G Miller wrote:
On Wednesday, July 11th, 2012, at 10:46:23h +0100, Nigel Wade advised:

> The startup script should then start that script,
> run it in the background with an appended "&", and exit.

Or to keep in style with the "Debian way", use start-stop-daemon
to invoke the script that actually takes the pictures.

As an example, look at one of the scripts which come up from

   grep -l 'start-stop-daemon' /etc/init.d/*



On 11 jul, 13:04, J G Miller wrote:
On Wednesday, July 11th, 2012, at 10:46:23h +0100, Nigel Wade advised:

> The startup script should then start that script,
> run it in the background with an appended "&", and exit.

Or to keep in style with the "Debian way", use start-stop-daemon
to invoke the script that actually takes the pictures.

As an example, look at one of the scripts which come up from

grep -l 'start-stop-daemon' /etc/init.d/*


Thanks for the reply.
I took the lpd service as an example.
I edited the original script called 'takepicture' as follows:
#!/bin/bash
#takepicture
PATH=/bin:/usr/bin
while true
do
wget --quiet --http-user=root --http-passwd=bigsecret \
http://192.162.0.60/axis-cgi/jpg/image.cgi?resolutiond0x480
\
-O /home/jean/pictures/picture$(date +%F:%H:%M:%S).jpg
sleep 1500
done

I put "takepicture" in /usr/sbin and made a new script called "/etc/
init.d/takepictured"
with the following contents:
#!/bin/sh
#
### BEGIN INIT INFO
# Provides: takepicture
# Required-Start: $syslog
# Required-Stop: $syslog
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description:
# Description: take picture every 15 minutes
#
#
### END INIT INFO

PATH=/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=/usr/sbin/takepicture

OPTIONS=""

test -x $DAEMON -a -f /usr/sbin/pac || exit 0

case "$1" in
start)
echo -n "Starting takepicture"
if start-stop-daemon --quiet --stop --signal 0 --exec $DAEMON
then
echo " [already running]"
else
/sbin/start-stop-daemon --start --quiet --exec $DAEMON
echo "."
fi
;;
stop)
echo -n "Stopping takepicture"
if start-stop-daemon --quiet --stop --signal 0 --exec $DAEMON
then
start-stop-daemon --quiet --stop --exec $DAEMON
echo "."
else
echo " [not running]";
fi
;;
force-reload|restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "Usage: /etc/init.d/takepictured {start|stop|restart|
force-reload}"
exit 1
esac

exit 0

I then gave the command "insserv takepictured" but when starting it as
"service takepictured start" nothing seems to happen...
Can anyone here help me further?

regards,
jean
Replies Reply to this message
Help Create a new topicNext page Replies Make a reply
Search Make your own search