rimvydazas has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a perl script,and I want to run 3 instances of it (the same script but listen on different sockets for data). Is there any way to assign custom name to each of them that I could do checkups with cron? Now, when I do ps, I can't distinguish between them by using process name:
#ps -e |grep portcap 17790 ? 00:00:00 portcap-0.06_re 18022 ? 00:00:00 portcap-0.06_re 18028 ? 00:00:00 portcap-0.06_re
Of course, it would work just by copying three files and renaming them to different ways, but maybe there is a programmable way? Is there a way to reserve and assign custom PID? Thanks PerlMonks!

Replies are listed 'Best First'.
Re: custom name to the process
by Fletch (Bishop) on Dec 04, 2007 at 21:44 UTC

    Some OSen will allow you to assign to $0; if yours is one of them then you could use that. Otherwise your best bet might be using symlinks to the real file, or consider adding a dummy argument that's ignored that you use to differentiate between instances.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Is there any way to assign different command options/arguments for each symlinc?
        I've used a symlinking approach allowing the real script to see how it was called, using a dispatch table.. perhaps this will work for your purposes:

        Create a script for example /usr/sbin/myscript.pl
        #!/usr/bin/perl use strict; use warnings; my $ME = $0 ; $ME =~ s@^\.\/|\/.*\/@@g; $ME =~ /myscript.pl/ and die ("\n Please dont call me directly!\n\n"); my %workerscripts = ( 'worker01' => \&worker01, 'worker02' => \&worker02, 'worker03' => \&worker03 ); my $real_command = $workerscripts{&findME}; &$real_command; #--- sub findME { while ( my($k,$v) = each %workerscripts ) { return $k if $k =~ /$ME/ ; }; }; sub worker01 { print "worker01 was called\n"; exit 0; }; sub worker02 { print "worker02 was called\n"; exit 0; }; sub worker03 { print "worker03 was called\n"; exit 0; };


        Then create links to that script:
        cd /usr/bin/ ln -s /usr/sbin/myscript.pl worker01 ln -s /usr/sbin/myscript.pl worker02 ln -s /usr/sbin/myscript.pl worker03
        Invoke /usr/bin/worker01, worker02, or worker03 and the proper subroutine should be called..

        Does that get you closer?
        -Harold

        Well, you can check the value of $0 and do different things with it. But probably the best solution is to use different parameters to ps. For instance, ps -fe will give you the parameters passed to each instance, which is probably a pretty good way of distinguishing your processes.