Hi guys!
I have a directory in the production environment from which i have to delete files older then 40 minutes with .pid extention. I wrote a script below for the purpose.
#!/bin/bash
#
# Script to delete specific file older than N minutes.
# OLDERTHAN="40" #40 minutes
FOLDER="home/optima/pids/"
PID="*.pid"
OLDERTHAN="40"
if [ -e ${FOLDER}/${PID} ]
then
ls_time=`ls -l ${FOLDER}/$PID`
pid_h=`echo $ls_time | cut -d\ -f 8 | cut -d\: -f 1`
pid_m=`echo $ls_time | cut -d\ -f 8 | cut -d\: -f 2`
echo pid time=$pid_h:$pid_m
let pid_time=(10#$pid_h*60)+10#$pid_m
curr_h=`date | cut -d\ -f 4 | cut -d\: -f 1`
curr_m=`date | cut -d\ -f 4 | cut -d\: -f 2`
let curr_time=(10#$curr_h*60)+10#$curr_m
echo curr_time=`date | cut -d\ -f 4`
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}..."
rm -f ${FOLDER}/${PID}
else
echo -e "${PID} is not older than $OLDERTHAN minutes"
fi
else
echo -e "${PID} not found."
fi
I created a test pid in /home/somefolder/pids to check if the script will delete old pids or not. I ran it & it showed the following result �*.pid not found.� Any advice?
# ls -ltr
total 96
-rw-r----- 1 root sys 0 Feb 28 09:00 test.pid
-rwxr-xr-x 1 root sys 15 Feb 28 11:52 optimamd_opx_LOD_GEN_110_00111010L.pid
-rwxr-xr-x 1 root sys 15 Feb 28 11:52 optimamd_opx_LOD_GEN_110_001110107.pid
-rwxr-xr-x 1 root sys 15 Feb 28 11:52 optimamd_opx_LOD_GEN_110_001110109.pid
-rwxr-xr-x 1 root sys 16 Feb 28 11:52 optimamd_opx_LOD_GEN_110_00111010H.pid
-rwxr-xr-x 1 root sys 15 Feb 28 11:52 optimamd_opx_LOD_GEN_110_00111009A.pid
-rwxr-xr-x 1 root sys 15 Feb 28 11:52 optimamd_opx_LOD_GEN_110_001110106.pid
#
/home/optima/run
-rwxrwxrwx 1 optima dba 1716 Jun 2 2010 NSN_MGWtest_DAP.sh
-rwxrwxrwx 1 optima dba 2828 Jul 7 2010 RunValidators_HUAWEI_BSC6000.sh.orig
-rwxrwxrwx 1 optima dba 5482 Jul 7 2010 NSN_MSS_DAP.sh
-rwxrwxrwx 1 optima dba 1729 Jul 7 2010 NSN_MGW_DAP.sh
-rwxrwxrwx 1 optima dba 244 Jul 7 2010 Run_FTP.sh
-rwxrwxrwx 1 optima dba 1914 Jul 7 2010 NSN_MGW_VAL.sh
-rwxrwxrwx 1 optima dba 6013 Jul 7 2010 NSN_MSS_VAL.sh
-rwxrwxrwx 1 optima dba 5138 Jul 9 2010 NSN_MSS_loader.sh
-rwxrwxrwx 1 optima dba 1715 Jul 9 2010 NSN_MGW_loader.sh
-rwxrwxrwx 1 optima dba 1460 Jul 10 2010 MOTOROLA_BSS_VALIDATOR.sh
-rwxrwxrwx 1 optima dba 6100 Oct 11 08:37 RunLoaders_HUAWEI_BSC6000.sh
-rwxrwxrwx 1 optima dba 2828 Oct 18 12:40 RunValidators_HUAWEI_BSC6000.sh
-rwxrwxrwx 1 optima dba 945 Nov 29 16:27 MOTOROLA_BSS_CMB.sh
-rwxrwxrwx 1 optima dba 2120 Dec 6 11:39 MOTOROLA_BSS_LOADERS.sh
-rwxrwxrwx 1 optima dba 228 Jan 10 11:31 OLD_RunProcessMonitor_004.sh
-rwx------ 1 root sys 252 Jan 19 12:34 Neighbour_single.sh
-rwx------ 1 root sys 228 Jan 26 12:07 RunProcessMonitor_004.sh
-rwxrwxrwx 1 root sys 957 Feb 28 11:39 pid_cleanup.sh
# ./pid_cleanup.sh
*.pid not found.
will changing
PID="*.pid"
to
PID="*pid* *.pid*".
help?
---------- Post updated at 06:44 AM ---------- Previous update was at 05:19 AM ----------
I replaced PID=�*.pid�
with PID="*pid* *.pid*"
and ran the script and got the below result. Kindly advice.
# ./pid_cleanup.sh
./pid_cleanup.sh: script: not found.
./pid_cleanup.sh[10]: Syntax error at line 16 : `(' is not expected.
#