ksh script - Preventing multiple runs of a particular script

Ask a Question related to Linux / Unix Administration, Design and Development.

  1. #1

    Default ksh script - Preventing multiple runs of a particular script

    I am having trouble with a ksh script I am writing. I need to make
    sure that the script is not already running when it is kicked off in
    cron. When I run the scripts manually, I do not have a problem.
    However, when cron runs the scripts, well, on occassion, it seems that
    more than one instance of the process is running. The odd part is
    that the output is creating only one log file and has only one
    instance of the run.

    I was hoping that someone would take some time to look over my script
    and make some suggestions:

    myScript.ksh
    ================================================== ====

    #!/usr/bin/ksh

    echo "Start Time: `date` "
    echo

    .. /opt/appl/batchftp/29g/29g_env

    process=`ps -ef | grep $0 | grep -v grep | wc -l`

    if [ process -gt 2 ]
    then
    echo "Script is already running. Wait for next run."
    else
    /opt/appl/batchftp/29g/runJava DownloadPOReceiptZATC
    fi

    echo
    echo "End Time: `date` "
    ================================================== =

    I have tried using the following as well:

    myScript2.ksh
    ================================================== =
    #!/usr/bin/ksh

    # set -o xtrace

    echo "Start Time: `date` "
    echo

    .. /opt/appl/batchftp/29g/29g_env

    set -o xtrace

    ps -ef | grep $0 | grep batchftp | grep -v grep

    process=$(ps -ef | grep $0 | grep batchftp | grep -v grep)

    if [ $(echo $process | /usr/bin/wc -l) -gt 1 ]
    then
    echo "Script is already running. Wait for next run."
    else
    /opt/appl/batchftp/29g/runJava DownloadPOReceiptZDIR
    fi

    echo
    echo "End Time: `date` "
    ================================================== ===

    Any suggestions you may have would be greatly appreciated!

    Thanks!

    Melissa
    Melissa Guest

  2. Similar Questions and Discussions

    1. Preventing script attacks from text boxes
      The HTML code that the users are entering. Is this supposed to be displayed as text to others? If so, you can use Server.HTMLEncode() Is it...
    2. Perl script runs in browser, but not via CLI ?
      Hi all, Just dabbling in Perl for the first time, and hit a problem that I can't seem to figure out why its occuring. I have a script that...
    3. Script runs once
      Hello, I have a perl script that is to stop and start at midnight, and write to a new log file. The problem is it runs once then does not run...
    4. #24580 [Opn->Bgs]: sometimes script runs slow
      ID: 24580 Updated by: sniper@php.net Reported By: longshot at home dot nl -Status: Open +Status: ...
    5. #24580 [NEW]: sometimes script runs slow
      From: longshot at home dot nl Operating system: win XP PHP version: 4.3.2 PHP Bug Type: Performance problem Bug description:...
  3. #2

    Default Re: ksh script - Preventing multiple runs of a particular script

    Melissa <melissa_2114@yahoo.com> wrote:
    > more than one instance of the process is running.
    I'd use a lock file approach, something like creating a file in
    a well-know location when the script start and removing when the
    script ends, and then checking if the file is already there before
    starting the script.
    > #!/usr/bin/ksh
    if [ -f /var/run/signal.lock ] ; then
    echo "lock file there"
    exit 0
    else
    touch /var/run/signal.lock
    fi

    ....rest of the script
    > echo
    > echo "End Time: `date` "
    rm /var/run/signal.lock

    Note: I'm not sure if the if/else syntax is correct for ksh.

    Davide
    Davide Bianchi Guest

  4. #3

    Default Re: ksh script - Preventing multiple runs of a particular script

    In article <bn8od3$u87p8$8@ID-18487.news.uni-berlin.de>, Davide Bianchi wrote:
    > Melissa <melissa_2114@yahoo.com> wrote:
    >> more than one instance of the process is running.
    >
    > I'd use a lock file approach, something like creating a file in
    > a well-know location when the script start and removing when the
    > script ends, and then checking if the file is already there before
    > starting the script.
    >
    >> #!/usr/bin/ksh
    >
    > if [ -f /var/run/signal.lock ] ; then
    # See if there is a process running with the PID that is stored
    # in the lock file.
    if kill -0 $(</var/run/signal.lock); then
    > echo "lock file there"
    > exit 0
    else
    echo "Found stale lock file"
    fi

    fi
    > else
    (remove "else")
    > touch /var/run/signal.lock
    Replace with:

    # Put my PID into the lock file.
    print $$ >/var/run/signal.lock
    > fi
    (remove "fi")
    >
    > ...rest of the script
    >
    >> echo
    >> echo "End Time: `date` "
    >
    > rm /var/run/signal.lock
    >
    > Note: I'm not sure if the if/else syntax is correct for ksh.
    >
    > Davide

    --
    Andreas Kähäri
    Andreas Kahari Guest

  5. #4

    Default Re: ksh script - Preventing multiple runs of a particular script

    Andreas Kahari wrote:
    > In article <bn8od3$u87p8$8@ID-18487.news.uni-berlin.de>, Davide Bianchi wrote:
    >
    >>Melissa <melissa_2114@yahoo.com> wrote:
    >>
    >>>more than one instance of the process is running.
    I think there is no 100% way to implement this using
    only shell scripts. This doesn't mean we cannot try
    to make it as good as possible... ;-)
    >>I'd use a lock file approach, something like creating a file in
    >>a well-know location when the script start and removing when the
    >>script ends, and then checking if the file is already there before
    >>starting the script.
    A lock file is one way, a symbolic link is another way to
    indicate that the script is already running. Netscape/Mozilla
    e.g. use an (invalid) symbolic link to the (ip address / process number)
    a process is running on:

    $ cd $HOME/.mozilla/default/*/
    $ ls -l lock
    lrwxrwxrwx 1 heiner users 14 2003-10-24 23:19 lock -> 127.0.0.2:4819

    This means that the mozilla process is running on host with
    ip address 127.0.0.2 (loop back address of my local host),
    process id 4819:

    $ ps -p 4819
    PID TTY TIME CMD
    4819 ? 00:02:50 mozilla-bin

    In my opinion this is a very elegant way avoiding a file.
    >>>#!/usr/bin/ksh
    >>
    >>if [ -f /var/run/signal.lock ] ; then
    >
    > # See if there is a process running with the PID that is stored
    > # in the lock file.
    > if kill -0 $(</var/run/signal.lock); then
    >> echo "lock file there"
    >> exit 0
    > else
    > echo "Found stale lock file"
    > fi
    >
    > fi
    >
    >>else
    >
    > (remove "else")
    >
    >> touch /var/run/signal.lock
    >
    > Replace with:
    >
    > # Put my PID into the lock file.
    > print $$ >/var/run/signal.lock
    >>fi
    >
    > (remove "fi")
    >
    >>...rest of the script
    >>
    >>>echo
    >>>echo "End Time: `date` "
    >>
    >>rm /var/run/signal.lock
    The lock file should be removed by a "trap" handler, too.
    This ensures that the lock file is removed when the script
    receives a signal:

    trap 'rm -f "$lockfile"' 0 # exit
    trap "exit 2" 1 2 3 13 15 # HUP INT QUIT PIPE TERM

    In a networked, multi-host environment it's important
    to either create the lock file on a local file system
    (e.g. /var/run), or (if the lock file may e.g. be NFS mounted)
    include the IP address of the host setting the lock.
    This is the way Mozilla handles it.

    Heiner
    --
    ___ _
    / __| |_ _____ _____ _ _ Heiner STEVEN <heiner.steven@nexgo.de>
    \__ \ _/ -_) V / -_) ' \ Shell Script Programmers: visit
    |___/\__\___|\_/\___|_||_| [url]http://www.shelldorado.com/[/url]

    Heiner Steven Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139