running linux daemon in the background using screen

I need to run a php script as a daemon in the background. The thing is I may need to go back and check what’s going on with the script. Using screen window manager we can achieve this in a pretty nice way.

here’s what I did:

# cat /etc/init.d/dahdiDNDonOff
#!/bin/sh
# chkconfig: 2345 90 10
# description: This script is used to start/stop the Asternic Queue parser
case "$1" in
'start')
cd /var/lib/asterisk/agi-bin/ami/
#    DAHDIDND=`nohup php dahdiOnOff.php > /dev/null &`
DAHDIDND=`/usr/bin/screen -S php /var/lib/asterisk/agi-bin/ami/dahdiOnOff.php`

DAHDIDNDSTOP=`ps -ef |grep [d]ahdiOnOff.php | awk {'print $2'}`
echo "PID is: "$DAHDIDNDSTOP
#        $DAHDIDND
RETVAL=$?
;;
'stop')
DAHDIDNDSTOP=`ps -ef |grep [d]ahdiOnOff.php | awk {'print $2'}`
echo "PID is: "$DAHDIDNDSTOP

kill -9 $DAHDIDNDSTOP
RETVAL=$?
;;
*)
echo "Usage: $0 { start | stop }"
RETVAL=1
;;
esac
exit $RETVAL

to get the list of screens run:
screen -ls

to connect to an already attached screen run:
screen -S <socketname>

To connect to a detached screen run:
screen -r <socketname>

here are some useful shortcuts:
Ctrl+a w: show the list of active screens.
Ctrl+a d: detach from the screen
Ctrl+a k: kill the screen

 

Resource:

http://www.rackaid.com/resources/linux-screen-tutorial-and-how-to/

http://magazine.redhat.com/2007/09/27/a-guide-to-gnu-screen/

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *