96 reputation
6
bio website
location
age
visits member for 1 year, 11 months
seen Nov 17 at 3:26

Aug
15
revised What's a reliable technique for killing background processes on script termination?
Added example.
Aug
15
awarded  Editor
Aug
15
revised What's a reliable technique for killing background processes on script termination?
Noted that the script must run as a process group leader for the technique to work.
May
8
awarded  Popular Question
Mar
8
accepted What's a reliable technique for killing background processes on script termination?
Mar
8
answered What's a reliable technique for killing background processes on script termination?
Mar
8
comment What's a reliable technique for killing background processes on script termination?
Actually, I think it's even simpler... the shell calls setpgid() when it starts up to create a new process group, and you can use regular old kill to signal a process group by using a negative number. So... kill -$$ should kill every background task - as long as none of them started their own process group :)
Mar
8
comment What's a reliable technique for killing background processes on script termination?
Thanks for the corrections! I'm not sure what I would gain by using a true subshell - it doesn't get me any closer to the background processes started on the left of the pipe. To be honest my main gripe with ps is the grotesquely bloated util that is GNU ps, which I've never learned to use well for scripting in favour of saner tools. It sounds like a good approach though... For my case, it looks like pgrep -g $$ gives me exactly what I want (PIDs of all processes belonging to the same process group as $$)... I'm just not 100$ sure where the "process group" boundary is.
Mar
3
awarded  Scholar
Feb
17
answered What's a reliable technique for killing background processes on script termination?
Feb
3
comment What's a reliable technique for killing background processes on script termination?
Thanks! The links focus on desktop performance but with a bit more investigation it became obvious how to use cgroups for this case
Jan
31
comment What's a reliable technique for killing background processes on script termination?
Right. The complication in this case the processes in question are launched in a subshell, which is (a) in the background and (b) part of a pipeline. So, it's not trivial to propagate the saved PID variables back to the parent shell (which is the only shell that knows when to terminate). I'm thinking a construct based on "trap EXIT" would allow me to elegantly clean up the background processes launched within the subshell, and then I just need a reliable way to signal termination to the subshell (which may be as simple as kill $!, not sure). Good point about forking processes.
Jan
30
awarded  Student
Jan
30
asked What's a reliable technique for killing background processes on script termination?
Jan
30
answered Shell script that starts a process, starts another process, then kills the first process
Jan
19
comment rsync using regex to include only some files
Ah yeah, you are quite right. It should work on a directory name of any length, but will fail as soon as you refer to a directory outside the current directory (because there will be a different number of slashes in the prefix portion). To fix that, probably easiest to use sed instead of cut, like: sed "s#^$1/*##" buuuut that will break on paths that contain a #. To fix that we have to quote the incoming directory name: prefix=$(echo "$1" | sed 's#/#\\/#g') and then sed "s/^$prefix\\/*//" The subleties of bash quoting are a bit of a nightmare ;)
Jan
16
awarded  Supporter
Jan
16
awarded  Teacher
Jan
15
answered find any script which is running for more than say 30 minutes in Linux System
Jan
15
answered rsync using regex to include only some files