Find the answer to your Linux question:
Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By atreyu
Hi All, I am new to the Linux service world, and I am attempting to write a sys v init script for a server which responds to tcp/ip requests. In ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2012
    Posts
    3

    daemon /etc/init.d/functions does not return from launching process


    Hi All,

    I am new to the Linux service world, and I am attempting to write a sys v init script for a server which responds to tcp/ip requests. In my start routine, I call daemon to launch the process. However, it just hangs, unless I try to run daemon as a background process then "status" reports the server as "ools dead but subsys locked". However, the internal tool to check it's status, shows that it is running. I am confused by the daemon function, and, therefore, I am probably misusing it.

    The following is the relevant script code:

    Code:
    # Source function library
    . /etc/init.d/functions
    
    # Path to the server binary, etc.
    exec="installdir/bin/ools"
    prog=$(basename $exec)
    OOLS_OPTIONS="ools_args"
    
    lockfile="/var/lock/subsys/$prog"
    pidfile="/var/run/$prog.pid"
    
    export LD_LIBRARY_PATH=installdir/lib
    
    # Start the service ools
    start() {
        [ -x $exec ] || exit 5
        echo -n $"Starting project_fullName project_version Lock Server Version \
        ls_version daemon $prog: "
        # if not running, start it up here
        daemon --pidfile=$pidfile $prog $OOLS_OPTIONS
        retval=$?
        echo
        [ $retval -eq 0 ] && touch $lockfile
        return $retval
    }
    Can anyone give me a clue on how to debug this or what I may be missing from the "daemon" function? Thank you very much in advance.

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    i've fought with the daemon function provided by the /etc/init.d/functions script before...it can help to edit that script to make it echo what it is doing, so that you can troubleshoot it.

    on RHEL 4, the relevant line from the daemon() function in /etc/init.d/functions looks something like:
    Code:
               $nice initlog $INITLOG_ARGS -c "$*"
    on Fedora 17, it looks something like:
    Code:
               $cgroup $nice /bin/bash -c "$corelimit >/dev/null 2>&1 ; $*"
    whatever it is, copy it to a new line and put an echo in front of it, to see what it is doing, exactly.

    i usually don't bother using daemon anymore. i just send it to the background (via &) and keep track of pid,lock,status, etc. myself. for the most part, my script would look just like yours, i'd just have something like this instead:

    Code:
    echo -n $"Starting $prog: "
    $prog $OOLS_OPTIONS &
    kev117 likes this.

  3. #3
    Just Joined!
    Join Date
    Jul 2012
    Posts
    3
    Quote Originally Posted by atreyu View Post
    i've fought with the daemon function provided by the /etc/init.d/functions script before...it can help to edit that script to make it echo what it is doing, so that you can troubleshoot it.

    on RHEL 4, the relevant line from the daemon() function in /etc/init.d/functions looks something like:
    Code:
               $nice initlog $INITLOG_ARGS -c "$*"
    on Fedora 17, it looks something like:
    Code:
               $cgroup $nice /bin/bash -c "$corelimit >/dev/null 2>&1 ; $*"
    whatever it is, copy it to a new line and put an echo in front of it, to see what it is doing, exactly.
    That is a great suggestion. I didn't think of that. Thank you very much.

    i usually don't bother using daemon anymore. i just send it to the background (via &) and keep track of pid,lock,status, etc. myself. for the most part, my script would look just like yours, i'd just have something like this instead:

    Code:
    echo -n $"Starting $prog: "
    $prog $OOLS_OPTIONS &
    Actually, when discussing this with one of my colleagues, a Linux developer, he pointed out that his SUSE distro didn't even have /etc/init.d/functions. Therefore, I am going to have to abandon the use of these functions, as well. How do you capture the process id from the output (i.e. using some regex on the stdout?) and where do you maintain the lock and pid files? I am sincerely hoping that I can create one script that will work on all linux distros...

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    Quote Originally Posted by kev117 View Post
    Actually, when discussing this with one of my colleagues, a Linux developer, he pointed out that his SUSE distro didn't even have /etc/init.d/functions. Therefore, I am going to have to abandon the use of these functions, as well. How do you capture the process id from the output (i.e. using some regex on the stdout?)
    the pid of the last forked command executed in bash is saved in the $! variable.
    and where do you maintain the lock and pid files?
    i don't really use lock/pid files unless some external process requires it. so if i do, i just use the system dirs already in place for such things, e.g.:

    /var/run/prog.pid
    /var/lock/subsys/prog

    i prefer to use the initscript itself to gather status/pid info about the program, versus using files. files tend to get stale/blown away, etc. and wind up confusing me and the system.

    I am sincerely hoping that I can create one script that will work on all linux distros...
    here's a skeleton initscript that reflects most of the things i typically do. it tries to be distro-agnostic, but could use some tweaks. put it in /etc/init.d/ and call it whatever you like. it can be passed the "start", "stop", "status" and "restart" arguments and so is (somewhat) SyS V compatible.

    you can put a chkconfig line and a description line in it and use Red Hat's chkconfig utility to control it. using chkconfig for example, you can add the runlevel links that start up the initscript (and thus your prog) in the appropriate runlevels. if you don't use chkconfig, then you'll have to create the symlinks yourself.

    just change the progpath variable to your binary and the opts variable to the command line options your binary uses and see if it helps you any.

    Code:
    #!/bin/bash
    
    # absolute path to executable binary
    progpath='/usr/X11R6/bin/xcalc'
    
    # arguments to script
    opts='-rpn'
    
    # binary program name
    prog=$(basename $progpath)
    
    # pid file
    pidfile="/var/run/${prog}.pid"
    
    # make sure full path to executable binary is found
    ! [ -x $progpath ] && echo "$progpath: executable not found" && exit 1
    
    eval_cmd() {
      local rc=$1
      if [ $rc -eq 0 ]; then
        echo '[  OK  ]'
      else
        echo '[FAILED]'
      fi
      return $rc
    }
    
    start() {
      # see if running
      local pids=$(pidof $prog)
    
      if [ -n "$pids" ]; then
        echo "$prog (pid $pids) is already running"
        return 0
      fi
      printf "%-50s%s" "Starting $prog: " ''
      $progpath $opts &
    
      # save pid to file if you want
      echo $! > $pidfile
    
      # check again if running
      pidof $prog >/dev/null 2>&1
      eval_cmd $?
    }
    
    stop() {
      # see if running
      local pids=$(pidof $prog)
    
      if [ -z "$pids" ]; then
        echo "$prog not running"
        return 0
      fi
      printf "%-50s%s" "Stopping $prog: " ''
      rm -f $pidfile
      kill -9 $pids
      eval_cmd $?
    }
    
    status() {
      # see if running
      local pids=$(pidof $prog)
    
      if [ -n "$pids" ]; then
        echo "$prog (pid $pids) is running"
      else
        echo "$prog is stopped"
      fi
    }
    
    case $1 in
      start)
        start
        ;;
      stop)
        stop
        ;;
      status)
        status
        ;;
      restart)
        stop
        sleep 1
        start
        ;;
      *)
        echo "Usage: $0 {start|stop|status|restart}"
        exit 1
    esac
    
    exit $?

  5. #5
    Just Joined!
    Join Date
    Jul 2012
    Posts
    3

    Thumbs up

    Perfect. That is exactly the advice that I was looking for. Thanks so much for the help. Your skeleton is almost exactly like my current implementation, except that it doesn't make the calls to the /etc/init.d/functions, so it should be distro-agnostic.

    $! ... I tried finding this and must've skimmed past it a bunch of times. Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •