- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Chnage process name ($0) in a shell script?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-09-2008 07:30 AM
тАО02-09-2008 07:30 AM
Chnage process name ($0) in a shell script?
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-09-2008 07:36 AM
тАО02-09-2008 07:36 AM
Re: Chnage process name ($0) in a shell script?
func1.sh &
func2.sh &
You should still get $! responding from each one. That's the only way I can think of to do it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-09-2008 07:55 AM
тАО02-09-2008 07:55 AM
Re: Chnage process name ($0) in a shell script?
--------------------------
# 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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-09-2008 08:51 AM
тАО02-09-2008 08:51 AM
Re: Chnage process name ($0) in a shell script?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-09-2008 09:02 AM - edited тАО09-17-2011 02:48 PM
тАО02-09-2008 09:02 AM - edited тАО09-17-2011 02:48 PM
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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-10-2008 12:58 AM
тАО02-10-2008 12:58 AM
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 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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-10-2008 06:47 AM
тАО02-10-2008 06:47 AM
Re: Chnage process name ($0) in a shell script?
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);