How to set the process name of a shell script? How to set the process name of a shell script? shell shell

How to set the process name of a shell script?


Here's a way to do it, it is a hack/workaround but it works pretty good. Feel free to tweak it to your needs, it certainly needs some checks on the symbolic link creation or using a tmp folder to avoid possible race conditions (if they are problematic in your case).

Demonstration

wrapper

#!/bin/bashscript="./dummy"newname="./killme"rm -iv "$newname"ln -s "$script" "$newname"exec "$newname" "$@"

dummy

#!/bin/bashecho "I am $0"echo "my params: $@"ps aux | grep bashecho "sleeping 10s... Kill me!"sleep 10

Test it using:

chmod +x dummy wrapper./wrapper some params

In another terminal, kill it using:

killall killme

Notes

Make sure you can write in your current folder (current working directory).

If your current command is:

/path/to/file -q --params somefile1 somefile2

Set the script variable in wrapper to /path/to/file (instead of ./dummy) and call wrapper like this:

./wrapper -q --params somefile1 somefile2


You cannot do this reliably and portably, as far as I know. On some flavors of Unix, changing what's in argv[0] will do the job. I don't believe there's a way to do that in most shells, though.

Here are some references on the topic.


You can use the kill command on a PID so what you can do is run something in the background, get its ID and kill it

PID of last job run in background can be obtained using $!.

echo test & echo $!