I've setup a linux (CentOS) service to run a script that runs in the backgroud (daemon /home/user/testftp.sh &
) or in the context of the start and stop service:
# Source function library.
. /etc/init.d/functions
prog=ftpmonitor
lockfile=/var/lock/subsys/ftpmonitor
start() {
[ "$EUID" != "0" ] && exit 4
# Start daemons.
echo -n $"Starting $prog: "
daemon /home/user/testftp.sh &
PID1=$!
#echo "$PID1"
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $lockfile
return $RETVAL
}
stop() {
[ "$EUID" != "0" ] && exit 4
echo -n $"Shutting down $prog: "
killproc $prog
kill $PID1
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $lockfile
return $RETVAL
}
My problem is stopping all the processes. killproc $prog
will stop the service but the background process is still running (testftp.sh
), which needs to be run in the backgroud to monitor ftp logs).
I've seen similar posts that say to use PID1=$!
which would represent the PID for testftp.sh
. But the PID seems to just be the service ftpmonitor
. So obviously my stop service command with kill $PID1
will not work.
EDIT:
testftp.sh script
#!/bin/sh
sudo tail -F /var/log/vsftpd.log | while read line; do
if sudo echo "$line" | grep -q 'OK UPLOAD:'; then
username=$(echo "$line" | cut -d" " -f8 | sed 's/\[\(.*\)\]/\1/')
filename=$(echo "$line" | cut -d, -f2 | sed 's/^[ \t]*//' | tr -d '"')
home="/home/vsftpd/$username"
if sudo ls "$home$filename" &> /dev/null; then
# do something with $filename
echo "$home$filename is the file to be proccesed"
fi
fi
done