I need to run a script before shutting down or rebooting my VPS running Debian 8,to keep count of network statistics.I tried adding the script directly to /etc/init.d
and symlinking it in /etc/rc0.d
and /etc/rc6.d
, and adding to it the LSB header, making it like an actual service with start and stop and generating the symlinks with update-rc.d, but nothing has worked, it seems like the script isn't executed at all.Maybe it could have to do with it being an VPS, so it doesn't get recognised by the system when it is apparently shut down or rebooted.
I just need a simple mechanism,without having to create a proper service. Also I'd like to know which could be the better way to check if the script is actually being executed or not,some simple way of logging. The script is just this:
RESULT=$(bc <<< "scale =2;($(cat /sys/class/net/venet0/statistics/rx_bytes)/1024/1024/1024)+($(cat /sys/class/net/venet0/statistics/tx_bytes)/1024/1024/1024)+($(cat /root/bw))")
echo $RESULT > /root/bw
/etc/init.d/.depend.*
? Are you running SysV init or systemd?service YOURSERVICENAME stop
? If yes, put in asleep 60
command for example, that gives you a noticeable delay during shutdown if executed. Pay attention to usestop
, that's the only argument used on runlevels 0 and 6 (see/usr/share/doc/sysv-rc/README.runlevels.gz
). If you runsystemd
as PID 1 (seeps 1
), there are other options, but the above should work as well.service SERVICENAME stop
doesn't work, but/etc/init.d/SERVICENAME stop
does.Yes, it seems I'm running systemd, PID 1 isinit -z
but it is a symlink to/lib/systemd/systemd
.I don't think I would notice the delay, since because of it being a VPS I guess, doingreboot
,init 6
or whatever doesn't output anything and anyway it just stays like that for a while until it says (when I press some key) that the connection was lost.