Tags: bash, daemon, finda, linux, programming, script, solution, startup, suitable, system, unix

bash script as daemon from startup

On Programmer » Unix & Linux

11,757 words with 9 Comments; publish: Wed, 30 Apr 2008 14:42:00 GMT; (20078.13, « »)

Hi there,

I know this question was asked often before. But I was not able to find

a suitable solution.

I want to start a bash script at system startup. Fromthen it should run

in the background forever.

As I found out, the script has to run as a daemon. I am using a debian

system and nicely there is a skeleton script in /etc/init.d/ which

provides the functionality to start a _binary_ as a daemon.

Now, as I said, I want to start a bash script as a daemon.

Any help will be nice. But remember: the "nohup" signal does not solve

the problem. So the "&" or "^Z" commands are not what I am looking for

also, because the the bashscript will die, as soon as the mother process

dies.

Thanks in advance!

reiner

All Comments

Leave a comment...

  • 9 Comments
    • On Wed, 23 Feb 2005 23:35:58 +0100, Reiner Griess

      <mynewnews.unix-linux.todaysummary.com.gmx.net> wrote:

      > Hi there,

      > I know this question was asked often before. But I was not able

      > to find a suitable solution.

      > I want to start a bash script at system startup. Fromthen it

      > should run in the background forever.

      > As I found out, the script has to run as a daemon. I am using

      > a debian system and nicely there is a skeleton script in

      > /etc/init.d/ which provides the functionality to start a

      > _binary_ as a daemon.

      > Now, as I said, I want to start a bash script as a daemon.

      > Any help will be nice. But remember: the "nohup" signal does

      > not solve the problem. So the "&" or "^Z" commands are not what

      > I am looking for also, because the the bashscript will die, as

      > soon as the mother process dies.

      > Thanks in advance! reiner

      Interesting. I run Debian too, and had a look at that skeleton

      script in /etc/init.d.

      Why wouldn't it work for a bash script? They are programs too...

      AC

      #1; Wed, 30 Apr 2008 14:43:00 GMT
    • On Wed, 23 Feb 2005 23:33:51 GMT, Alan Connor <zzzzzz.unix-linux.todaysummary.com.xxx.yyy>

      wrote:

      > On Wed, 23 Feb 2005 23:35:58 +0100, Reiner Griess

      ><mynewnews.unix-linux.todaysummary.com.gmx.net> wrote:

      >

      > Interesting. I run Debian too, and had a look at that skeleton

      > script in /etc/init.d.

      > Why wouldn't it work for a bash script? They are programs

      > too...

      > AC

      >

      Hmmmm... I wonder if you are looking for this:

      while true # while :

      do

      command

      command

      command

      sleep 1

      done

      ?

      AC

      #2; Wed, 30 Apr 2008 14:44:00 GMT
    • You can create two scripts one in /etc/init.d/ directory to start the daemon

      script

      Start up script

      =========

      /etc/init.d/startsheeldaemon

      write something like this

      start)

      nohup /usr/shelldeamon.sh > /var/log/shell.log 2>&1 &

      stop)

      kill ` ps -fed | grep shelldeamon.sh | grep -v grep | awk '{

      print $2 }`

      /user/shelldamon.sh is your processing script

      -SR

      "Alan Connor" <zzzzzz.unix-linux.todaysummary.com.xxx.yyy> wrote in message

      news:zp8Td.3986$MY6.2565.unix-linux.todaysummary.com.newsread1.news.pas.earthlink.net...

      > On Wed, 23 Feb 2005 23:35:58 +0100, Reiner Griess

      > <mynewnews.unix-linux.todaysummary.com.gmx.net> wrote:

      >

      > Interesting. I run Debian too, and had a look at that skeleton

      > script in /etc/init.d.

      > Why wouldn't it work for a bash script? They are programs too...

      > AC

      >

      #3; Wed, 30 Apr 2008 14:45:00 GMT
    • On Wed, 23 Feb 2005 19:59:53 -0500, Software Groups

      <top posting corrected -- please don't do that: makes posts hard

      to read>

      > "Alan Connor" <zzzzzz.unix-linux.todaysummary.com.xxx.yyy> wrote in message

      > news:zp8Td.3986$MY6.2565.unix-linux.todaysummary.com.newsread1.news.pas.earthlink.net...

      >

      >

      > You can create two scripts one in /etc/init.d/ directory to

      > start the daemon script

      > Start up script

      > ========================================

      ========================

      > /etc/init.d/startsheeldaemon write something like this

      > start) nohup /usr/shelldeamon.sh > /var/log/shell.log 2>&1

      > & stop) kill ` ps -fed | grep shelldeamon.sh | grep -v grep |

      > awk '{ print $2 }`

      >

      > /user/shelldamon.sh is your processing script

      >

      > -SR

      >

      Okay. But the OP said that nohup didn't work.

      AC

      #4; Wed, 30 Apr 2008 14:46:00 GMT
    • Hi Alan,

      thanks for your help. But...

      Alan Connor wrote:

      > On Wed, 23 Feb 2005 23:35:58 +0100, Reiner Griess

      > <mynewnews.unix-linux.todaysummary.com.gmx.net> wrote:

      >

      > 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

      #5; Wed, 30 Apr 2008 14:47:00 GMT
    • On Thu, 24 Feb 2005 12:10:32 +0100, Reiner Griess <mynewnews.unix-linux.todaysummary.com.gmx.net> wrote:

      >

      > Hi Alan,

      > thanks for your help. But...

      > Alan Connor wrote:

      > the skeleton: /etc/init.d/skeleton

      >

      > 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?

      :-)

      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.

      There's probably a utility somewhere that one can use to detach

      processes from the shell, but I don't know what it is.

      Perhaps killall or pgrep or pkill?

      $ killall -l

      HUP INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE

      ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ

      VTALRM PROF WINCH IO PWR SYS UNUSED

      Don't know what all those signals do.

      It could be that a simple command sent to /proc/<PID> would do

      the job.

      Hopefully, one of the gurus will drop by this thread.

      AC

      #6; Wed, 30 Apr 2008 14:48:00 GMT
    • Alan Connor wrote:

      > On Thu, 24 Feb 2005 12:10:32 +0100, Reiner Griess <mynewnews.unix-linux.todaysummary.com.gmx.net> wrot

      e:

      >

      > 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

      #7; Wed, 30 Apr 2008 14:49:00 GMT
    • Software Groups wrote:

      > You can create two scripts one in /etc/init.d/ directory to start the daem

      on

      > script

      > Start up script

      > =========

      > /etc/init.d/startsheeldaemon

      > write something like this

      > start)

      > nohup /usr/shelldeamon.sh > /var/log/shell.log 2>&1 &

      > stop)

      > kill ` ps -fed | grep shelldeamon.sh | grep -v grep | awk '{

      > print $2 }`

      >

      > /user/shelldamon.sh is your processing script

      Thank you, this works! I got problems with "nohup" on a BSD system. I

      actually can't remember it in detail. But I should have test this on my

      debian system before posting here. For the sake of completeness

      (pan.2003.02.25.21.01.32.6133.5132.unix-linux.todaysummary.com.gmx.net): for *BSD someone came out

      with this C program:

      #include <stdlib.h>

      #include <unistd.h>

      extern char *optarg;

      extern int optind;

      int main(int ac, char *av[])

      {

      int cflag=1, fflag=1;

      int ch;

      while ((ch = getopt(ac, av, "cf")) != -1) {

      switch (ch) {

      case 'c':

      cflag = 0;

      break;

      case 'f':

      fflag = 0;

      break;

      default:

      return 1;

      }

      }

      ac -= optind;

      av += optind;

      if(daemon(cflag,fflag) == -1)

      return 1;

      return execvp(av[0], av);

      }

      But it does not work under debian.

      Thanks and bye

      reiner

      #8; Wed, 30 Apr 2008 14:50:00 GMT
    • On Thu, 24 Feb 2005 15:48:25 +0100, Reiner Griess <mynewnews.unix-linux.todaysummary.com.gmx.net> wrote:

      >

      > Alan Connor wrote:

      <snip>

      > Thank you, alan!

      > reiner

      Thank _you_, Reiner, for being a gentleman and a very

      interesting thread that I learned a lot from.

      AC

      #9; Wed, 30 Apr 2008 14:51:00 GMT