fuser exit code different when script is run from cron


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting fuser exit code different when script is run from cron
# 1  
Old 12-29-2009
fuser exit code different when script is run from cron

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:

Code:
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:

Code:
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:

Code:
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:

Code:
*/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:

Code:
/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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inner script run and its exit status

Main Script #!/bin/ksh echo "Maimn script" ./clocal/www/web-data/WAS/WebSphere7/scripts/DealerLocator/Scripts/secondscript.ksh echo "$? = status" Sdecond Script #!/bin/ksh echo "In second SCript" exit 1 Output: Maimn script ./testmain.ksh:... (4 Replies)
Discussion started by: dineshaila
4 Replies

2. UNIX for Advanced & Expert Users

Fuser alternative OR running fuser on a script

Hi, Not sure whether there is a fuser alternative or any better way to check for file in use or not. I am wanting to check whether files are in use or not before removing them. Using fuser, the awk seems to be giving me 'weird' output not to mention that it is giving me 2 lines instead of... (0 Replies)
Discussion started by: newbie_01
0 Replies

3. UNIX for Advanced & Expert Users

Script run by cron gives no output

Hi All, I have a script which checks the status of HP Smart Array & then emails me the output. The script run fine when executed manually but I receive no output when configured in a cron job. The script is below: hpacucli ctrl slot=3 show config | mail -s "ARRAY STATUS-`date`"... (6 Replies)
Discussion started by: coolatt
6 Replies

4. UNIX for Advanced & Expert Users

Need a exit from sftp if its ask for password and continue to run remaining part of script.

Hi I am checking status of sftp in Health check script, sftp command is used to connect the server with secure RSA key, which is successfully get connected most of the time but in some case if RSA key ask for password then I need to exit sftp command after few second and continue to run... (1 Reply)
Discussion started by: ketanraut
1 Replies

5. Shell Programming and Scripting

Need help to run a script on cron

I need someone to help me out to upload script on host but I have problem can anyone help me out ? (2 Replies)
Discussion started by: spit
2 Replies

6. HP-UX

script in cron didnt run

Hi, I have script in the cron which run at predefined time everyday. If the script fails then we get the mail and a log is created. But last night the script didnt run. If the script is even started it creates log so it seems the script never ran. It has never happened before. Th... (5 Replies)
Discussion started by: shipra_31
5 Replies

7. Shell Programming and Scripting

how to run script? call other script? su to another user? make a cron?

Good morning. I am searching for "how-to"'s for some particular questions: 1. How to write a script in HP-UX 11. 2. How to schedule a script. 3. How to "call" scripts from the original script. 4. How to su to another user from within a script. This is the basics of what the... (15 Replies)
Discussion started by: instant000
15 Replies

8. UNIX for Advanced & Expert Users

Need help with a script run by a cron job.

Hi, new to this forum and not sure if this is the right place to post. I'm new to cron jobs and scripts, and I need some help with a script to be used with a cron job. I already have a bot set up at a certain website address and need a script that the cron job will load every day that tells it to... (1 Reply)
Discussion started by: klawless
1 Replies

9. Shell Programming and Scripting

Help with script - run by cron

Hello, I have a shell script that runs every minute to process incoming files delivered externally via SFTP to a directory. Basically the script works and processes the files however I get an error when a new file is delivered into the directory. Please see my script below. A new file is... (2 Replies)
Discussion started by: richo king
2 Replies

10. Shell Programming and Scripting

Can run script Manually, but not through Cron?

Hi all, I have a main script (called OracleCleanup) that runs some sql queries. that runs off a wrapper script which contains the sources for the login information and and JOB_HOME (the script is below). When I schedule this job in the cron the log says that it cannot open my list file, which... (4 Replies)
Discussion started by: MadHatter
4 Replies
Login or Register to Ask a Question