Sponsored Content
Full Discussion: pid.cleanup script.
Top Forums Shell Programming and Scripting pid.cleanup script. Post 302500331 by sajid.shah on Monday 28th of February 2011 09:38:00 AM
Old 02-28-2011
Quote:
Originally Posted by zaxxon
And another more general thing is, that the programs or scripts that generate those pid files, should be able to clean them up themselves. It seems that there is something not working properly and that this script is just a workaround for the real problem.

:-) You got me there mate. This is a work around. I am trying to support my customer who is on the other side of the planet. And I cant have a remote session on his production server due to their IT Policey. I will look into your suggestion and will give you an Update. Thank you very much for your support.

---------- Post updated at 09:19 AM ---------- Previous update was at 08:43 AM ----------

Quote:
Originally Posted by methyl
Referring to post #1 (we'll ignore post #2).
The script contains a fundamental logic error. If there is more than one file matching the pattern *.pid the variable $PID contains multiple values. From that point on the script fails.

I'm not quite clear what you are trying to do here. Reading betweeen the lines, beware that deleting a ".pid" file will not stop the process with that Process ID.

The script needs restructuring to process each file in turn.

e.g.

Code:
#!/bin/bash
#
# Script to delete specific file older than N minutes.
# OLDERTHAN="40" #40 minutes
 
FOLDER="home/optima/pids/"
ls -1d "${FOLDER}"/*.pid 2>/dev/null | while read PID
do
OLDERTHAN="40"
if [ -e ${FOLDER}/${PID} ]
then
     ls_time=`ls -l ${FOLDER}/$PID`
     pid_h=`echo $ls_time | cut -d' ' -f8 | cut -d\: -f1`
     pid_m=`echo $ls_time | cut -d' ' -f8 | cut -d\: -f2`
     echo pid time=$pid_h:$pid_m
     let pid_time=(10#$pid_h*60)+10#$pid_m
     curr_h=`date | cut -d' ' -f4 | cut -d\: -f1`
     curr_m=`date | cut -d' ' -f4 | cut -d\: -f2`
     let curr_time=(10#$curr_h*60)+10#$curr_m
     echo curr_time=`date | cut -d' ' -f4`
     let diff=10#$curr_time-10#$pid_time
     #echo pid_time=$pid_time
     #echo curr_time=$curr_time
     echo diff=$diff minutes
     if [ $diff -ge $OLDERTHAN ]
     then
          echo "${PID} is older than $OLDERTHAN minutes"
          echo "Deleting ${PID}..."
          # Remove echo when thoroughly tested
          echo rm -f ${FOLDER}/${PID}
     else
          echo -e "${PID} is not older than $OLDERTHAN minutes"
     fi
else
     echo -e "${PID} not found."
fi
done

Keep the "rm" line as an "echo" until you are happy that your script does what you want.
Noted zaxxon comments and corrected "cut" statements.
Noted that the "date" line could be better but didn't change it.

Thanks methyl. That was very educating. You are right about the process thingy. I might end up getting a zombie process.

Let me explain what is going on a bit. There is a process, or a program, called the Process Monitor. Its job is to remove the failed programs.

Whenever a process is initiated, it generates an associated monitor file (.i.e. <exename>_<programid>.pid) file. The purpose of this file is to make sure that the new instances of a program are not started if the previous instance is still running.

Before a program starts an instance, it checks if an associated monitor file exists. If one does exist, then this indicates that an instance is already running and so the program immediately exits. If a monitor file does not exist, the program starts and creates a monitor file. This file is uniquely associated to the program instance using the PRID in a common directory (the default is $OPTIMA/PIDS). When the program has run, it removes the monitor file. The Process Monitor ensures that monitor files are removed if programs crash or hang.

By the way, I tried the following set of lines and it worked but with a catch.


Code:
#!/bin/ksh
# filetimes
filetime()
{
perl -e '
$mtime = (stat $ARGV[0])[9];
print $mtime
' $1
}
now=$(date +%s)
limit=$(( $now - 1800 )) # one half hour ago.
find /home/optima/pids -type f |\
while read filename 
do
dt=$(filetime $filename)
if [[ $dt -lt $limit ]] ; then
rm -f $filename # delete ALL files older then 30 minutes
fi
done


I have realized that the PIDS in /pids folder have the same updated time like the below figs

Quote:
total 96
Quote:
-rw-r----- 1 root sys 0 Feb 28 09:00 test.pid
-rwxr-xr-x 1 root sys 15 Feb 28 23:52 optimamd_opx_LOD_GEN_110_00111010L.pid
-rwxr-xr-x 1 root sys 15 Feb 28 23:52 optimamd_opx_LOD_GEN_110_001110107.pid
-rwxr-xr-x 1 root sys 15 Feb 28 23:52 optimamd_opx_LOD_GEN_110_001110109.pid
-rwxr-xr-x 1 root sys 16 Feb 28 23:52 optimamd_opx_LOD_GEN_110_00111010H.pid
-rwxr-xr-x 1 root sys 15 Feb 28 23:52 optimamd_opx_LOD_GEN_110_00111009A.pid
-rwxr-xr-x 1 root sys 15 Feb 28 23:52 optimamd_opx_LOD_GEN_110_001110106.pid
#


the script will not be able to delete anyone of the pids since the time is same and the loader will see that another process is running. I have hit the wall here Smilie
any advice dear?




---------- Post updated at 09:23 AM ---------- Previous update was at 09:19 AM ----------

Quote:
Originally Posted by xoops
can be done with 1 find command

Code:
 
find /home/somefolder/pids -maxdepth 1 -type f -name "*.pid" -amin +40 -exec rm {} \;

Thanks mate. I tried this before even posting here. Particular flavor of the solaris dosnt support -amin or -mmin of the find command

---------- Post updated at 09:38 AM ---------- Previous update was at 09:23 AM ----------

Standard Unix file systems don't store creation time. The only times
Unix stores for files are access time, modification time, and inode
change time.

Quote:
total 96
-rw-r----- 1 root sys 0 Feb 28 09:00 test.pid
-rwxr-xr-x 1 root sys 15 Feb 28 23:52 optimamd_opx_LOD_GEN_110_00111010L.pid
-rwxr-xr-x 1 root sys 15 Feb 28 23:52 optimamd_opx_LOD_GEN_110_001110107.pid
-rwxr-xr-x 1 root sys 15 Feb 28 23:52 optimamd_opx_LOD_GEN_110_001110109.pid
-rwxr-xr-x 1 root sys 16 Feb 28 23:52 optimamd_opx_LOD_GEN_110_00111010H.pid
-rwxr-xr-x 1 root sys 15 Feb 28 23:52 optimamd_opx_LOD_GEN_110_00111009A.pid
-rwxr-xr-x 1 root sys 15 Feb 28 23:52 optimamd_opx_LOD_GEN_110_001110106.pid
Oh GOD help ... I not cut for this Smilie

Last edited by sajid.shah; 02-28-2011 at 11:08 AM..
 

10 More Discussions You Might Find Interesting

1. Programming

printing ppid,child pid,pid

question: for the below program i just printed the value for pid, child pid and parent pid why does it give me 6 values? i assume ppid is 28086 but can't figure out why there are 5 values printed instead of just two! can someone comment on that! #include<stdio.h> #define DIM 8 int... (3 Replies)
Discussion started by: a25khan
3 Replies

2. UNIX for Dummies Questions & Answers

Session PID & socket connection pid

1. If I use an software application(which connects to the database in the server) in my local pc, how many PID should be registered? Would there be PID for the session and another PID for socket connection? 2. I noticed (through netstat) that when I logged in using the my software application,... (1 Reply)
Discussion started by: pcx26
1 Replies

3. Shell Programming and Scripting

awk/sed/ksh script to cleanup /etc/group file

Many of my servers' /etc/group file have many userid's that does not exist in /etc/passwd file and they need to be deleted. This happened due to manual manipulation of /etc/passwd files. I need to do this for 40 servers. Can anyone help me in achieving this? Even reducing a step or two will be... (6 Replies)
Discussion started by: pdtak
6 Replies

4. Shell Programming and Scripting

User Cleanup Script

Hi Guys, I've got an system setup to act as an sftp server. I have a script that allows me to create chroot users running a custom shell within their home directory, it also creates a subdirectory that they can write into. I'm trying to write a script (that I can cron at a later date) that checks... (3 Replies)
Discussion started by: King_Brucie
3 Replies

5. Shell Programming and Scripting

Cleanup script

Hi! I would like to write a script which remove some files, all beginning with the same prefix : prefix.1 doc/prefix.2 ../prefix.3 etc. So, I would create a file and chmod it executable. But I dont know how to pass a variable to a script. I would like to write something like ... (2 Replies)
Discussion started by: tipi
2 Replies

6. Shell Programming and Scripting

Suggestions/cleanup Bash script

Hello, beginner bash scripter here.. I was able to write a script and it works just fine. I'm just wondering if someone could chime in or any suggestions to make it cleaner or tighter so to speak. I have a disk to disk backup solution which uses 250GB disks. When one gets full I just po in a new... (7 Replies)
Discussion started by: woodson2
7 Replies

7. Shell Programming and Scripting

Mail cleanup from ksh script, keeping 50 most recent msgs

I found some posts describing how to completely clean out a mailbox in Unix/Linux. But I want to keep the 50 most recent messages. Any ideas out there? Thanks! (3 Replies)
Discussion started by: OPTIMUS_prime
3 Replies

8. UNIX for Advanced & Expert Users

Table Cleanup Script

I needed some help with a script to fetch and delete all records prior to 3 days from now connecting to sybase from sunos. I wrote the following script but not working..can someone please guide me with my code. Thanks #!/bin/ksh ##GET PREVIOUS DAY DATE dt=`date | awk... (3 Replies)
Discussion started by: moe458
3 Replies

9. Shell Programming and Scripting

Suggestion with script to cleanup

I need help with sed and awk scripts to search for Symmetrix ID=000090009902 and then grep its child disk devices associated to the dead paths and display them only, so that those dead devices can be removed. test01:/#powermt display dev=all Pseudo name=hdiskpower0 Symmetrix ID=000090009902... (0 Replies)
Discussion started by: aix_admin_007
0 Replies

10. Shell Programming and Scripting

UNIX script for cleanup

Hello, I need some help from unix guru's here..I am looking for some advanced level script to cleanup the directories and files from specific directories under a file system.. The folders are created under /opt/modules And under modules, there are multiple subfolders with the application... (6 Replies)
Discussion started by: mb525
6 Replies
All times are GMT -4. The time now is 04:39 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy