18

Basically I want to dynamically start some processes which may create their own children processes, also I want to kill a certain group of processes I just created whenever I want.

One way I could think of is to start processes with a name (to distinguish as a group), then use pkill to kill them by the name.

The question is how to start a process with a name so that I can use pkill to kill them by the name? I am open to other solutions as well.

Eric Carvalho
  • 54,385
cache
  • 1,431
  • Are you asking how to start a process with a name different from what it usually uses? Or are you just asking how to start a process from the command-line? – Eliah Kagan Jun 21 '12 at 02:09
  • The process name is shown the same as its file name by default in Linux. I dont want to use its default file name as its process name nor change the file name every time I run. So I want to start the processes with a specific name that I want so that I can use 'pkill -f MyOwnName' to kill all the processes that I just started named MyOwnName at once . – cache Jun 21 '12 at 02:13

3 Answers3

17

You can use the exec shell builtin:

bash -c "exec -a MyUniqueProcessName <command> &"

<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:

pkill -f MyUniqueProcessName

You can start more than one process under the same name, then pkill -f <name> will kill all of them.

Eric Carvalho
  • 54,385
  • 1
    How do you undo bash -c "exec -a MyUniqueProcessName <command> &" ? – VNVN Nov 23 '16 at 20:22
  • 2
    @VNVN you can't. If a command has been run, it has run. To "undo" it, you need to know exactly what it did and manually undo that, if possible. There is not Ctrl+Z when running commands. – terdon Nov 24 '16 at 10:09
  • 1
    +1. my observation is: the name used for exec -a name sleep 100 shows up when i do ps -ef but not when i do pstree -p. – Trevor Boyd Smith Dec 12 '17 at 13:47
  • It doesn't work for meld. -> bash -c "exec -a mymeld123 meld &" – Eduardo Lucio Apr 21 '20 at 17:56
  • @TrevorBoydSmith, it does not show in ps -e, shows in ps -ef. The answer IMO should start with disclaimer that is does not change process name, but still can be used with pkill via adding -f / --full option – Martian2020 Jan 14 '22 at 00:43
1

AMItac / The Solaris Agency

I had an issue with an audio transcoding tool that ran 3 times under the same name.

I did following. Went to the bin directory and copyied the transcoding script 3 times and gave each one a new name. tc-1 , tc-2 , tc-3 (it´s a small tool so it doesn't eat much drive space (with large binaries you shouldn't use this method)

so the process started with an unique Name and can be killed with this unique Name without the danger of killing other transcoding processes i want to continue.

another trick MIGHT work....

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.

Hope something helps someone out there.

Riley
  • 140
  • 9
0

Here is the Python solution I use. Obviously there is a slight startup time and memory overhead, but aside from that it does the job. It uses the setproctitle module, which you can install via python -m pip install setproctitle

#!/usr/bin/env python
'''
example:
launch_with_name HelloWorld sleep 10
'''

from setproctitle import setproctitle # python -m pip install setproctitle from subprocess import run import sys args = sys.argv[1:] if len(args) < 2: print(f"Not enough arguments") print(f"{sys.argv[0]} <process name> <command> [<args> ...]") exit(1) setproctitle(args[0]) cmd = args[1:] exit(run(cmd).returncode)

  • My main use case for the above is not killing processes, but rather being able to wait for a process to finish. So I can start a script using this script so that there is a containing process with a desired name that exits as soon as the script does. – John Allsup Jan 24 '23 at 20:13