On Fri, 27 Dec 2019 12:32:27 +0000
"Laurent Bercot" <ska-supervision_at_skarnet.org> wrote:
> As for cgroups-related chainloaders, I could probably write some in
> s6-linux-utils, but wasn't the cgroups interface designed to make
> sure those operations are trivial to implement as small scripts?
I changed in the past sysv init scripts in exactly that way, that they
created a cgroup first at start and killed all processes within that
cgroup at the end.
That where 5? lines of shell. You could provide that system wide, if you
would offer some include mechanism (pre-run, post-stop) or within the
run/finish scripts just include a shell library.
In the first place it would be ok, to have the name of the service
available as an identifier for the cgroup. A random name would only
work if you persist it somewhere and have to manage the clean-up - I
would prefer to use the directory's name of the run script. Is it
available through a environment?
Something like:
##############
run
##############
CGROUP_BASE_DIR=/sys/fs/cgroup/freezer
cgroup=$run_dir
mkdir $CGROUP_BASE_DIR/$cgroup
echo $$ > $CGROUP_BASE_DIR/$cgroup/tasks
# exec or include
exec do_the_real_stuff
##############
finish
##############
CGROUP_BASE_DIR=/sys/fs/cgroup/freezer
cgroup=$run_dir
state_file=$CGROUP_BASE_DIR/$cgroup/freezer.state
echo FROZEN > $state_file
i=0
while ! grep FROZEN $state_file > /dev/null; do
let i=$i+1
sleep 0.1
if [ $i -gt 100 ]; then
break
fi
done
kill -9 $(cat $CGROUP_BASE_DIR/$cgroup/tasks)
########################
Disclaimer: this has race-conditions by design. systemd has them as
well. No, they don't say that of course. You can't read the tasks
atomically and change their state to stopped, freeze or whatever. So
they always could fork away.
What you can do is repeat the killing/freezing/stopping until it
succeeds (mabye never).
Best Regards
Oli
--
Automatic-Server AG •••••
Oliver Schad
Geschäftsführer
Turnerstrasse 2
9000 St. Gallen | Schweiz
www.automatic-server.com | oliver.schad_at_automatic-server.com
Tel: +41 71 511 31 11 | Mobile: +41 76 330 03 47
Received on Fri Dec 27 2019 - 23:54:07 UTC