?
Solved

Simultaneous Runs

Posted on 2003-03-31
7
Medium Priority
?
339 Views
Last Modified: 2010-04-21
I am sort of new to UNIX and don't know much of the commands, there I have this question to ask of you guys.

I have a unix script that is on a schedule to run every friday at 5pm, and to also run on last day of month at 5pm.  There will be cases when the two criterias above are true, therefore causing the same job to run simultaneously.  Are there any UNIX statements that would avoid this situation from occuring?

Thanks much for your help!
0
Comment
Question by:EvanstonEddie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 6

Expert Comment

by:bira
ID: 8241581
Place a test in your script as follow:

a=`ps -ef |grep YOURSCRIPT`
if [ $? -eq 0 ] ; then
echo "this is running now"
exit
fi

   So the script will never run if it find another instance
 running.
0
 
LVL 48

Accepted Solution

by:
Tintin earned 100 total points
ID: 8242355
Generally, you use a lock file for this sort of thing, eg:

#!/bin/sh

LOCKDIR=/var/lock
LOCKFILE=`basename $0`

lock()
{
    [ -d $LOCKDIR ] || error "Lock dir: $LOCKDIR does not exist"

    if [ -f $LOCKFILE ]; then
        pid=`cat $LOCKFILE`
        if kill -0 $pid >/dev/null 2>&1
        then
            echo "Previous process $pid still running"
            exit 0
        else
            echo "Removing stale lock file"
        fi
    fi

    echo "Creating lockfile: $LOCKFILE"
    echo $$ >$LOCKFILE || error "Can not create $LOCKFILE"
}


unlock()
{
    [ -f $LOCKFILE ] || return 0

    rm -f $LOCKFILE
    echo "Removing lockfile: $LOCKFILE"
}

trap 'unlock' 1 2 3 15

lock

echo "Starting script....."

unlock
0
 
LVL 1

Expert Comment

by:Santunes
ID: 8245525
in bira's script you should change the first line for
a=`ps -ef |grep YOURSCRIPT|grep -v grep`
otherwise the script will never run.
0
 
LVL 6

Expert Comment

by:bira
ID: 8246507
Hey Santunes

  I agree

    Regards
0
 
LVL 62

Assisted Solution

by:gheist
gheist earned 100 total points
ID: 8389768
with grep used users can evade your script too , locking script seems fine when you use random pid's in your system, just replace every occurence of "echo" with "logger" to run from cron
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question