0

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
8
  • Can you see your service mentioned in /etc/init.d/.depend.*? Are you running SysV init or systemd? Commented Sep 8, 2015 at 7:07
  • Yes, it appears in /etc/init.d/.depend.stop.What do you mean with which I'm running? Commented Sep 8, 2015 at 10:08
  • Then your script seems to be installed correctly. Does it work if you invoke it (as root) via service YOURSERVICENAME stop? If yes, put in a sleep 60 command for example, that gives you a noticeable delay during shutdown if executed. Pay attention to use stop, that's the only argument used on runlevels 0 and 6 (see /usr/share/doc/sysv-rc/README.runlevels.gz). If you run systemd as PID 1 (see ps 1), there are other options, but the above should work as well. Commented Sep 9, 2015 at 7:10
  • service SERVICENAME stop doesn't work, but /etc/init.d/SERVICENAME stop does.Yes, it seems I'm running systemd, PID 1 is init -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, doing reboot, 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. Commented Sep 9, 2015 at 8:44
  • I've managed to get it to work with systemd,but it seems that the net statistics are wiped before the script is run.Is there anything I can do about that? Commented Sep 9, 2015 at 12:12

2 Answers 2

3

This works for me as /etc/systemd/system/netstat.service:

[Unit]
Description=Save interface stats on shutdown

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/true
ExecStop=/bin/sh -c '{ date; ip -s link; } >>/root/ipstat.log'

[Install]
WantedBy=multi-user.target

Enable it with systemctl enable netstat. This won't give you precise statistics, because the network interfaces can still be used during shutdown, but that may be acceptable for you.

7
  • It seems it didn't work.The service was started, I don't know if at shutdown or at boot: sep 11 10:56:42 Otroservidor systemd[1]: Starting Save interface stats on shutdown... sep 11 10:56:42 Otroservidor systemd[1]: Started Save interface stats on shutdown. Commented Sep 11, 2015 at 8:59
  • This service should be started during bootup. But this is a purely administrative step, only to have something around to stop during shutdown. Commented Sep 11, 2015 at 12:09
  • It finally worked.Thank you very much.Could you briefly comment how/why it works? I swear I ended up trying something very similar (I don't know anything about systemd scripts), maybe save for the Before=... Commented Sep 11, 2015 at 13:33
  • This defines a normal service (DefaultDependencies=yes), which is started after system initialization and stopped prior to system shutdown (see man systemd.service for details). It does nothing but registering on start, and does its work on stop. The Before setting did not help you, because you started your service during system startup (WantedBy=multi-user.target), not shutdown (which would be another, different way to solve your problem). Commented Sep 12, 2015 at 6:25
  • there is a typo ,not ExecStart=/bin/true,but ExecStart=/bin/bash
    – showkey
    Commented Feb 5, 2017 at 13:03
0

Well, I use an ugly hack by calling my script from /etc/rc.d/init.d/network. This will give you statistics whenever network goes up/down/restart or system is halted/shutdown.

Don't really know if this is a good practice or not does work though.

6
  • /etc/rc.d/init.d/network doesn't exist in my system.Maybe it's specific to some distro? Commented Sep 11, 2015 at 8:46
  • paste an "ls" of your rc6.d here. you must have a script that deals with network connections.
    – sibzz
    Commented Sep 11, 2015 at 10:29
  • on debian7 i have this file K08networking in rc6.d which is symlinked to ../init.d/networking. its behavior is same. my script runs successfully. Oh and the correct path for this deb7 is /etc/rc6.d/ /etc/init.d/
    – sibzz
    Commented Sep 11, 2015 at 10:45
  • Ok /etc/rc6.d/K08networking exists here too.Where am I supposed to call the script from in the file? It looks kind of complicated. Commented Sep 11, 2015 at 10:55
  • add it after the first line that says #!/bin/bash -e .
    – sibzz
    Commented Sep 11, 2015 at 11:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .