How to set the process name of a shell script?

nohup set process name
macos change process name
python process name linux
create dummy process linux
perl change process name
bash exec
how to get process id in unix shell script
how to find process id in unix

Is there any way to set the process name of a shell script? This is needed for killing this script with the killall command.

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/bash
script="./dummy"
newname="./killme"

rm -iv "$newname"

ln -s "$script" "$newname"

exec "$newname" "$@"

dummy

#!/bin/bash
echo "I am $0"
echo "my params: $@"

ps aux | grep bash

echo "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

kill - How can I start a process with a different name?, add a #bash script Name.sh, make it executable. Type in your commands there and start the bash script itself. On Centos it uses then the Bashscript Name you excecuted and not the bin Name itself. Open your favorite editor and write a shell script file named as my_script.sh containing following lines #!/bin/bash echo "hello world" //print to screen The first line called a hashbang or

Update process name in shell, is it possible?, zsh 's jobs builtin can change the shell's process name. jobs -Z newname. So both our variables are giving us the script name of the executed script. Get script path in shell script. Now there can be two posible situation where the script is located in some directory under a symlink or it is located under physical path. Now for both situation the variable will vary . Get script path under symlink

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 $!

Chnage process name ($0) in a shell script?, Re: Change process name ($0) in a shell script? I doubt there is any way. Why do you care? >Is there any way I can  2 Answers 2. You can use the exec shell builtin: <command> replaces the current shell, no new process is created, that's why I'm starting a new shell to call exec. Then you can kill the process with: You can start more than one process under the same name, then pkill -f <name> will kill all of them. @VNVN you can't.

On Linux at least, killall dvb works even though dvb is a shell script labelled with #!. The only trick is to make the script executable and invoke it by name, e.g.,

dvb watch abc write game7 from 9pm for 3:30

Running ps shows a process named

/usr/bin/lua5.1 dvb watch ...

but killall dvb takes it down.

Running a program with a custom process name in Linux, The traditional Unix solution for this is to store the PID in a file with a known name in a known location, then use the contents of that file when  strictly speaking, it doesn't change the process name (as shown by ps) but the arg list (as shown by ps -f) or as the zsh documentation more precisely puts it: The -Z option replaces the shell's argument and environment space with the given string, truncated if necessary to fit. (it's probably what's the OP's after though).

%1, %2... also do an adequate job:

#!/bin/bash
# set -ex

sleep 101 &
FIRSTPID=$!
sleep 102 &
SECONDPID=$!

echo $(ps ax|grep "^\(${FIRSTPID}\|${SECONDPID}\) ")
kill %2
echo $(ps ax|grep "^\(${FIRSTPID}\|${SECONDPID}\) ")
sleep 1
kill %1
echo $(ps ax|grep "^\(${FIRSTPID}\|${SECONDPID}\) ")

Giving a process a specific name in GNU/Linux?, Update again: You could also do some shell trickery to get the PID of the servers to a file based upon startup name -- then we are talking about shell scripting which is If you really want to change the name, the best way is to use a simple​  I need to figure out how to write a shell script to kill a process by name as given to the script as an argument. I've got that part working OK, but i need to make sure that the script does not allow processes that are owned by root to be killed.

ProcessManagement, I'll put a script in crontab, and if it's not running, I'll restart it. The metadata stored by the kernel includes a process "name" and "command line". complex suite of child processes and events, don't try to do it in a shell script. When the script is run, you are in a new shell, another interactive C shell, but the environment variable is set: % teredo % env | grep TEREDO TEREDO_WORMS=ukelele % When you exit from this shell, the original shell takes over:

How to check running process in Linux using command line, root – User name; 1 – PID (Linux process ID); 19:10 – Process start time; /sbin/init splash Unix change the priority of a running process  Surely you can think of something to "grep away" again, as the PID of your script, which should be available via the $$ variable – blue Jun 6 '13 at 14:57. Thanks @blue for answers – Mayank Jain Jun 6 '13 at 15:03. A trick for avoiding the 2nd grep is to give grep a regular expression that matches the same thing, but appears different on

How to Find a Process Name Using PID Number in Linux, Learn more about how to install Glances in Linux systems. Find Out Process PID Number. To find out the PID of a process, you can use pidof , a  I have to run a shell script that stays active for a long, long time. But when I run top, ps, glance, etc I only see "ksh" as the process name. I would prefer seeing something more significant. I'd like to have it renamed. The only way I know how to do this is to start it from a C program with a fudged exec() call. That's not nice.

Comments
  • It's easy to do in zsh: unix.stackexchange.com/a/170322/15513
  • Not true. Try it. Create a file called "tst.sh", make it executable, have it do something like a "sleep 30", and fire it up. You'll note that the process name associated with it is "/bin/sh" or "/bin/bash" or whatever you put in the shebang line.