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.

  • 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 2: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 2:13

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.

  • How do you undo bash -c "exec -a MyUniqueProcessName <command> &" ? – VNVN Nov 23 '16 at 20:22
  • 1
    @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. 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

AMItac / The Solaris Agency

i have this case with an Audio transcoding tool. ( running 3 times ) but has only ONE Name.

I did following. Wen´t to the bin dir from the Shell tool and copyed it 3 times and gave it then a new Name. tc-1 , tc-2 , tc-3 ( it´s a small tool so it don´t eat much Harddisc Space ( with fat binaries you shouldn´t use the other mentioned ways )

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.