Hi,
I am writing a bash script (running on Centos 5.4) to process video (.MTS) files which may have appeared in a certain directory. The files will be dragged and dropped there from a Windows box using Samba, and the script is to check periodically (i.e. run from cron) whether any new .MTS files have arrived and process them if so.
However, due to the size of the files and their method of arrival, it is possible that when the script starts to run, the files may still be in the process of being copied in and will thus be incomplete. In such circumstances the script is to ignore them and exit. I have been using fuser for this purpose, as per the following code snippet:
echo "There are MTS files"
# Check whether files are in use (might still be being copied into directory)
fuser *.MTS > /dev/null 2>&1
fuser_output=`echo $?`
########### TEST CODE
echo "$fuser_output"
#######################
if [[ "$fuser_output" != "0" ]]; then
echo "MTS files not in use; processing..."
...
else
echo "MTS files in use; skipping"
fi
If I manually copy some big files into the designted directory and then kick off the script (as root) from the command line, the test lines above show that the fuser exit code is 0, and the output from the script is as follows:
MTS files in use; skipping
Once the files have finished copying, if I run the script from the command line again, the fuser exit code is 1, and I get the following output from the script:
MTS files not in use; processing...
This is exactly what I want the script to do, and it appears to be working perfectly. However the problem starts when I try to automate the process by calling the script from cron. The following is my entry in (root's) crontab:
*/5 * * * * /usr/bin/Camcorder_processing.sh >/dev/null 2>&1 # Check for/process camcorder .MTS files every 5 minutes
If I again copy some large files into the directory just as the cron entry is about to run, I find that the script still proceeds to process the files, despite them being in use (I sent the output to a log file). This time the exit code from the fuser command is 127, and because it is not 0 the script is proceeding to process the files when it should not do.
What I don't understand is why the fuser exit code is different when the script is run from cron (also I don't know what exit code 127 means). Can anyone shed any light on this strange behaviour and help me to solve this problem please?
Thank you in advance.
---------- Post updated at 03:26 PM ---------- Previous update was at 11:40 AM ----------
I figured it out.
It turns out that the exit code 127 was not from the fuser command but from bash, telling me that it couldn't find fuser. I needed to use the full path to fuser in the script:
/sbin/fuser *.MTS > /dev/null 2>&1
With that in place it works great. Hope this helps someone else who may be as confounded by it as I was!