deamons and IPC

M

Moritz Karbach

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
 
J

J. Gleixner

Moritz said:
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
 
D

David Efflandt

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.
 
M

Moritz Karbach

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
 
J

Joe Smith

Moritz said:
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
 
A

Anno Siegel

Moritz Karbach said:
Ok, I'm gonna check this again.


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top