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

后端 未结 7 1346
悲&欢浪女
悲&欢浪女 2020-12-08 10:15

In a C program I can write argv[0] and the new name shows up in a ps listing.

How can I do this in bash?

相关标签:
7条回答
  • 2020-12-08 10:50

    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
    
    0 讨论(0)
  • 2020-12-08 10:57

    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 ./)

    0 讨论(0)
  • 2020-12-08 11:01

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

    0 讨论(0)
  • 2020-12-08 11:05

    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.

    0 讨论(0)
  • 2020-12-08 11:07
    ( exec -a foo bash -c 'echo $0' ) 
    
    0 讨论(0)
  • 2020-12-08 11:09

    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].

    0 讨论(0)
提交回复
热议问题