Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've setup a linux (CentOS) service to run a script that runs in the backgroud (daemon /home/user/ &) or in the context of the start and stop service:

# Source function library.
. /etc/init.d/functions


start() {
        [ "$EUID" != "0" ] && exit 4

        # Start daemons.
        echo -n $"Starting $prog: "
        daemon /home/user/ &
        #echo "$PID1"

        [ $RETVAL -eq 0 ] && touch $lockfile
        return $RETVAL

stop() {
        [ "$EUID" != "0" ] && exit 4
        echo -n $"Shutting down $prog: "
        killproc $prog
        kill $PID1
        [ $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 (, 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 But the PID seems to just be the service ftpmonitor. So obviously my stop service command with kill $PID1 will not work.

EDIT: script


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 '"')
    if sudo ls "$home$filename" &> /dev/null; then
      # do something with $filename
        echo "$home$filename is the file to be proccesed"
share|improve this question
up vote 2 down vote accepted

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

  • killall

  • kill $(pgrep

  • pkill

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

You must log in to answer this question.

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