deamons and IPC

Discussion in 'Perl Misc' started by Moritz Karbach, May 12, 2005.

  1. Hi!

    I'm very new to inter process communication, maybe you have some suggestions
    or comments:

    I'm trying to write a deamon which basically launches some programs
    periodically, and I want to steer the deamon somehow. Eg like

    ../deamon.pl start
    ../deamon.pl stop
    ../deamon.pl something_interesting

    How can I send such messages to my deamon? I came to the conclusion, that
    sockets may be what I need. Maybe some of you have some very easy server
    and client scripts, using tcp and localhost?

    Thanks,

    - Moritz
    Moritz Karbach, May 12, 2005
    #1
    1. Advertising

  2. Moritz Karbach

    J. Gleixner Guest

    Moritz Karbach wrote:
    > Hi!
    >
    > I'm very new to inter process communication, maybe you have some suggestions
    > or comments:
    >
    > I'm trying to write a deamon which basically launches some programs
    > periodically, and I want to steer the deamon somehow. Eg like
    >
    > ./deamon.pl start
    > ./deamon.pl stop
    > ./deamon.pl something_interesting
    >
    > How can I send such messages to my deamon? I came to the conclusion, that
    > sockets may be what I need. Maybe some of you have some very easy server
    > and client scripts, using tcp and localhost?


    You'd think stuff like that would be available all over the place, yeah? :)

    Take a look at the documentation that comes with perl:

    perldoc perlipc

    Search CPAN for "Socket", which will give you a lot of possibly helpful
    modules.

    You may also find a lot of code or examples by using a search engine and
    simply query on what you've already determined you need "perl daemon
    socket".

    Maybe this will help: http://www.stonehenge.com/merlyn/UnixReview/col47.html

    Use the Internet, Luke... :-D
    J. Gleixner, May 12, 2005
    #2
    1. Advertising

  3. On Thu, 12 May 2005, Moritz Karbach <> wrote:
    > Hi!
    >
    > I'm very new to inter process communication, maybe you have some suggestions
    > or comments:
    >
    > I'm trying to write a deamon which basically launches some programs
    > periodically, and I want to steer the deamon somehow. Eg like
    >
    > ./deamon.pl start
    > ./deamon.pl stop
    > ./deamon.pl something_interesting
    >
    > How can I send such messages to my deamon? I came to the conclusion, that
    > sockets may be what I need. Maybe some of you have some very easy server
    > and client scripts, using tcp and localhost?


    perldoc perlipc not only tells how to communicate with processes, but also
    how to have a script automatically deamonize itself. While tcp sockets
    may be handy for remote control, if you just need to communicate with it
    from a local shell, you could use a fifo (named pipe). Then you could
    simply have the deamon script read the fifo, which you could echo or write
    to like a regular file to feed it commands.
    David Efflandt, May 13, 2005
    #3
  4. > perldoc perlipc not only tells how to communicate with processes, but also
    > how to have a script automatically deamonize itself.


    Ok, I'm gonna check this again.

    > While tcp sockets
    > may be handy for remote control, if you just need to communicate with it
    > from a local shell, you could use a fifo (named pipe). Then you could
    > simply have the deamon script read the fifo, which you could echo or write
    > to like a regular file to feed it commands.


    In fact this is what I tried first. But I stuck because

    my $result = <FIFO>;

    waits until some other process writes into the pipe. This is why something
    simple like

    while(my $result = <FIFO>)
    {
    if ( $result=~m/stop/ )
    {
    exit;
    }

    system("./launch_this_every_second.sh");

    sleep 1;
    }

    doesn't work. Or am I missing something obvious?

    - Moritz
    Moritz Karbach, May 13, 2005
    #4
  5. Moritz Karbach

    Joe Smith Guest

    Moritz Karbach wrote:

    > In fact this is what I tried first. But I stuck because
    >
    > my $result = <FIFO>;
    >
    > waits until some other process writes into the pipe.
    > Or am I missing something obvious?


    Don't read from the socket unless you know it won't block.

    for($running=1;$running;) {
    if(input_available($socket)) {
    $command = <$socket>;
    check_for_stop_command($command) and $running = 0;
    check_for_other_command($command) and do_something_else();
    } else {
    perform_one_iteration();
    }
    }

    Another approach is to use fork() and shared memory.
    -Joe
    Joe Smith, May 13, 2005
    #5
  6. Moritz Karbach

    Anno Siegel Guest

    Moritz Karbach <> wrote in comp.lang.perl.misc:
    > > perldoc perlipc not only tells how to communicate with processes, but also
    > > how to have a script automatically deamonize itself.

    >
    > Ok, I'm gonna check this again.
    >
    > > While tcp sockets
    > > may be handy for remote control, if you just need to communicate with it
    > > from a local shell, you could use a fifo (named pipe). Then you could
    > > simply have the deamon script read the fifo, which you could echo or write
    > > to like a regular file to feed it commands.

    >
    > In fact this is what I tried first. But I stuck because
    >
    > my $result = <FIFO>;
    >
    > waits until some other process writes into the pipe. This is why something
    > simple like
    >
    > while(my $result = <FIFO>)
    > {
    > if ( $result=~m/stop/ )
    > {
    > exit;
    > }
    >
    > system("./launch_this_every_second.sh");
    >
    > sleep 1;
    > }
    >
    > doesn't work. Or am I missing something obvious?


    You can open the pipe with sysopen() and use the O_NDELAY flag.
    Then the pipe will not block, neither on open() nor on a read
    attempt.

    use Fcntl qw( O_RDONLY O_NDELAY);
    my $fifo;
    sysopen $fifo, $_, O_RDONLY | O_NDELAY or die "$_: $!" for '/tmp/pipe';

    while( 1 ) {
    if ( defined( my $result = <$fifo>) ) {
    print "got $result";
    last if $result =~ /stop/;
    }
    system( "echo Just my job, Sir"); sleep 1;
    }

    Anno
    Anno Siegel, May 13, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gary
    Replies:
    3
    Views:
    541
    Steve Grazzini
    Sep 18, 2003
  2. Brian Hann

    IPC::Run and hanging pipes

    Brian Hann, Dec 8, 2003, in forum: Perl
    Replies:
    1
    Views:
    788
    Brian Hann
    Dec 11, 2003
  3. Dave Newberry
    Replies:
    0
    Views:
    648
    Dave Newberry
    Oct 17, 2004
  4. Paul Tremblay
    Replies:
    1
    Views:
    1,266
    Victor Bazarov
    Jun 16, 2005
  5. Mike Nicholaides

    Deamons.rb: changing the pid directory

    Mike Nicholaides, Oct 11, 2008, in forum: Ruby
    Replies:
    1
    Views:
    94
    Brian Candler
    Oct 12, 2008
Loading...

Share This Page