Sponsored Content
Full Discussion: pid.cleanup script.
Top Forums Shell Programming and Scripting pid.cleanup script. Post 302500305 by methyl on Monday 28th of February 2011 07:31:19 AM
Old 02-28-2011
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.

Last edited by methyl; 02-28-2011 at 08:40 AM.. Reason: layout correct cut syntax
This User Gave Thanks to methyl For This Post:
 

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
uuclean(1M)															       uuclean(1M)

NAME
uuclean - uucp spool directory clean-up SYNOPSIS
[options] DESCRIPTION
The commands, including are targeted for removal from HP-UX; see the below. scans the spool directories for files with the specified prefix and deletes all those that are older than the specified number of hours. Options recognizes the following options: Clean directory instead of the spool directory. If directory is not a valid spool directory, it cannot contain ``work files''; i.e., files whose names start with These files have special meaning to pertaining to job statistics. Scan for files with pre as the file prefix. Up to 10 arguments can be specified. A without any pre following will cause all files older than the specified time to be deleted. Files whose age is more than time hours are deleted if the prefix test is satisfied (default time is 72 hours). The default action for is to remove files that are older than a specified time (see option). The option is used to find files older than time hours; however, the files are not deleted. If the argument file is present the warning is placed in file; oth- erwise, the warnings go to the standard output. Only files destined for system sys are examined. Up to 10 arguments can be specified. The option sends mail to the owner of the file when it is deleted. If a file is specified, an entry is placed in file. This program is typically started by (see cron(1M)). WARNINGS
Use of commands, including is discouraged because they are targeted for removal from HP-UX. Use ftp(1) or rcp(1) instead. FILES
spool directory SEE ALSO
uucp(1), uux(1), cron(1M), uucleanup(1M). Tim O'Reilly and Grace Todino, O'Reilly & Associates, Inc. USA. Grace Todino and Dale Dougherty, O'Reilly & Associates, Inc. USA. TO BE OBSOLETED uuclean(1M)
All times are GMT -4. The time now is 02:37 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy