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

后端 未结 7 1381
悲&欢浪女
悲&欢浪女 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: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 ./)

提交回复
热议问题