Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
share|improve this question

1 Answer 1

up vote 2 down vote accepted

You should be able to kill it using one or all of these:

  • killall testftp.sh

  • kill $(pgrep testftp.sh)

  • pkill testftp.sh

If your script is launching other processes and you want to kill it and all its children, use pkill -P:

-P ppid,... 
    Only match processes whose parent process ID is listed.

So, instead of kill $PID1, use

pkill -P $PID1

Alternatively, you could try

kill -- -$(ps opgid= $PID)

This question on SO has some nice pointers.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.