How to change argv0 in bash so command shows up with different name in ps?

萝らか妹 提交于 2019-11-28 06:51:18

You can do it when running a new program via exec -a <newname>.

I've had a chance to go through the source for bash and it does not look like there is any support for writing to argv[0].

Just for the record, even though it does not exactly answer the original poster's question, this is something trivial to do with zsh:

ARGV0=emacs nethack
( exec -a foo bash -c 'echo $0' ) 

I'm assuming you've got a shell script that you wish to execute such that the script process itself has a new argv[0]. For example (I've only tested this in bash, so i'm using that, but this may work elsewhere).

#!/bin/bash

echo "process $$ here, first arg was $1"
ps -p $$

The output will be something like this:

$ ./script arg1
process 70637 here, first arg was arg1
  PID TTY           TIME CMD
70637 ttys003    0:00.00 /bin/bash ./script arg1

So ps shows the shell, /bin/bash in this case. Now try your interactive shell's exec -a, but in a subshell so you don't blow away the interactive shell:

$ (exec -a MyScript ./script arg1)
process 70936 here, first arg was arg1
  PID TTY           TIME CMD
70936 ttys008    0:00.00 /bin/bash /path/to/script arg1

Woops, still showing /bin/bash. what happened? The exec -a probably did set argv[0], but then a new instance of bash started because the operating system read #!/bin/bash at the top of your script. Ok, what if we perform the exec'ing inside the script somehow? First, we need some way of detecting whether this is the "first" execution of the script, or the second, execed instance, otherwise the second instance will exec again, and on and on in an infinite loop. Next, we need the executable to not be a file with a #!/bin/bash line at the top, to prevent the OS from changing our desired argv[0]. Here's my attempt:

$ cat ./script
#!/bin/bash

__second_instance="__second_instance_$$"
[[ -z ${!__second_instance} ]] && {
  declare -x "__second_instance_$$=true"
  exec -a MyScript "$SHELL" "$0" "$@"
}

echo "process $$ here, first arg was $1"
ps -p $$

Thanks to this answer, I first test for the environment variable __second_instance_$$, based on the PID (which does not change through exec) so that it won't collide with other scripts using this technique. If it's empty, I assume this is the first instance, and I export that environment variable, then exec. But, importantly, I do not exec this script, but I exec the shell binary directly, with this script ($0) as an argument, passing along all the other arguments as well ($@). The environment variable is a bit of a hack.

Now the output is this:

$ ./script arg1
process 71143 here, first arg was arg1
  PID TTY           TIME CMD
71143 ttys008    0:00.01 MyScript ./script arg1

That's almost there. The argv[0] is MyScript like I want, but there's that extra arg ./script in there which is a consequence of executing the shell directly (rather than via the OS's #! processing). Unfortunately, I don't know how to get any better than this.

Update for Bash 5.0

Looks like Bash 5.0 adds support for writing to special variable BASH_ARGV0, so this should become far simpler to accomplish.

(see release announcement)

frankc

I will just add that this must be possible at runtime, at least in some environments. Assigning $0 in perl on linux does change what shows up in ps. I do not know how that is implemented, however. If I can find out, i'll update this.

edit: Based on how perl does it, it is non-trivial. I doubt there is any bask built in way at runtime but don't know for sure. You can see how perl does sets the process name at runtime.

Copy the bash executable to a different name.

You can do this in the script itself...

cp /bin/bash ./new-name
PATH=$PATH:.
exec new-name $0

If you are trying to pretend you are not a shell script you can rename the script itself to something cool or even " " (a single space) so

exec new-name " "

Will execute bash your script and appears in the ps list as just new-name.

OK so calling a script " " is a very bad idea :)

Basically, to change the name

bash script

rename bash and rename the script.

If you are worried, as Mr McDoom. apparently is, about copying a binary to a new name (which is entirely safe) you could also create a symlink

ln -s /bin/bash ./MyFunkyName
./MyFunkyName

This way, the symlink is what appears in the ps list. (again use PATH=$PATH:. if you dont want the ./)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!