Matthew Ames

Systemd

Matthew Ames - 2016-03-01 - 8:50 am

Simple oneshot script to be triggered on occasion

unit file

[Unit]
Description=TCPDump Service
After=network.target

[Service]
User=root
ExecStart=/bin/bash /root/tcpdumpscript/script.sh
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
#Restart=on-failure
Type=oneshot
#PIDFile=/var/run/tcpdump.pid
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

script file

#! /bin/bash
LOGFILE="tcpdump.log"
LOGDIR="/tcpdumplogs/"
PIDFILE="/var/run/tcpdump.pid"
if [ -f $PIDFILE ]; then
PID=$(cat $PIDFILE)
kill ${PID}
fi

if [ -f $LOGDIR$LOGFILE ]; then
cd $LOGDIR
DATE=”$LOGFILE-$(date +’%Y-%m-%d_%H-%M-%S’)”
mv $LOGFILE $DATE
gzip $DATE
fi

tcpdump -w $LOGDIR$LOGFILE port 5672 &
echo $! > /var/run/tcpdump.pid
exit 0

0 Comments