Operating System - Linux
1752601 Members
4623 Online
108788 Solutions
New Discussion юеВ

Chnage process name ($0) in a shell script?

 

Chnage process name ($0) in a shell script?

Hi all,

I have a bunch of shell scripts that call shell functions in the background - here's a simplified example:

--------------------------

# cat myscript.sh

#!/usr/bin/sh
# shell script with background functions

function func1
{
sleep 10
}

function func2
{
sleep 20
}

func1 &
x=$!
func2 &
y=$!

wait x
wait y
# end of script

--------------------------

Now when this script is running - if I query the process table with 'ps -ef' I will see 3 instances of 'myscript.sh' running. Is there any way I can assign a different name to the sub-shells that are created when the functions are called in the background so that they show up with different names in the process table? I don't want to have to breal these functions out into seperate shell scripts...

Thx

Duncan

I am an HPE Employee
Accept or Kudo
6 REPLIES 6
Matt Hearn
Regular Advisor

Re: Chnage process name ($0) in a shell script?

I think you'd have to define the functions in external files, like func1.sh and func2.sh, and then background them thusly:

func1.sh &
func2.sh &

You should still get $! responding from each one. That's the only way I can think of to do it.

Re: Chnage process name ($0) in a shell script?

Yes, but thats what I wanted to avoid doing... actually the esample I gave wasn't great as it would probably just show 2 sleep processes in the process table - this gives the (un)desired effec:

--------------------------

# cat myscript.sh

#!/usr/bin/sh
# shell script with background functions

function func1
{
while true
do
:
done
}

function func2
{
while true
do
:
done
}

func1 &
x=$!
func2 &
y=$!

wait x
wait y
# end of script

--------------------------


Duncan

I am an HPE Employee
Accept or Kudo
Hein van den Heuvel
Honored Contributor

Re: Chnage process name ($0) in a shell script?

I would suggest to use a command line argument to show the sub-function.
Something like:

#!/usr/bin/sh
# shell script with background functions
print "$$ 0=$0 1=$1"
case $1 in
func1)
sleep 10
;;
func2)
sleep 20
;;
*)
$0 func1 &
x=$!
$0 func2 &
y=$!
UNIX95= ps -u hein -o ppid,pid,args
wait $x
wait $y
print "All done"
;;
esac
# end of (all) scripts

Dennis Handly
Acclaimed Contributor

Re: Change process name ($0) in a shell script?

I doubt there is any way. Why do you care?

>Is there any way I can assign a different name

While you can't change the name, you can pass a parm to it with its name, so you can look closer on the ps -ef output.
(Hmm, Hein had the same idea.)

Re: Chnage process name ($0) in a shell script?

Hein,

Thanks that works for my example in that you can differentiate between the parent/child scripts in a ps output without having to muck around with PID/PPID to figure everything out, but would take too much code to change the real scripts... being a lazy type if it can't be done with a one-liner I probably won't bother!


Dennis,

I only care cos I'm lazy and want an easy way of doing this.

Thanks,

Duncan

I am an HPE Employee
Accept or Kudo
Hein van den Heuvel
Honored Contributor

Re: Chnage process name ($0) in a shell script?

> Thanks that works for my example in that you can differentiate between the parent/child scripts

It tends to work nicely allowing one to have just one larger script for a bunch of related but independent task.

For me it beats a collection of scripts like 'task_first_time, task_daily, task_report,...' and gives a common point (the start of the script) to define a buch of task related goodies.

Furthermore, it's the only way to do this on OpenVMS, my home, because it's shell (DCL) does not allow detached subroutines, only 'spawned' images/scripts.

>> would take too much code to change the real scripts... being a lazy type if it can't be done with a one-liner I probably won't bother!

My example shows the case doing the work, and then joining a main tail code.
The case could be a straight forward call to original subroutines + exit, making for a quick straightforward (scriptable!) edit.

>> I only care cos I'm lazy and want an easy way of doing this.

Lazy is good!

Just for yuks I made a perl script to do the edits for you. But because I am lazy too, you need to add 1 line to your original script marking the end to the function definitions and the begin of main line.
That line should start with #magic.
Works on example script
But is probably an excercise in futility as I did not bother with function arguments.
Anyway, silly script below.

Hein.

Example usage:

#perl in_and_out_burger.pl old > new

Source:

$ cat in_and_out_burger.pl
use strict;
use warnings;
my $tagline = '#magic';
my ($i, $magic, $function, %defined_functions, %detached_functions);
my @script = <>;
my $lines = @script - 1;
for $i ( 0 .. $lines ) {
$_ = $script[$i];
if ($magic) {
if (/^s*(\w+).*?&$/) {
if ($defined_functions{$1}) {
$detached_functions{$1}=1;
$script[$i] = '$0 ' . $_ ;
}
}
} else {
if (/^$tagline/) {
die "Been there done that." if /done/;
$script[$i] =~ s/$/ done./;
$magic = $i;
}
$defined_functions{$1}=1 if /^\s*function\s+(\w+)/;
}
}
die "No Magic dividing line found" unless $magic;
print $script[$_] for (0..$magic);
$magic++;
print "\ncase \$1 in\n";
foreach $function (sort keys %detached_functions) {
print "$function)\n $function\n exit\n;;\n";
}
print "*)\n;;\nesac\n";
print $script[$_] for ($magic..$lines);