Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Below is an init script which starts/stops a daemon. The daemon is unfortunately printing the parsed packet data (from /home/nuthan/program/server), a socket server written in perl, listening to a socket for tcp packets from a client.

As soon as I start the service, the parsed data (print statements from /home/nuthan/program/server) is echoed on my TTY. And, if I try to kill the terminal, the daemon quits. How do I overcome this?

daemon and logging packet data command

 daemon -18 perl /home/nuthan/program/server -d -v -r -f -O=/var/log/vts.log --daemon

#!/bin/bash
#
# vts      Start/Stop Vts.
#
# description: VTS daemon for vehicle Tracking.
# Source function library
. /etc/init.d/functions

# Get network config
. /etc/sysconfig/network

RETVAL=0

start() {
echo -n $"Starting vts: "
# Start me up!
daemon -18 perl /home/nuthan/program/server -d -v -r -f -O=/var/log/vts.log --daemon
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/vts
    touch /var/lock/subsys/vts
return $RETVAL

}

stop() {
echo -n $"Stopping vts: "
killproc vts
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/vtsdaemon
return $RETVAL
}

restart() {
  stop
start
}

reload() {
stop
start
}

case "$1" in
  start)
  start
;;
  stop)
  stop
;;
  status)
status vts
;;
  restart)
  restart
;;
  condrestart)
  [ -f /var/lock/subsys/vts ] && restart || :
;;
  reload)
reload
;;
  *)
echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
exit 1
esac

exit 0
share|improve this question
    
Where does the daemon command come from and what is its usage syntax? –  rozcietrzewiacz Jan 16 '12 at 11:32
add comment

3 Answers

up vote 2 down vote accepted

You are mixing three commands: deamon, perl and /home/nuthan/program/server without any quotes. Think about the following: How does each of them know, which of the parameters it should interpret?

The syntax you used could probably only be correctly interpreted, if:

  • deamon would treat everything after -18 to be the command to run
  • perl would interpret only the first parameter (/home/nuthan/program/server) as the script to run, and all that follows as parameters passed to that script

I can suggest two things to fix the issues:

  1. Make sure whether you need to call perl explicitly. If the server script contains a proper interpreter declaration (probably /usr/bin/perl) on its first line, and it has executable bit set, you should not need to explicitly call perl.
  2. Check the syntax of the daemon command. If it indicates that everything after -18 (in your command) should be treated as a full command to run, then it's ok. Otherwise, you might either need to everything that follows in quotes, or put create an additional function or a wrapper script that would run your entire command - so the entire command line you've written would be changed to daemon -18 your_function or daemon -18 /path/to/your/wrapper_script.sh.
share|improve this answer
    
many thnx... finally, the daemon and perl has to be separated using quotes: daemon -18 "perl /home/nuthan/program/server >/dev/null -d -v -r -f -O=/var/log/vts.log --daemon" 2>&1 1>/home/nuthan/program/vts.log & This worked fine. –  nuthan Jan 16 '12 at 12:28
add comment
daemon -18 perl /home/nuthan/program/server -d -v -r -f -O=/var/log/vts.log --daemon
RETVAL=$? 2>&1 1>/some/where/over_the_rainbow

But note, that it looks like the -O should already do it some of the sort, as should that "daemon" so there could be more ti this than meets the eye.

share|improve this answer
    
thnx for the answer. Now, /some/where/over_the_rainbow logs the print statements. But, still i'm not able to detach tty from the daemon.Dont know why!!! –  nuthan Jan 16 '12 at 11:16
add comment

You would overcome this by redirecting stdout to the abyss (/dev/null) and redirect stderr to where stdout is redirected. /dev/null discards everything it recieves. So something like this could work:

daemon -18 perl /home/nuthan/program/server -d -v -r -f -O=/var/log/vts.log --daemon >/dev/null 2>&1
share|improve this answer
    
redirecting to /dev/null is not working. i cannot detach tty from the daemon. –  nuthan Jan 16 '12 at 11:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.