Only one running instance of a script:
flock -n /var/lock/lock_file.lock script.sh
Showing posts with label script. Show all posts
Showing posts with label script. Show all posts
Thursday, July 10, 2014
Thursday, December 20, 2012
Find files in a particular directory which are older than 30 min
My solution to the following problem:
#!/bin/bash
# Current time in seconds since the EPOCH
CURRENT_TIME=$(date +%s)
# 30 minutes in seconds
OFFSET=$((30 * 60))
# The record delimiter
IFS=$'\012'
# The Linux ls
for LINE in $(ls -l --time-style=+%s)
do
FILE_TIME=$(echo ${LINE} | sed 's/[ ]\+/ /g' | cut -d " " -f 6)
FILE_NAME=$(echo ${LINE} | sed 's/[ ]\+/ /g' | cut -d " " -f 7)
TIME_DIFF=$((CURRENT_TIME - FILE_TIME))
# Avoiding that annoying total count in ls -l
if [ -n "${FILE_NAME}" ]
then
if [ ${TIME_DIFF} -gt ${OFFSET} ]
then
echo "${FILE_NAME} ${FILE_TIME} ${CURRENT_TIME} ${TIME_DIFF}"
fi
fi
done
##END##
How can I find files in a particular directory which are older than 30 min? I am not able to use several functions like -mmin, -stat, date -r . And I cant create a dummy file with touch command also,as it is to be done in a production environment.Posted in: http://unix-simple.blogspot.com/2008/01/awkgawk-shell-script-question_31.html
#!/bin/bash
# Current time in seconds since the EPOCH
CURRENT_TIME=$(date +%s)
# 30 minutes in seconds
OFFSET=$((30 * 60))
# The record delimiter
IFS=$'\012'
# The Linux ls
for LINE in $(ls -l --time-style=+%s)
do
FILE_TIME=$(echo ${LINE} | sed 's/[ ]\+/ /g' | cut -d " " -f 6)
FILE_NAME=$(echo ${LINE} | sed 's/[ ]\+/ /g' | cut -d " " -f 7)
TIME_DIFF=$((CURRENT_TIME - FILE_TIME))
# Avoiding that annoying total count in ls -l
if [ -n "${FILE_NAME}" ]
then
if [ ${TIME_DIFF} -gt ${OFFSET} ]
then
echo "${FILE_NAME} ${FILE_TIME} ${CURRENT_TIME} ${TIME_DIFF}"
fi
fi
done
##END##
Friday, August 5, 2011
Convert WAV files to MP3 in Ubuntu
#!/bin/bash
# name of this script: wav2mp3.sh
# wav to mp3
for i in *.wav; do
if [ -e "$i" ]; then
file=`basename "$i" .wav`
lame -h -b 192 "$i" "$file.mp3"
fi
done
# name of this script: wav2mp3.sh
# wav to mp3
for i in *.wav; do
if [ -e "$i" ]; then
file=`basename "$i" .wav`
lame -h -b 192 "$i" "$file.mp3"
fi
done
Thursday, September 23, 2010
Servers, clients, logs and shell arithmetics
Hello everyone,
--
Let's say that we have a client that connects to a server through the network. The application's protocol specifies that the the client should maintain a session and send a message (heartbeat) every 30 seconds to keep the session alive. We have a log file with lines like the one below:
[2010-09-16 08:41:01.966] CLD -> HeartBeat-Sent
If we want to see if everything is OK we could take the last line of the file and examine the timestamp against the local time of the machine. I decide to take only the minutes and seconds and create a number like 1401 and compare that number against the local time in the same format. The line of the shell script that do that part is below:
if [ $(( ${TIME_HERE} - ${TIME_THERE} )) -gt ${INTERVAL} ]
then
STATUS="ERROR"
else
STATUS="OK"
fi
There is a potential runtime error in that block of code. Any idea what could be wrong? Assume both machines are time synchronized to the milliseconds resolution.
The problem in that sentence is related to the way the shell interprets integers. When just one of the operands in the expression (( ${TIME_HERE} - ${TIME_THERE} )) is something like 0008, the shell cries out.
The reason for that cry is that the shell thinks the number is an octal number so the 8 is not a valid digit in that base (0..7). You can see it with a command like:
janeiros@harlie:~$ echo "$(( 1000 - 0008 ))"
-bash: 1000 - 0008: value too great for base (error token is "0008")
You can also verify that the shell is doing octal arithmetic with something like this:
janeiros@harlie:~$ echo "$(( 1000 - 0011 ))"
991
All this could be solve by using the expr command:
janeiros@harlie:~$ echo "$(expr 1000 - 0011)"
989
Octal numbers bring good memories to me, from the time I was in college and I got in touch with the first computer in my life, it was around the beginning of the Eighties. That computer was so primitive that it doesn't have a boot loader, well it doesn't have a disk either! So you have to load a small set of instruction to indicate the paper tape reader to read the monitor program 'cause the machine doesn't even have an operating system! The set of instructions to do that was in the form of octal number that you have to load in the machine as a binary digit representation of the octal number. For doing that you have a series of switches that you can switch to ON/OFF (0,1).
Later I got in touch with UNIX (1991), Interactive UNIX to be more precise (http://en.wikipedia.org/wiki/ INTERACTIVE_UNIX). In UNIX I discovered that my useless knowledge of the octal representation was in fact very useful at the time of working with absolute file permission representation in chmod!
Have a good day!
J. E. Aneiros
GNU/Linux User #190716 en http://counter.li.org
perl -e '$_=pack(c5,0105,0107,0123, 0132,(1<<3)+2);y[A-Z][N-ZA-M]; print;'
PK fingerprint: 5179 917E 5B34 F073 E11A AFB3 4CB3 5301 4A80 F674
Subscribe to:
Posts (Atom)