Re: bash script as daemon from startup

From: Reiner Griess (mynewnews_at_gmx.net)
Date: 02/24/05


Date: Thu, 24 Feb 2005 15:48:25 +0100

Alan Connor wrote:
> On Thu, 24 Feb 2005 12:10:32 +0100, Reiner Griess <mynewnews@gmx.net> wrote:
>>Alan Connor wrote:
>>>On Wed, 23 Feb 2005 23:35:58 +0100, Reiner Griess
>>><mynewnews@gmx.net> wrote:
>>>
>>>>I know this question was asked often before. But I was not able
>>>>to find a suitable solution. [...]
>>>>
>>>>Now, as I said, I want to start a bash script as a daemon. [...]
>>>
>>>
>>>Interesting. I run Debian too, and had a look at that skeleton
>>>script in /etc/init.d.
>>
>>the skeleton: /etc/init.d/skeleton
>>
>>
>>>Why wouldn't it work for a bash script? They are programs too...
>>
>>My test-shell-script /root/SCRIPTS/while:
>>#!/bin/bash
>>while true
>>do
>> echo "jop" >> /root/SCRIPTS/output
>> sleep 1
>>done
>>
>>And the initscript, based on the skeleton:
>><--- while.initscript --->
>>#! /bin/sh
>>PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
>>DAEMON=/root/SCRIPTS/while
>>NAME=whiledaemon
>>DESC="while daemon"
>>
>>test -x $DAEMON || exit 0
>>
>>set -e
>>
>>case "$1" in
>> start)
>> echo -n "Starting $DESC: $NAME"
>> start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
>> --exec $DAEMON
>> echo "."
>> ;;
>> stop) # and so on and on
>><--- while.initscript --->
>>
>>
>>Now, when I am executing the initscript:
>>/etc/init.d/while.init start
>>the output of the script is "Starting while daemon: whiledaemon" and
>>nothing else happens. The script runs in the background but will not get
>>detached from the shell!
>>
>>Any hints?
>>reiner
>
>
> Errrr.....RTFM?
>
> :-)

YEAH! This seems to be a good idea ;-)

> From man start-stop-daemon:
>
>
> <quote>
>
> -b|--background
> Typically used with programs that don't detach on
> their own. This option will force start-stop-daemon
> to fork before starting the process, and force it
> into the background. WARNING: start-stop-daemon
> cannot check the exit status if the process fails
> to execute for any reason. This is a last resort,
> and is only meant for programs that either make no
> sense forking on their own, or where it's not fea
> sible to add the code for it to do this itself.
>
>
> </quote>
>
> Another option would be to run the script in screen, and then
> detach the screen session. But it wouldn't be anywhere near
> as efficient.

Uh, no!

But "-b" works! I read the man page. But, I was *blind* maybe :)

> There's probably a utility somewhere that one can use to detach
> processes from the shell, but I don't know what it is.

The most important thing is: it runs!

Nice. Hopefully this works with my original bash script and not only
with this sensless do...while loop.

Thank you, alan!

reiner



Relevant Pages

  • Re: The test command and syntax error detection
    ... > From: Alan Connor ... I use the script ALL the time and it works PERFECTLY ... Here's the script, AGAIN, and the reason it works, AS was pointed out on ... I enter the alias gn and the script brings up my internet connection, ...
    (comp.unix.shell)
  • Re: Bash script to see if PPP link is up...
    ... this script should be *avoided*. ... "Alan Connor", who is a well known net kook that has posted ... The only way I've found to effectively test a link is to ping ... won't be a pleasant experience. ...
    (comp.os.linux.networking)
  • Re: bash script as daemon from startup
    ... > Alan Connor wrote: ... and had a look at that skeleton ... The script runs in the background but will not get ... There's probably a utility somewhere that one can use to detach ...
    (comp.unix.shell)
  • Re: The test command and syntax error detection
    ... Alan Connor wrote: ... I use the script ALL the time and it works PERFECTLY ... But your technique fails miserably on my system, ... I saw no trolls there. ...
    (comp.unix.shell)
  • Re: How to make a script run every 10 minutes?
    ... Alan Connor wrote: ... > of telling me AGAIN that a script I use all the time doesn't work. ... so it's a dismal failure. ... Modify what you *say* you want it to ...
    (comp.os.linux.misc)