15

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.

2
  • 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? 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
14

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.

4
  • 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. 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. Dec 12 '17 at 13:47
  • It doesn't work for meld. -> bash -c "exec -a mymeld123 meld &" Apr 21 '20 at 17:56
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.

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.