ali_kerem has asked for the wisdom of the Perl Monks concerning the following question:
Hello All,
I have a serious problem about daemonizing my executable perl script ( I used a free compiler on the net, perlc ) . I use standart template ( Centos 5.9 ) but due to a problem I could not figure out the reason, after random time it fails to start, and since I made it in while loop it restarts forever. For example, it works 2-3 hours, then it fails and begins to restart forever. After waiting for a while ( for example; 20 minutes ) when I try to start it again, it works. So I thought can it be related to memory usage or something like that, but what should I do to solve it ?
Perl script has an infinite loop ( while (1) ), after opening a socket connection to another server it enters this loop and uses this connection to transmit some hex codes produced in the script if it sees a certain file in a certain folder ( so it checks that place continuously ). Unfortunately I cannot share the code here, but my question is what is the best way to clear all variables at the end of the last step and start the loop like it was first try ?
Here is the daemon code;#!/bin/sh # #chkconfig: 2345 99 30 #description: myScript #processname: myScript # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 1 # Process name ( For display ) NAME=myScript # Daemon name, where is the actual executable DAEMON=/myScript/APP/myScript # Daemon user DAEMONUSER=scriptUser # pid file for the daemon PIDFILE=/var/run/myScript/myScript.pid # log file for deamon LOGFILE=/var/log/myScript.log # Define variables PS_CMD="ps -e -o pid,args" proc_list=`$PS_CMD | grep $NAME | grep -v grep | grep -v mqReader | gr +ep -v sh | grep -v vi | grep -v su | grep -v tail` procs=`echo $proc_list | awk '{print $1}'` start () { # see if it exists if [ -n "$procs" ]; then echo "$NAME is already running..." elif [ -e "$PIDFILE" ]; then echo "$NAME is already running..." else # start daemon while [ 1 ] do touch $PIDFILE daemon +9 $DAEMON >> $LOGFILE echo "#--------------------------------------- +-----------#" >> $LOGFILE echo "# $NAME quit due to error, re-running in + 15 seconds #" >> $LOGFILE echo "#--------------------------------------- +-----------#" >> $LOGFILE sleep 15 killproc $NAME rm -rf $PIDFILE done fi } stop () { # see if it exists if [ -n "$procs" ]; then # stop daemon killproc $NAME rm -rf $PIDFILE echo -e "\n`date +%a' '%b' '%d' '%H:%M:%S' '%Y` - $NAM +E has been stopped...\n" >> $LOGFILE elif [ -e "$PIDFILE" ]; then rm -rf $PIDFILE echo -e "\n`date +%a' '%b' '%d' '%H:%M:%S' '%Y` - $NAM +E pid file deleted...\n" >> $LOGFILE killproc $NAME else killproc $NAME echo "$NAME is not running..." fi } case "$1" in start) start & ;; stop) stop ;; status) status $NAME RETVAL=$? ;; *) echo $"Usage: $0 {start|stop|status}" exit 1 ;; esac exit 0
and here is the log file;
Thu May 9 12:03:17 2013 - myScript has been started... ^[[60G[^[[0;32m OK ^[[0;39m]^M#------------------------------------- +-------------# # myScript quit due to error, re-running in 15 seconds # #--------------------------------------------------# Thu May 9 12:03:33 2013 - myScript has been started... ^[[60G[^[[0;32m OK ^[[0;39m]^M#------------------------------------- +-------------# # myScript quit due to error, re-running in 15 seconds # #--------------------------------------------------# Thu May 9 12:03:48 2013 - myScript has been started... ^[[60G[^[[0;32m OK ^[[0;39m]^M#------------------------------------- +-------------# # myScript quit due to error, re-running in 15 seconds # #--------------------------------------------------# Thu May 9 12:04:04 2013 - myScript has been started... ^[[60G[^[[0;32m OK ^[[0;39m]^M#------------------------------------- +-------------# # myScript quit due to error, re-running in 15 seconds # #--------------------------------------------------# Thu May 9 12:04:19 2013 - myScript has been started... ^[[60G[^[[0;32m OK ^[[0;39m]^M#------------------------------------- +-------------# # myScript quit due to error, re-running in 15 seconds # #--------------------------------------------------# Thu May 9 12:04:35 2013 - myScript has been started... ^[[60G[^[[0;32m OK ^[[0;39m]^M#------------------------------------- +-------------# # myScript quit due to error, re-running in 15 seconds # #--------------------------------------------------#
Problem solved;
I found the problem but I think I did not understand some of it.
First of all, I forgot an "exit" in the code, so I fixed that one
The other thing is, I used last but noticed that its in one loop ( was thinking it was in two loops ) so either I was thinking something wrong or using "last" wrongly.
All thanks to people pointing me to return source code executions. While its executable it did not give proper errors but in source code part, it showed faulty parts. And it was not related to daemonizing part. Thanks again.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Daemonized perl and clearing variables...
by marto (Cardinal) on May 30, 2013 at 11:55 UTC | |
by ali_kerem (Acolyte) on May 30, 2013 at 12:05 UTC | |
by 5mi11er (Deacon) on May 30, 2013 at 12:30 UTC | |
by ali_kerem (Acolyte) on May 30, 2013 at 13:02 UTC | |
by marto (Cardinal) on May 30, 2013 at 13:16 UTC | |
| |
|
Re: Daemonized perl and clearing variables...
by Anonymous Monk on May 30, 2013 at 12:51 UTC | |
by ali_kerem (Acolyte) on May 30, 2013 at 13:07 UTC | |
|
Re: Daemonized perl and clearing variables...
by clueless newbie (Curate) on May 30, 2013 at 16:28 UTC | |
by ali_kerem (Acolyte) on May 31, 2013 at 05:51 UTC |