Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Disowning CRON

by daveime (Novice)
on Jun 05, 2003 at 15:06 UTC ( #263364=perlquestion: print w/replies, xml ) Need Help??

daveime has asked for the wisdom of the Perl Monks concerning the following question:

Under Linux Redhat 7.something, Perl 5.6.something, when a perl script is triggered by a crontab entry, how can i then cause the script to disown it's parent and act as an orphan.

I need this as I'm writing a Perl DAEMON, and I need cron to "kick the DAEMON" every 20 minutes to see if it's still alive.

So my DAEMON script itself is triggered by crontab every 20 mins, the first thing it does is look for another instance of DAEMON running, if it finds one then this instance exits immediately.

If there is no other instance running, this script has to become the new DAEMON, so I then need to disown it from it's parent cron so that cron doesn't hang waiting for the DAEMON to finish (which it shouldn't do !!).

(If this isn't the most garbled nonsense I ever wrote !)

Please help, someone ?

Thanks Dave

Replies are listed 'Best First'.
Re: Disowning CRON
by daeve (Deacon) on Jun 05, 2003 at 15:40 UTC
Re: Disowning CRON
by hardburn (Abbot) on Jun 05, 2003 at 15:09 UTC

    Instead of doing this from the daemon program itself, why not run a shell script from cron that checks for the daemon running and starts a new one if it's not? Seems to be a lot simpler than trying to do everything in the daemon code.

    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: Disowning CRON
by zby (Vicar) on Jun 05, 2003 at 15:24 UTC
    I think you can put it into background at once. To do the exclusion you can lock $0. This way the daemon will just die when it is started when another copy of it is allready working. Just don't do it in the BEGIN block: Never lock $0 inside of a BEGIN block.
Re: Disowning CRON
by Dog and Pony (Priest) on Jun 05, 2003 at 16:30 UTC
    Well, either fork off the process as soon as you've realized you need a new daemon, or simply start the script in the background, freeing it from cron.
    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
      Many thanks to hardburn, zby, daeve, Dog and Pony !!

      I'll try starting as a background from cron ... that seems the easiest solution.

        If you're writing in a Linux box (i cant vouch for the other unixes) you can check out the list of start-up scripts in /etc/init.d.

        A few scripts are written to be 're-startable', and will find and restart the process in question when run with the option '-restart'.

        These scripts would give you a good idea of how they look for a process already running, in order to stop it, and start it again.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://263364]
Approved by phydeauxarff
Front-paged by broquaint
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2021-11-28 01:45 GMT
Find Nodes?
    Voting Booth?

    No recent polls found