SPUG: assigning to $0; different Linuces

Jeremy G Kahn kahn at cpan.org
Thu Aug 28 20:03:20 CDT 2003


Has anybody else discovered that the ability to assign with $0 varies 
across even Linux versions?

I wrote a very simple script to test this:

#!/usr/bin/perl
print "getting started\n";
sleep 5;
$0 = "foo";
sleep 5;
print "done!\n";

When I run it on Debian (kernel 2.4, perl 5.8.0 
i386-linux-thread-multi), I can check the ps results thusly:

jeremy at mystique:~$ ./test.pl& for f in 1 2 3; do ps -ef | grep foo; ps 
-ef | grep test; sleep 3; done
[1] 5803
getting started
jeremy    5805  5726  0 17:48 pts/4    00:00:00 grep foo
jeremy    5803  5726  0 17:48 pts/4    00:00:00 /usr/bin/perl ./test.pl
jeremy    5807  5726  0 17:48 pts/4    00:00:00 grep test
jeremy    5810  5726  0 17:48 pts/4    00:00:00 grep foo
jeremy    5803  5726  0 17:48 pts/4    00:00:00 /usr/bin/perl ./test.pl
jeremy    5812  5726  0 17:48 pts/4    00:00:00 grep test
jeremy    5803  5726  0 17:48 pts/4    00:00:00 foo                   
jeremy    5817  5726  0 17:48 pts/4    00:00:00 grep test
jeremy at mystique:~$ done!

See how 5803 successfully changes its name ($0) to "foo" instead of 
test.pl ?

Clever, eh? I'm using this as a status monitor for some locking tools.

But when I run the same thing on redhat 7.3 (kernel 2.4, perl version 
5.8.0 for i686-linux) I get these results:

[1] 14104
getting started
jgk      14106 11162  0 17:52 pts/5    00:00:00 grep foo
jgk      14104 11162  0 17:52 pts/5    00:00:00 /usr/nikola/bin/perl 
./test.pl
jgk      14108 11162  0 17:52 pts/5    00:00:00 grep test
jgk      14111 11162  0 17:52 pts/5    00:00:00 grep foo
jgk      14104 11162  0 17:52 pts/5    00:00:00 /usr/nikola/bin/perl 
./test.pl
jgk      14113 11162  0 17:52 pts/5    00:00:00 grep test
jgk      14116 11162  0 17:52 pts/5    00:00:00 grep foo
jgk      14104 11162  0 17:52 pts/5    00:00:00 /usr/nikola/bin/perl 
./test.pl
jgk      14118 11162  0 17:52 pts/5    00:00:00 grep test
bash-2.05a$ all done!

Notice that the name of process 14104 does *not* change.

This is really irritating, since the assign-to-$0 is a nice feature, 
when you're doing lots of fork-ing and exec-ing.  Does anybody know how 
one would check that a system can do this from within a script? Or a 
configuration variable in Linux that somebody might be able to tune?

--jeremy






More information about the spug-list mailing list