断网前如何记录网络流量?

How to record net traffic before net closed when to poweroff?

我想在debian8上每次记录网络流量。
这是我的方式。

sudo vim /etc/systemd/system/graphical.target.wants/Ktraffic.service

[Unit]
Description=Record net traffic

[Service]
Type=oneshot
ExecStart=/bin/bash   /etc/init.d/K01trafficLog.sh

[Install]
WantedBy=poweroff.target

sudo vim  /etc/init.d/K011trafficLog.sh
#!/bin/bash
trafficlog="/var/log/traffic.log"
echo  `date "+%Y-%m-%d %H:%M:%S  "` >> $trafficlog
iptables  -v -L INPUT |grep Chain   >> $trafficlog 

systemctl enable ktraffic.service

重启时检查流量日志文件。

sudo cat /var/log/traffic.log
2017-02-05 10:49:31Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
2017-02-05 11:40:25Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

/var/log/traffic.log.
中没有记录数据包数量 /etc/init.d/K011trafficLog.sh 执行前网络连接关闭,如何让它在网络连接关闭前执行?

哪个服务在关机期间关闭了网络连接?

systemctl |grep net - vim
(standard input):sys-devices-pci0000:00-0000:00:1c.2-0000:03:00.0-net-eth0.device                         loaded active plugged   RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller
(standard input):sys-subsystem-net-devices-eth0.device                                                    loaded active plugged   RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller
(standard input):netfilter-persistent.service                                                             loaded active exited    netfilter persistent configuration
(standard input):network-online.target                                                                    loaded active active    Network is Online
(standard input):network.target                                                                           loaded active active    Network
vim:sys-devices-pci0000:00-0000:00:1c.2-0000:03:00.0-net-eth0.device                         loaded active plugged   RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller
vim:sys-subsystem-net-devices-eth0.device                                                    loaded active plugged   RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller
vim:netfilter-persistent.service                                                             loaded active exited    netfilter persistent configuration
vim:network-online.target                                                                    loaded active active    Network is Online
vim:network.target                                                                           loaded active active    Network

network.service是否在关机时关闭我的网络?

sudo vim /etc/systemd/system/graphical.target.wants/Ktraffic.service

[Unit]
Description=Record net traffic
Before=networking.service

[Service]
Type=oneshot
ExecStart=/bin/bash   /etc/init.d/K01trafficLog.sh

[Install]
WantedBy=poweroff.target

重启测试,问题依旧。
我在 /var/log/syslog 中设置了一个标志。

echo  "it is a flag here to get output info of poweroff " | sudo tee -a  /var/log/syslog 

重新启动并获取系统日志如下。

[![enter image description here][1]][1]

哪个服务导致信息开始将硬件时钟同步到系统时钟

感谢 Ferenc Wágner。

solution to Execute simple script before shutdown and reboot

在我的eth0上记录网络流量是正确的格式。

[Unit]
Description=Record net traffic
Before=shutdown.target reboot.target halt.target poweroff.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/bash
ExecStop=/bin/bash   /etc/init.d/K01trafficLog.sh

[Install]
WantedBy=poweroff.target shutdown.target reboot.target halt.target