unix - starting a new process group from bash script -


i want run script (which calls more scripts) in new process group can send signal processes called script.

in linux, found out setsid helps me in doing that, not available on freebsd.

syntax setsid (provided util-linux-ng).

setsid /path/to/myscript 

i, learnt session , process group not same. starting new session solves problem.

sessions , groups not same thing. let's make things clean:

a session consists of 1 or more process groups, , can have controlling terminal. when session has controlling terminal, session has, @ moment, 1 foreground process group , 1 or more background process groups. in such scenario, terminal-generated signals , input seen every process in foreground process group.

also, when session has controlling terminal, shell process session leader, dictating process group foreground process group (implicitly making other groups background process groups). processes in group put there linear pipeline. example, ls -l | grep | sort typically create new process group ls, grep , sort live.

shells support job control (which requires support kernel , terminal driver), in case of bash, create new process group each command invoked -- , if invoke run in background (with & notation), process group not given control of terminal, , shell makes background process group (and foreground process group remains shell).

so, can see, don't want create session in case. typical situation you'd want create session if daemonizing process, other that, there not use in creating new session.

you can run script background job, mentioned, create new process group. since fork() inherits process group id, every process executed script in same group. example, consider simple script:

#!/bin/bash  ps -o pid,ppid,pgid,comm | grep ".*" 

this prints like:

  pid  ppid  pgid command 11888 11885 11888 bash 12343 11888 12343 execute.sh 12344 12343 12343 ps 12345 12343 12343 grep 

as can see, execute.sh, ps , grep on same process group (the value in pgid).

so want is:

/path/to/myscript & 

then can check process group id of myscript ps -o pid,ppid,pgid,comm | grep myscript. send signal group, send group leader (pgid pid of leader of group). signal sent group delivered every process in group.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -