Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

sending variables to a script running in a bg

by baxy77bax (Chaplain)
on Oct 22, 2009 at 12:55 UTC ( #802735=perlquestion: print w/replies, xml ) Need Help??
baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:


continuation of my everlasting problem

what i wish to do is to run a program :

$|=1; while(1){ chomp(my $say = <>); ($say eq 'q') ? (exit(0)) : (print "\nI caught $say \n"); }
in background.

once the program is started, from time to time (when there is a need) i wish to pass some data (a,b,c,d...) to it. i have no clue how to do this?? should i do it by creating some file that i can read and write to, that program in bg can somehow check when the contest is changed, or is there a way to directly send input data to it ? ... is there a module that deals with this ....

please help!!

thank you !

P.S. the program has to be running because it stores some data in memory depending on the input passed to it and latter that stored data is being accessed depending on a variable passed to it in a second iteration.

Replies are listed 'Best First'.
Re: sending variables to a script running in a bg
by Corion (Pope) on Oct 22, 2009 at 13:08 UTC

    There are many approaches to this. Four of the more common for the background process are:

    • A web server (like HTTP::Server::Simple)
    • An irc bot which connects to an irc server to receive the commands (Bot::BasicBot
    • A shared directory which the background process polls, or a named pipe/mailslot to which commands are written
    • A database table into which new jobs are written

    Depending on your needs and capabilities, any of these might be suitable or unsuitable. For example the webserver requires very little infrastructure and has lots of client tools and infrastructure available. The irc client requires more infrastructure, but allows easy restart/addition of both servers and clients as long as the irc server itself remains up. It provides no recovery/queueing of commands if no server can handle them. The filesystem approach can store things indefinitely but does usually have interesting problems when working over the network. Using a database requires the appropriate database and its drivers.

    Other solutions are SMTP ("email"), a protocol which has very robust and established retry and failure mechanisms and scales very well.

Re: sending variables to a script running in a bg
by jakobi (Pilgrim) on Oct 22, 2009 at 13:09 UTC

    The oldest trick in the book is probably using a flag file in the filesystem with something like while(1){sleep 1; -f $FLAGFILE and do{ # open file if flag file exists, etc }}. The flag is created by the writer when the data files have been fully created. Have the reader process (your background program) check the flag and remove it when done.

    Other ways to communicate, queue or synchronize asynchronous processes are signals or ipc (mmap to shared memory; perlipc). Check e.g. the recent mmap thread.

    At a in-process level, you can use the same concepts based on threads instead of processes: threads. Be a bit careful in mixing multiprocessing and threads (-> signals...), and consider dedicating some thread(s?) for specific IO-tasks.

    The bold words above should cover the more interesting keywords to use in searching for further ideas, modules, or frameworks.

    cu & HTH, Peter -- hints may be untested unless stated otherwise; use with caution & understanding.
Re: sending variables to a script running in a bg
by cdarke (Prior) on Oct 22, 2009 at 14:30 UTC
    Here is a simple mechanism, assuming you are running on UNIX, use a named pipe. Create the pipe using mkfifo(1) and just treat it as you would any other file.
    If you open it (for read or write) when there is no one at the other end then you will block, but you can synchronise using a second empty file. When you want the background job to proceed then just touch the sync file:
    my $syncfile = 'sync.fred'; my $start = (stat $syncfile)[9]; while ($start == (stat $syncfile)[9]) { sleep 1; } print "Now we can read the pipe\n";
Re: sending variables to a script running in a bg
by kgish (Acolyte) on Oct 22, 2009 at 15:46 UTC
    This sounds to me like a classic ipc problem. Have a look at perlipc.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://802735]
Approved by Corion
and the grasshoppers chirp...

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

    Results (383 votes). Check out past polls.