Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Simultaneous Runs

Posted on 2003-03-31
7
Medium Priority
?
340 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
5 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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 Month10 days, 22 hours left to enroll

572 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