The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Accessing PL/SQL OUT variables in Korn Shell Script bright_future UNIX for Advanced & Expert Users 2 11-01-2007 09:42 PM
accessing shell script variable in file HIMANI UNIX for Dummies Questions & Answers 6 08-06-2007 08:07 PM
Accessing variables of one shell script in another shell script rsendhilmani Shell Programming and Scripting 1 04-30-2007 05:43 AM
Accessing var from .profile into shell script videsh77 Shell Programming and Scripting 7 05-28-2005 09:21 AM
Accessing the home directory in shell script krishan SUN Solaris 1 05-20-2005 09:41 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-25-2007
Registered User
 

Join Date: Feb 2007
Posts: 1
concurrency issue while Accessing Mail Box from shell script

Hi Bros,

I am in a serious trouble with concurrency issue while using mailx. I have a shell script which reads all the emails of a unix user account and create a copy (another mbox for processing and archive purpose). This script works fine for 99.99% of the time but sometime it start creating multiple copies of the same email during creation of another temp mail box copy and subsequently results in large repetitive insert database calls.

This email loading shell script is a regular cron job which runs after every 30 minutes. My doubts goes to somebody running mailx command or reading email of the same user account while Email Loader is running. Am i correct with my doubt ? Is there anyways to explicit lock the unix user mailbox while my Email Loader program is running ? please advise if i am wring somewhere in script.

I am also providing code snippet here -
# Functions for checking whether there is any emails in user mailbox - returns 0 in variable mailstat if there are new emails in mailbox otherwise 1.

Code:
check_mailbox()
{
 if [ "$mailfile" = "" ]; then  
    mailx -e
 else
   #mailx -e -f $mailfile
    if [ -f "$mailfile" ]; then  
       mailstat=0
     else
       mailstat=1
    fi
 fi
 mailstat=$?
} # check_mailbox  

#### main Block which is running and creating a copy of all mails in users mailbox.


------------
/* mail file is always blank as we never pass any argument to script.*/
mailfile="$1"  

echo "Test to see if there is mail. Exit status will be 0 if there is." >> $logfile
check_mailbox
if [ $mailstat -eq 1 ]; then
   echo "No mails exist in mailbox" >> $logfile
   exit 1
fi

echo "Archiving Mailbox." >> $logfile
touch  ${mailboxarchive} 

while ( [ $mailstat -eq 0 ] )
do
  echo "save ${mailboxarchive}
        quit" > $exmailfl
  if [ "empty$mailfile" = "empty" ]; then
     mailx < $exmailfl > $outfile
  else
    if [ ! -f $mailfile ]; then
      # all done
       break
    fi
    mailx -f $mailfile < $exmailfl > $outfile
  fi
  check_mailbox
done

anybody have any clue on where i am doing foolish act ? any help bros

looking forward for some unix experts help (i m a j2ee guy and don't have that depth in Unix schell scripting)

Thx in well advance.
Sumit

Last edited by Perderabo; 02-25-2007 at 08:58 AM. Reason: Add code tags for readability
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-25-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,333
You don't say what os you're using, what language you're trying to use, or give much info on how the script is used (always for one particular user? for everyone on the system?). I will assume that you're using Solaris 10, trying to program in ksh, and running the script for every user on the box. If these assumptions are wrong, your mileage may vary.

There are several problems with your code. I see the comment where you claim the mailfile is always blank. But the code which is present to handle a non-blank mailfile is guaranteed to fail and should be removed. Leaving broken remnants of old code in a script is asking for trouble down the road. I have never seen syntax like:
while ( [ $mailstat -eq 0 ] )
I think that may be legal. It might even be useful with a much more complex test. The normal syntax is:
while [ $mailstat -eq 0 ]
Your parentheses are demanding that the test be preformed in a subshell. The exit code of the subshell should be the exit code of the [ command in this case. But you are starting an extra process for no good reason. More to the point, the loop is not useful. You should be doing:
1) check for existence of mail, exit if none
2) save any mail present

There is a concurrency issue here... mail may be present during step 1 and absent during step 2. Step 2 needs to accept that...it is not an error. Step 2 should be a one shot deal, not a loop. If new mail arrives immediately after the save, your script will get to it the next time it is launched. You are looping, probably not to handle newly arrived mail, but rather to handle one message at a time. That is not efficient. "save * filename" will grab all of the mail at once. It is more efficient and eliminates most of your problem.

mailx initializes itself by processing a system-wide startup script. Then it processes a user specific startup script. A -n will inhibit the former but not the latter. The mailx man page says the save command "Save the specified messages in the given file. The file is created if it does not exist. The file defaults to mbox. The message is deleted from the mailbox when mailx terminates unless keepsave is set (see also Internal Variables and the exit and quit commands). If a user has a .mailrc that sets keepsave, your script will not empty his mailbox. This is my best guess as to why you have your current problem and I don't know what to suggest. I don't think you should override a user's wishes without consulting with the user in question.
Reply With Quote
  #3 (permalink)  
Old 02-25-2007
Registered User
 

Join Date: Jan 2007
Posts: 366
Quote:
Originally Posted by Sumit_Fundoo
Hi Bros,

I am in a serious trouble with concurrency issue while using mailx. I have a shell script which reads all the emails of a unix user account and create a copy (another mbox for processing and archive purpose). This script works fine for 99.99% of the time but sometime it start creating multiple copies of the same email during creation of another temp mail box copy and subsequently results in large repetitive insert database calls.

This email loading shell script is a regular cron job which runs after every 30 minutes. My doubts goes to somebody running mailx command or reading email of the same user account while Email Loader is running. Am i correct with my doubt ? Is there anyways to explicit lock the unix user mailbox while my Email Loader program is running ? please advise if i am wring somewhere in script.

I am also providing code snippet here -
# Functions for checking whether there is any emails in user mailbox - returns 0 in variable mailstat if there are new emails in mailbox otherwise 1.

Code:
check_mailbox()
{
 if [ "$mailfile" = "" ]; then  
    mailx -e
 else
   #mailx -e -f $mailfile
    if [ -f "$mailfile" ]; then  
       mailstat=0
     else
       mailstat=1
    fi
 fi
 mailstat=$?
} # check_mailbox  

#### main Block which is running and creating a copy of all mails in users mailbox.


------------
/* mail file is always blank as we never pass any argument to script.*/
mailfile="$1"  

echo "Test to see if there is mail. Exit status will be 0 if there is." >> $logfile
check_mailbox
if [ $mailstat -eq 1 ]; then
   echo "No mails exist in mailbox" >> $logfile
   exit 1
fi

echo "Archiving Mailbox." >> $logfile
touch  ${mailboxarchive} 

while ( [ $mailstat -eq 0 ] )
do
  echo "save ${mailboxarchive}
        quit" > $exmailfl
  if [ "empty$mailfile" = "empty" ]; then
     mailx < $exmailfl > $outfile
  else
    if [ ! -f $mailfile ]; then
      # all done
       break
    fi
    mailx -f $mailfile < $exmailfl > $outfile
  fi
  check_mailbox
done

anybody have any clue on where i am doing foolish act ? any help bros

looking forward for some unix experts help (i m a j2ee guy and don't have that depth in Unix schell scripting)

Thx in well advance.
Sumit
Why not create a .forward file in the home directory of those accounts for which you want this process to take place.

Suppose we have an account "abc".

The .forward file in the homedirectory of "abc" could look like:
Code:
#cat .forward
\abc, /archivedir/abc
This .forward file will cause new email to be delivered to the regular mailbox of abc as well as to "/archivedir/abc". "/archivedir/abc" has the format of any other mailbox and can be read as such.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:34 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0