<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Chnage process name ($0) in a shell script? in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/chnage-process-name-0-in-a-shell-script/m-p/4142655#M93360</link>
    <description>&lt;!--!*#--&gt;Hi all,&lt;BR /&gt;&lt;BR /&gt;I have  a bunch of shell scripts that call shell functions in the background - here's a simplified example:&lt;BR /&gt;&lt;BR /&gt;--------------------------&lt;BR /&gt;&lt;BR /&gt;# cat myscript.sh&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;# shell script with background functions&lt;BR /&gt;&lt;BR /&gt;function func1&lt;BR /&gt;{&lt;BR /&gt;  sleep 10&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;function func2&lt;BR /&gt;{&lt;BR /&gt;  sleep 20&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;func1 &amp;amp;&lt;BR /&gt;x=$!&lt;BR /&gt;func2 &amp;amp;&lt;BR /&gt;y=$!&lt;BR /&gt;&lt;BR /&gt;wait x&lt;BR /&gt;wait y&lt;BR /&gt;# end of script&lt;BR /&gt;&lt;BR /&gt;--------------------------&lt;BR /&gt;&lt;BR /&gt;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...&lt;BR /&gt;&lt;BR /&gt;Thx&lt;BR /&gt;&lt;BR /&gt;Duncan&lt;BR /&gt;</description>
    <pubDate>Sat, 09 Feb 2008 15:30:36 GMT</pubDate>
    <dc:creator>Duncan Edmonstone</dc:creator>
    <dc:date>2008-02-09T15:30:36Z</dc:date>
    <item>
      <title>Chnage process name ($0) in a shell script?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/chnage-process-name-0-in-a-shell-script/m-p/4142655#M93360</link>
      <description>&lt;!--!*#--&gt;Hi all,&lt;BR /&gt;&lt;BR /&gt;I have  a bunch of shell scripts that call shell functions in the background - here's a simplified example:&lt;BR /&gt;&lt;BR /&gt;--------------------------&lt;BR /&gt;&lt;BR /&gt;# cat myscript.sh&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;# shell script with background functions&lt;BR /&gt;&lt;BR /&gt;function func1&lt;BR /&gt;{&lt;BR /&gt;  sleep 10&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;function func2&lt;BR /&gt;{&lt;BR /&gt;  sleep 20&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;func1 &amp;amp;&lt;BR /&gt;x=$!&lt;BR /&gt;func2 &amp;amp;&lt;BR /&gt;y=$!&lt;BR /&gt;&lt;BR /&gt;wait x&lt;BR /&gt;wait y&lt;BR /&gt;# end of script&lt;BR /&gt;&lt;BR /&gt;--------------------------&lt;BR /&gt;&lt;BR /&gt;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...&lt;BR /&gt;&lt;BR /&gt;Thx&lt;BR /&gt;&lt;BR /&gt;Duncan&lt;BR /&gt;</description>
      <pubDate>Sat, 09 Feb 2008 15:30:36 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/chnage-process-name-0-in-a-shell-script/m-p/4142655#M93360</guid>
      <dc:creator>Duncan Edmonstone</dc:creator>
      <dc:date>2008-02-09T15:30:36Z</dc:date>
    </item>
    <item>
      <title>Re: Chnage process name ($0) in a shell script?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/chnage-process-name-0-in-a-shell-script/m-p/4142656#M93361</link>
      <description>I think you'd have to define the functions in external files, like func1.sh and func2.sh, and then background them thusly:&lt;BR /&gt;&lt;BR /&gt;func1.sh &amp;amp;&lt;BR /&gt;func2.sh &amp;amp;&lt;BR /&gt;&lt;BR /&gt;You should still get $! responding from each one.  That's the only way I can think of to do it.</description>
      <pubDate>Sat, 09 Feb 2008 15:36:37 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/chnage-process-name-0-in-a-shell-script/m-p/4142656#M93361</guid>
      <dc:creator>Matt Hearn</dc:creator>
      <dc:date>2008-02-09T15:36:37Z</dc:date>
    </item>
    <item>
      <title>Re: Chnage process name ($0) in a shell script?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/chnage-process-name-0-in-a-shell-script/m-p/4142657#M93362</link>
      <description>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:&lt;BR /&gt;&lt;BR /&gt;--------------------------&lt;BR /&gt;&lt;BR /&gt;# cat myscript.sh&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;# shell script with background functions&lt;BR /&gt;&lt;BR /&gt;function func1&lt;BR /&gt;{&lt;BR /&gt;  while true&lt;BR /&gt;  do&lt;BR /&gt;    :&lt;BR /&gt;  done&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;function func2&lt;BR /&gt;{&lt;BR /&gt;  while true&lt;BR /&gt;  do&lt;BR /&gt;    :&lt;BR /&gt;  done&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;func1 &amp;amp;&lt;BR /&gt;x=$!&lt;BR /&gt;func2 &amp;amp;&lt;BR /&gt;y=$!&lt;BR /&gt;&lt;BR /&gt;wait x&lt;BR /&gt;wait y&lt;BR /&gt;# end of script&lt;BR /&gt;&lt;BR /&gt;--------------------------&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Duncan</description>
      <pubDate>Sat, 09 Feb 2008 15:55:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/chnage-process-name-0-in-a-shell-script/m-p/4142657#M93362</guid>
      <dc:creator>Duncan Edmonstone</dc:creator>
      <dc:date>2008-02-09T15:55:54Z</dc:date>
    </item>
    <item>
      <title>Re: Chnage process name ($0) in a shell script?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/chnage-process-name-0-in-a-shell-script/m-p/4142658#M93363</link>
      <description>&lt;!--!*#--&gt;I would suggest to use a command line argument to show the sub-function.&lt;BR /&gt;Something like:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;# shell script with background functions&lt;BR /&gt;print "$$ 0=$0 1=$1"&lt;BR /&gt;case $1 in&lt;BR /&gt;func1)&lt;BR /&gt;  sleep 10&lt;BR /&gt;;;&lt;BR /&gt;func2)&lt;BR /&gt;  sleep 20&lt;BR /&gt;;;&lt;BR /&gt;*)&lt;BR /&gt;  $0 func1 &amp;amp;&lt;BR /&gt;  x=$!&lt;BR /&gt;  $0 func2 &amp;amp;&lt;BR /&gt;  y=$!&lt;BR /&gt;  UNIX95= ps -u hein -o ppid,pid,args&lt;BR /&gt;  wait $x&lt;BR /&gt;  wait $y&lt;BR /&gt;  print "All done"&lt;BR /&gt;;;&lt;BR /&gt;esac&lt;BR /&gt;# end of (all) scripts&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sat, 09 Feb 2008 16:51:19 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/chnage-process-name-0-in-a-shell-script/m-p/4142658#M93363</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2008-02-09T16:51:19Z</dc:date>
    </item>
    <item>
      <title>Re: Change process name ($0) in a shell script?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/chnage-process-name-0-in-a-shell-script/m-p/4142659#M93364</link>
      <description>&lt;P&gt;I doubt there is any way. Why do you care?&lt;BR /&gt;&lt;BR /&gt;&amp;gt;Is there any way I can assign a different name&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;(Hmm, Hein had the same idea.)&lt;/P&gt;</description>
      <pubDate>Sat, 17 Sep 2011 21:48:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/chnage-process-name-0-in-a-shell-script/m-p/4142659#M93364</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2011-09-17T21:48:07Z</dc:date>
    </item>
    <item>
      <title>Re: Chnage process name ($0) in a shell script?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/chnage-process-name-0-in-a-shell-script/m-p/4142660#M93365</link>
      <description>Hein,&lt;BR /&gt;&lt;BR /&gt;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!&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Dennis,&lt;BR /&gt;&lt;BR /&gt;I only care cos I'm lazy and want an easy way of doing this.&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;&lt;BR /&gt;Duncan</description>
      <pubDate>Sun, 10 Feb 2008 08:58:55 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/chnage-process-name-0-in-a-shell-script/m-p/4142660#M93365</guid>
      <dc:creator>Duncan Edmonstone</dc:creator>
      <dc:date>2008-02-10T08:58:55Z</dc:date>
    </item>
    <item>
      <title>Re: Chnage process name ($0) in a shell script?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/chnage-process-name-0-in-a-shell-script/m-p/4142661#M93366</link>
      <description>&lt;!--!*#--&gt;&amp;gt; Thanks that works for my example in that you can differentiate between the parent/child scripts&lt;BR /&gt;&lt;BR /&gt;It tends to work nicely allowing one to have just one larger script for a bunch of related but independent task.&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;&amp;gt;&amp;gt;  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!&lt;BR /&gt;&lt;BR /&gt;My example shows the case doing the work, and then joining a main tail code.&lt;BR /&gt;The case could be a straight forward call to original subroutines + exit, making for a quick straightforward (scriptable!) edit.&lt;BR /&gt;&lt;BR /&gt;&amp;gt;&amp;gt; I only care cos I'm lazy and want an easy way of doing this.&lt;BR /&gt;&lt;BR /&gt;Lazy is good!&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;That line should start with #magic.&lt;BR /&gt;Works on example script &lt;BR /&gt;But is probably an excercise in futility as I did not bother with function arguments.&lt;BR /&gt;Anyway, silly script below.&lt;BR /&gt;&lt;BR /&gt;Hein.&lt;BR /&gt;&lt;BR /&gt;Example usage:&lt;BR /&gt;&lt;BR /&gt;#perl in_and_out_burger.pl old &amp;gt; new&lt;BR /&gt;&lt;BR /&gt;Source:&lt;BR /&gt;&lt;BR /&gt;$ cat in_and_out_burger.pl&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;my $tagline = '#magic';&lt;BR /&gt;my ($i, $magic, $function, %defined_functions, %detached_functions);&lt;BR /&gt;my @script = &amp;lt;&amp;gt;;&lt;BR /&gt;my $lines = @script - 1;&lt;BR /&gt;for $i ( 0 .. $lines ) {&lt;BR /&gt;  $_ = $script[$i];&lt;BR /&gt;  if ($magic) {&lt;BR /&gt;    if (/^s*(\w+).*?&amp;amp;$/) {&lt;BR /&gt;      if ($defined_functions{$1}) {&lt;BR /&gt;        $detached_functions{$1}=1;&lt;BR /&gt;        $script[$i] = '$0 ' . $_ ;&lt;BR /&gt;      }&lt;BR /&gt;    }&lt;BR /&gt;  } else {&lt;BR /&gt;    if (/^$tagline/) {&lt;BR /&gt;      die "Been there done that." if /done/;&lt;BR /&gt;      $script[$i] =~ s/$/ done./;&lt;BR /&gt;      $magic = $i;&lt;BR /&gt;    }&lt;BR /&gt;    $defined_functions{$1}=1 if /^\s*function\s+(\w+)/;&lt;BR /&gt;  }&lt;BR /&gt;}&lt;BR /&gt;die "No Magic dividing line found" unless $magic;&lt;BR /&gt;print $script[$_] for (0..$magic);&lt;BR /&gt;$magic++;&lt;BR /&gt;print "\ncase \$1 in\n";&lt;BR /&gt;foreach $function (sort keys %detached_functions) {&lt;BR /&gt;  print "$function)\n  $function\n  exit\n;;\n";&lt;BR /&gt;}&lt;BR /&gt;print "*)\n;;\nesac\n";&lt;BR /&gt;print $script[$_] for ($magic..$lines);&lt;BR /&gt;</description>
      <pubDate>Sun, 10 Feb 2008 14:47:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/chnage-process-name-0-in-a-shell-script/m-p/4142661#M93366</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2008-02-10T14:47:38Z</dc:date>
    </item>
  </channel>
</rss>

