Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

How do I run a script in the background? How should I run a script as daemon?

by jeroenes (Priest)
on Nov 16, 2000 at 21:16 UTC ( #41998=categorized question: print w/replies, xml ) Need Help??
Contributed by jeroenes on Nov 16, 2000 at 21:16 UTC
Q&A  > input and output


These are related questions. I asked the first question in the node Exec'd perl running wild on cpu-time.

Answer: How do I run a script in the background? How should I run a script as daemon?
contributed by jeroenes

If you run a script in a the background with exec, things go nasty after a while when you kill your terminal. You can prevent this (I tried it only under linux) by setting SIG{INT}='IGNORE'.

Actually, a better solution is the Proc::daemon module, or the Net::daemon module if you need to claim a socket.

Answer: How do I run a script in the background? How should I run a script as daemon?
contributed by c0d34w4y

Try this...

close STDIN; close STDOUT; close STDERR; if (open(DEVTTY, "/dev/tty")) { ioctl(DEVTTY,0x20007471,0); close DEVTTY; } open(STDIN,"</dev/null"); open(STDOUT,">/dev/null"); open(STDERR,">&STDOUT"); # at this point there are two processes... for parent, fork wi +ll return a 'true' number, # for child it'll return 0. thus, parent will exit and child w +ill remain to run. fork && exit; # certain signals should be ignored $SIG{"HUP"} = $SIG{"ALRM"} = $SIG{"PIPE"} = $SIG{"INT"} = "IGN +ORE"; # set some priority for the process... # (so that it doesn't end up wasting your # server resources) setpriority( "PRIO_PROCESS", 0, 10 );

Please (register and) log in if you wish to add an answer

  • 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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    [stevieb]: beech already found a thing I don't like... it'll take time to sort out some new methods to deal with little/big endian ;)
    [stevieb]: yeah, going to have to make some changes when reading a 'word'. I digress, for now

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (7)
    As of 2017-07-22 00:09 GMT
    Find Nodes?
      Voting Booth?
      I came, I saw, I ...

      Results (336 votes). Check out past polls.