Vincent
Vincent

Reputation: 21

How to read the option when a perl script is running?

Assume that I have a perl script, which keep running in the background, say run.pl How can I interact with this script by calling it self? such as: perl run.pl -reload , perl run.pl -reset

Is it possible to do it? If yes, which modular should I study?

Thanks a lot!

Upvotes: 0

Views: 170

Answers (3)

Igor Chubin
Igor Chubin

Reputation: 64563

There are several methods of interprocess communications in a operating system (in case of Linux/Unix this is pipes, FIFOs, signals, shared memory and several other). Perl supports all of them.

I think, that the best solution for your task is to use signals.

So you can intercept a signal in Perl:

sub INT_handler {
    print("Don't Interrupt!\n");
}

$SIG{'INT'} = 'INT_handler';

And so you can send signals:

kill  9     => $pid;                    # send $pid a signal 9
kill -1     => $pgrp;                   # send whole job a signal 1
kill  USR1  => $$;                      # send myself a SIGUSR1
kill  HUP   => @pids;                   # send a SIGHUP to processes in @pids

More about signals in Learning Perl, chapter 16.8 Sending and Receiving Signals.

There are not so many signals available in an OS (maximum 64), and signals that you can use for your own purposes even fewer.

If you need something more powerful, use sockets or message passing systems like ZeroMQ.

Upvotes: 1

CyberDem0n
CyberDem0n

Reputation: 15036

IPC (perldoc perlipc) can be done by various methods:

  1. Using sockets
  2. Using signals
  3. Using shared memory

I think the most simple way is handle for signal HUP in script, and after the signal is received, read some file for additional information. Of course, you must write this file before sending the signal.

Upvotes: 2

Ivan Kruglov
Ivan Kruglov

Reputation: 753

Since your perl-script runs in background you can interact with it directly. But, there are few workarounds:

  1. Files. You can use files as a trigger for some actions. Imagine the you have one copy of run.pl running in background and it constantly checks existing of file /tmp/file. If it does not exist the script simply continues checking, if it doest - the script to whatever you want. Some if your command "perl run.pl -reload" will create this file you will achieve what you want.
  2. Signals. You can sent a signal to the process. How to work with signals in perl scripts see here

Upvotes: 0

Related Questions