Update process name in shell, is it possible?

8,498

Solution 1

zsh's jobs builtin can change the shell's process name. jobs -Z newname

Solution 2

With recent Linux, printf foo > /proc/$$/comm will change the executable name (the ps -p thing) provided "noclobber" isn't set (and the wind is in the right direction).

In zsh, printf foo >! /proc/$$/comm works regardless of clobbering state.

Solution 3

You could make your script recursive this way:

#! /bin/sh -
do-something-with "$1"
shift
[ "$#" -eq 0 ] || exec "$0" "$@"

Then when running your-script a b c, the ps output would show in turn:

your-script a b c
your-script a b
your-script a
Share:
8,498

Related videos on Youtube

Jerry Epas
Author by

Jerry Epas

Updated on September 18, 2022

Comments

  • Jerry Epas
    Jerry Epas over 1 year

    In C/C++ it is in general possible to do strcpy(argv[0], "new process name"). It's a hack used in malicous software to hide real process name. Is such operation possible via shell?

    What I want to do is: change $1, $2, ... so that the script can make certain information easily available to user. For example, if the script is processing file This_is_file_nr12456.txt, it can "publish" this by changing argv[1] making it instantly available to user by simple ps ax.

    • mikeserv
      mikeserv over 9 years
      with zsh you can change $0 - but I dunno about how that is reflected in a ps - let alone the positionals. You can change those though at any time with set -- one two etc. And If you want to ensure that gets reflected in a ps you might do: exec "$0" -- new positionals here.
    • text
      text over 9 years
      Why not use an intermediate tmp file?
    • Jerry Epas
      Jerry Epas over 9 years
      @mdpc: if multiple such processes are running multiple tmp files are needed. And in case of restart they will not be cleaned automatically, unless I'll change system startup to discover the files (either specific dir or file pattern). I know it's doable but the script already ustilizes specific directories on Dropbox, log files, cron entry, secondary script, so the ps way would be best. I would rather write zsh module for this than create the additional sparse (script, separate of tmp files, separate of system startup for cleaning, plus the possibility of crash/kill) layer to the script.
    • Jpark822
      Jpark822 over 9 years
      You might want to make sure that the new name you copy to argv[0] is as long or shorter than the original.
    • Jerry Epas
      Jerry Epas over 9 years
      @Hennes: yes but the shell could even reallocate argv's storage (not sure if that's possible on the os level)
  • Jerry Epas
    Jerry Epas over 9 years
    @StéphaneChazelas: it does change ps listing, as it writes to argv table. It is good to check jobs.c file in zsh source. The thing is a quite of a hack, and before using it, one basically is required to first run zsh with long parameter list, to reserve enough space for argv, to then be able to assign strings of that length.
  • Stéphane Chazelas
    Stéphane Chazelas over 9 years
    @JerryEpas, on Linux at least it doesn't. There, zsh -c 'jobs -Z foo; ps -p "$$"; :' still shows zsh while zsh -c 'jobs -Z foo; ps -fp "$$"; :' shows foo. On Linux, you need prctl(PR_SET_NAME) to change the process name which zsh doesn't do.
  • Jerry Epas
    Jerry Epas over 9 years
    @StéphaneChazelas: it still allows to indicate script's state via ps, so it's a good solution.
  • Stéphane Chazelas
    Stéphane Chazelas over 9 years
    @JerryEpas, yes totally agreed, that was just a nitpick about process name not being completely accurate.
  • Marco Lavagnino
    Marco Lavagnino over 6 years
    I get printf: write error: invalid argument
  • Yunus
    Yunus about 2 years
    @MarcoLavagnino i think only the process itself that can rename itself , you got this error cuz you wanted to rename a process by its id, try it in a terminal using $$ and it will work it will rename the curent process!