Sponsored Content
Top Forums UNIX for Advanced & Expert Users Trapping a kill command sent to a script and using it to send an EOF to a subprocess before killing Post 302880579 by DeCoTwc on Friday 20th of December 2013 04:55:13 PM
Old 12-20-2013
Trapping a kill command sent to a script and using it to send an EOF to a subprocess before killing

I originally had a script written in pure shell that I used to parse logs in real time and create a pipe delimited file that only contained errors. It worked but it was using a lot of memory (still not clear on why). I originally got around this by writing a wrapper for the script that ran on cron and periodically killed the script and started a new instance. This worked for a while, but was still too heavy. So, I rewrote the parsing logic in awk instead of shell and the memory and CPU utilization dropped completely.

So now, I have this script running, but every 15 minutes the cronjob kills it and starts a new instance. What I want to do now is have my awk section of the script building out arrays, and when the cron kicks in, it sends an EOF to the awk so it can dump it's info into a separate file before it's killed and a new instance is started. Below is my code. What I want to do is add blocks to the awk section that creates arrays, and then add an end block to it that dumps the values from those arrays into a file. So, I need to trap the kill command and then have it send an EOF to awk, then kill the script and start a new instance.


Code:
#!/bin/bash
######################################################
# Program:      logGen.sh
# Date Created: 22 Aug 2012
# Description:  parses the manager log in real time into daily error files
# Date Updated: 27 Nov 2013
#		|_moved all data parsing logic to awk instead of shell
#		|_Need to analyze memory/CPU usage and consider changing
#		|_restart frequency in cron
# Developer:    Redacted (Senior Support Engineer)
######################################################
#Prefix for pid file
pidPrefix="logGen"
#output direcory
outDir="/opt/Redacted/logs/allerrors/"
#Simple function to see if running on primary
checkPrime ()
{
  if /sbin/ifconfig eth0:0|/bin/grep -wq inet;then isPrime=1;else isPrime=0;fi
}


#function to kill previous instances of this script
killScript ()
{
  /usr/bin/find /var/run -name "${pidPrefix}.*.pid" |while read pidFile;do
    if [[  "${pidFile}" != "/var/run/${pidPrefix}.${$}.pid" ]];then
      /bin/kill -- -$(/bin/cat ${pidFile})
      /bin/rm ${pidFile}
    fi
  done
}


#Check to see if primary
#If so, kill any previous instance and start log parsing
#If not, just kill leftover running processes

checkPrime
if [[ "${isPrime}" -eq 1 ]];then
  echo "$$" > /var/run/${pidPrefix}.$$.pid
  killScript
  tail -F -n0 /opt/Fabrix.TV/logs/manager_proxy.log|awk -v dir=$outDir '{OFS="|"}
{
  if ($3 == "E")
  {
    file="allerrors."$1".log"
    gsub("/",".",file)
    if ($9 ~ /@/)
      print $1,$2,$8,$9,substr($0, index($0,$10)) >> dir file
    else {if ($8 !~ /@/)
      print $1,$2,$7,"NULL",substr($0, index($0,$8)) >> dir file
    }
    fflush(stdout);
  }
}'
else
  killScript
  exit 0
fi

 

10 More Discussions You Might Find Interesting

1. HP-UX

Trapping the rm command in UNIX

Hi All, Whoever types rm in the command prompt I need to trap the username, the ip address,timestamp and the rm filename(which file they are removing) and write it to the log file. could anyone help me?. Advanced thanks Suma (1 Reply)
Discussion started by: sumas
1 Replies

2. Shell Programming and Scripting

Getting value of variable set in subprocess script

I am writing a shell script that executes another script by fetching it over the network and piping its contents into sh (ftp -o - $script | sh; or wget -O - |sh). Since this bypasses putting the script on the filesystem, this means I can't source the script directly (using . ), but rather it... (1 Reply)
Discussion started by: hadarot
1 Replies

3. Shell Programming and Scripting

Killing an Xterm while leaving subprocess alive...

Hi, I'm not quite understanding what I'm doing (happens often). This pseudocode works: #!/bin/pseudoksh function kill_parent { when i_want_to ; do sleep 2 kill -TERM $PPID exit done } kill_parent & ssh remote_host sh <<-EOF ... (0 Replies)
Discussion started by: mschwage
0 Replies

4. Shell Programming and Scripting

command << EOF(dont want to call other script)

Dear Freinds, Help needed in input redirection . My problem is as follows.. I have a shell script as follows which calls another gnuplot script . datagen.sh #!/bin/ksh gnuplot plot_I.plt In the above file I am calling another file called plot_I.plt which reside in the same... (4 Replies)
Discussion started by: user_prady
4 Replies

5. UNIX for Advanced & Expert Users

How to kill top command using script?

Hi all, I am using top command in my script to redirect output to temp file. I used kill -9 `ps -ef|grep top|grep -v grep|awk '{print $2}'` to kill top command in my script, but it is not working? Can you please tell how to kill top command in my script? (4 Replies)
Discussion started by: johnl
4 Replies

6. Shell Programming and Scripting

Trapping Enter command !!

Hi I have written a script which works well .. It's divided into a series of jobs one running after another But on pressing the enter key the whole script goes haywire .. I want a way to trap this enter command and echo it as invalid input .. Any suggestions highly appreciated... Thanks :) (2 Replies)
Discussion started by: ultimatix
2 Replies

7. UNIX for Dummies Questions & Answers

Send output of grep as input of kill command

I would appreciate any help. I need to run 'ps -ef | grep 'process', get the process id and kill that process. I have got this far: - Get pid using ps -ef | awk '/process/{ print $2}' after this I'm kind of stuck.. - Use pipe to redirect the output to kill pid=ps -ef | awk '/bmserver/{... (2 Replies)
Discussion started by: foxtron
2 Replies

8. Shell Programming and Scripting

Perl script to kill the process ID of a command after a certain period

All, I am trying to build a script in perl that will alllow me to pass the IP address to a ping command and redirect the output to a file and then kill that process after a certain period of time. let's say, I call my script ping.pl, I would like to be able to run it like this for example :... (7 Replies)
Discussion started by: Pouchie1
7 Replies

9. Shell Programming and Scripting

Kill idle script is killing unnecessarly

Hi All,I have a problem with my kill idle script.my script is supposed to kill the user sessions which are idle for more than 2 hours.But is is killing the sessions which are idle for less than 2 hrs also.I dont know the exact time after which the script is killing,but it is less than 2 hours i am... (3 Replies)
Discussion started by: prabhu_kumar
3 Replies

10. Shell Programming and Scripting

Script which uses “kill -9” command

i have one script which uses “kill -9” command. That prevents from getting the process core dumps. Apparently once tomcat lands in a confused state, we seem to have no other option, other than Kill -9. is there any other way to get rid of this. Script: sleep 2 PID=`ps -ef | grep "^tomcat... (3 Replies)
Discussion started by: Amrutayan09
3 Replies
PPERL(1p)						User Contributed Perl Documentation						 PPERL(1p)

NAME
PPerl - Make perl scripts persistent in memory SYNOPSIS
$ pperl foo.pl DESCRIPTION
This program turns ordinary perl scripts into long running daemons, making subsequent executions extremely fast. It forks several processes for each script, allowing many processes to call the script at once. It works a lot like SpeedyCGI, but is written a little differently. I didn't use the SpeedyCGI codebase, because I couldn't get it to compile, and needed something ASAP. The easiest way to use this is to change your shebang line from: #!/usr/bin/perl -w To use pperl instead: #!/usr/bin/pperl -w WARNINGS
Like other persistent environments, this one has problems with things like BEGIN blocks, global variables, etc. So beware, and try checking the mod_perl guide at http://perl.apache.org/guide/ for lots of information that applies to many persistent perl environments. Parameters $ pperl <perl params> -- <pperl params> scriptname <script params> The perl params are sent to the perl binary the first time it is started up. See perlrun for details. The pperl params control how pperl works. Try -h for an overview. The script params are passed to the script on every invocation. The script also gets any current environment variables, the current working directory, and everything on STDIN. Killing In order to kill a currently running PPerl process, use: pperl -- -k <scriptname> You need to make sure the path to the script is the same as when it was invoked. Alternatively look for a .pid file for the script in your tmp directory, and kill (with SIGINT) the process with that PID. ENVIRONMENT
pperl uses the PPERL_TMP_PATH environment variable to determine the directory where to store the files used for inter-process communication. By default, the subdirectory .pperl of the user's home directory is used. BUGS
The process does not reload when the script or modules change. $^S is not represented identically with respect to perl, since your script will be run within an eval block AUTHOR
Matt Sergeant, matt@sergeant.org. Copyright 2001 MessageLabs Ltd. SEE ALSO
perl. perlrun. perl v5.14.2 2011-11-15 PPERL(1p)
All times are GMT -4. The time now is 09:26 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy