Saturday, June 13, 2009

New process group in shell script

To create a new process group for a command (or another shell script) in bash script, the following method can be used.

 b=$(bash -i -c "<command> & echo \$!") 

This can be put in a shell script. String after -c will be executed by the (sub-)bash, while -i means the (sub-)bash will behave like a interactive bash. So "<command> &" in string will execute <command> in a new process group, just like "<command> &" is typed in a login shell. The "echo \$!" in string will print the process ID, that is, the new process group ID, so you can operate on the new created process group, such as send signal to process group via "kill -SIGNAL -$b".

No comments: