Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > deamons and IPC

Reply
Thread Tools

deamons and IPC

 
 
Moritz Karbach
Guest
Posts: n/a
 
      05-12-2005
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
 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      05-12-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?


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...
 
Reply With Quote
 
 
 
 
David Efflandt
Guest
Posts: n/a
 
      05-13-2005
On Thu, 12 May 2005, Moritz Karbach <(E-Mail Removed)> 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.
 
Reply With Quote
 
Moritz Karbach
Guest
Posts: n/a
 
      05-13-2005
> 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
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      05-13-2005
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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      05-13-2005
Moritz Karbach <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deamons.rb: changing the pid directory Mike Nicholaides Ruby 1 10-12-2008 09:25 AM
Another newbie question re: STDERR and IPC::open3(), nowait() and zombies Dave Newberry Perl 0 10-17-2004 06:41 PM
Mail Deamons Arnold H. Hollander Computer Support 0 06-15-2004 02:47 AM
IPC::Run and hanging pipes Brian Hann Perl 1 12-11-2003 08:41 PM
open, ipc, and return codes gary Perl 3 09-18-2003 12:17 AM



Advertisments