Update process name in shell, is it possible?
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
Related videos on Youtube
Jerry Epas
Updated on September 18, 2022Comments
-
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 fileThis_is_file_nr12456.txt
, it can "publish" this by changingargv[1]
making it instantly available to user by simpleps ax
.-
mikeserv over 9 yearswith
zsh
you can change$0
- but I dunno about how that is reflected in aps
- let alone the positionals. You can change those though at any time withset -- one two etc
. And If you want to ensure that gets reflected in aps
you might do:exec "$0" -- new positionals here.
-
text over 9 yearsWhy not use an intermediate tmp file?
-
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 over 9 yearsYou might want to make sure that the new name you copy to argv[0] is as long or shorter than the original.
-
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 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 over 9 years@JerryEpas, on Linux at least it doesn't. There,
zsh -c 'jobs -Z foo; ps -p "$$"; :'
still showszsh
whilezsh -c 'jobs -Z foo; ps -fp "$$"; :'
showsfoo
. On Linux, you needprctl(PR_SET_NAME)
to change the process name whichzsh
doesn't do. -
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 over 9 years@JerryEpas, yes totally agreed, that was just a nitpick about process name not being completely accurate.
-
Marco Lavagnino over 6 yearsI get
printf: write error: invalid argument
-
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!