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.


In reply to Daemonized perl and clearing variables... by ali_kerem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.