Visit The New, Modern Unix Linux Community

Top Forums Shell Programming and Scripting A script that kills previous instances of itself upon running not killing child processes Post 302687531 by DeCoTwc on Thursday 16th of August 2012 06:20:36 PM
A script that kills previous instances of itself upon running not killing child processes

I'm likely going to explain this clumsily, so apologies in advance:

I have the following script:

Code:
#!/bin/bash

pidPrefix="logGen"


checkPrime ()
{
  if /sbin/ifconfig eth0:0|/bin/grep -wq inet;then isPrime=1;else isPrime=0;fi
}

killScript ()
{
  /usr/bin/find /var/run -name "${pidPrefix}.*.pid" |while read pidFile;do
    if [[ ! "${pidFile}" =~ "${$}" ]];then
      #/bin/kill $(/usr/bin/tail -1 ${pidFile})
      /bin/kill $(/bin/cat ${pidFile})
      /bin/rm ${pidFile}
    fi
  done
}
echo "$$" > /var/run/${pidPrefix}.$$.pid
killScript
#  echo "$$" > /var/run/${pidPrefix}.$$.pid
   tail -F -n0 /opt/REDACTED/logs/manager.log|while read -a  i;do
    if [[ "${i[2]}" == "E" ]];then
      if [[ "${i[7]:0:1}" == "@" ]];then
        echo "${i[0]}|${i[1]}|${i[6]}|${i[@]:8:${#i[@]}}" >> /tmp/allerrors.${i[0]//\//.}.log
      else
        echo "${i[0]}|${i[1]}|${i[6]}|${i[@]:7:${#i[@]}}" >> /tmp/allerrors.${i[0]//\//.}.log
      fi
    fi
  done

The bulk of it you can ignore, the main points are:
* the function killScript is meant to check for lock files in /var/run (excluding lock files created by the current instance of the script) and kill the pid inside the lock file.
* a tail is kicked off against a log file, and the output parsed into a separate logfile (if anyone actually cares, I can show sample input for the script, and explain what everything does, but it's not really relevant to my problem).
* The goal is that this script is run periodically on cron. Every time it's run, the last running instance of the script is killed, and a new one is started


So here's the issue. The script starts a child process, and just killing the first processes pid doesn't kill the child process. It just keeps running and running until manually killed. As seen below:
*
Code:
[root@liwmgmt02 utils]# bash new.logGen.sh #kick of an instance of the script

*
Code:
[root@liwmgmt02 utils]# ps -ef|grep [n]ew.log #in a separate terminal check pids
  root 11391 7109 0 16:51 pts/1 00:00:00 bash new.logGen.sh
  root 11395 11391 0 16:51 pts/1 00:00:00 bash new.logGen.sh

* So now the script is running, and there are two pids
Code:
[root@liwmgmt02 utils]# bash new.logGen.sh #in separate terminal start a   second instance

* In the first terminal the script exited back to a prompt and says Terminated
*
Code:
[root@liwmgmt02 utils]# ps -ef|grep [n]ew.log
  root 11395 1 0 16:51 pts/1 00:00:00 bash new.logGen.sh
  root 14623 10569 0 16:52 pts/3 00:00:00 bash new.logGen.sh
  root 14630 14623 0 16:52 pts/3 00:00:00 bash new.logGen.sh

* and now I have the child process of the script still running and doing stuff.


So yeah, this is my problem. How can I accomplish my goal of making a script that can kill previous instances of itself when it's run if the script spawns child processes?

Also, this script is far from done. There's still error checking to be added, and comments, and general tidying up. But I've reached this point and don't know how to proceed.
 
Test Your Knowledge in Computers #877
Difficulty: Medium
A real-time operating system (RTOS) is an operating system (OS) intended to serve real-time applications that process data as it comes in, typically without buffer delays.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to limit the number of running instances of a script?

I would like to allow only one instance of a script to run at any moment. I've tried the following solution to count the instances but the result is always the number of running instances plus one and I can't find the problem ps -ef | grep $0 | sed '/^$/ d' | sed '/grep/ d' | wc -l Please... (2 Replies)
Discussion started by: oti
2 Replies

2. Shell Programming and Scripting

Script that checks for previous instances running

Hello, I'm trying to write a script that checks for previous instances of the same script which may still be running (this script is scheduled to run every 30 minutes). I want to somehow use the pid from each instance to make sure the previous one isn't running before continuing with my... (5 Replies)
Discussion started by: bd_joy
5 Replies

3. Shell Programming and Scripting

Leaving Program running but killing the script

Sorry for all the threads. I am almost done. I ahve a bash script that is launching a diags program then copying the .html over my client. then it does the following line /opt/firefox/firefox report.html it launches it fines but the program waits for me to close the window or kill the script.... (2 Replies)
Discussion started by: deaconf19
2 Replies

4. UNIX for Advanced & Expert Users

Monitoring Processes - Killing hung processes

Is there a way to monitor certain processes and if they hang too long to kill them, but certain scripts which are expected to take a long time to let them go? Thank you Richard (4 Replies)
Discussion started by: ukndoit
4 Replies

5. UNIX for Advanced & Expert Users

killing all child processes

Hi, Is there a way I can kill all the child processes of a process, given its process id. Many thanks in advance. J. (1 Reply)
Discussion started by: superuser84
1 Replies

6. Shell Programming and Scripting

36 Child Processes not running as desired

I have a parent process which will start 36 child process. This I achieved by using the 'for loop'. In Parent.sh:- ./Child.sh <arg1> <arg2> ... & If I execute "ps -ef | grep Child.sh", I can see 72 child processes running at the background. I mean I can see the duplicate of each process. ... (2 Replies)
Discussion started by: nthiruvenkatam
2 Replies

7. Shell Programming and Scripting

Finding the age of a unix process, killing old processes, killing zombie processes

I had issues with processes locking up. This script checks for processes and kills them if they are older than a certain time. Its uses some functions you'll need to define or remove, like slog() which I use for logging, and is_running() which checks if this script is already running so you can... (0 Replies)
Discussion started by: sukerman
0 Replies

8. Shell Programming and Scripting

Script, child kills parent

Hello everyone, I'm trying to write a script, i would like to say the child kills the parent, how would i do that? (2 Replies)
Discussion started by: jessy21
2 Replies

9. Shell Programming and Scripting

Script counting instances of software running on a machine

Hello to all @here, as Iīm new to this forum, I will try to start in a easy way for my first post. Iīm not beginner in scripting, but also not a proffessional. So please keep easy, if I donīt understand your explanation at once ;) I donīt mean it in a bad way! Here is the Problem: There were... (2 Replies)
Discussion started by: muogli
2 Replies

10. Shell Programming and Scripting

Running a script on remote server kills my login session

Hi there, I'm trying to run a script remotely on a server in a particular directory named after hostname which already exists, my login session gets killed as soon as I run the below command. Not sure what is wrong, is there a better way to do it ? Note: I can also use nohup command to run... (14 Replies)
Discussion started by: mbak
14 Replies

Featured Tech Videos

All times are GMT -4. The time now is 08:32 PM.
Unix & Linux Forums Content Copyright 1993-2020. All Rights Reserved.
Privacy Policy