Ask a Question related to Linux / Unix Administration, Design and Development.
-
Melissa #1
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
-
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... -
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... -
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... -
#24580 [Opn->Bgs]: sometimes script runs slow
ID: 24580 Updated by: sniper@php.net Reported By: longshot at home dot nl -Status: Open +Status: ... -
#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:... -
Davide Bianchi #2
Re: ksh script - Preventing multiple runs of a particular script
Melissa <melissa_2114@yahoo.com> wrote:
I'd use a lock file approach, something like creating a file in> more than one instance of the process is running.
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.
if [ -f /var/run/signal.lock ] ; then> #!/usr/bin/ksh
echo "lock file there"
exit 0
else
touch /var/run/signal.lock
fi
....rest of the script
rm /var/run/signal.lock> echo
> echo "End Time: `date` "
Note: I'm not sure if the if/else syntax is correct for ksh.
Davide
Davide Bianchi Guest
-
Andreas Kahari #3
Re: ksh script - Preventing multiple runs of a particular script
In article <bn8od3$u87p8$8@ID-18487.news.uni-berlin.de>, Davide Bianchi wrote:
# See if there is a process running with the PID that is stored> 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
# in the lock file.
if kill -0 $(</var/run/signal.lock); then
else> echo "lock file there"
> exit 0
echo "Found stale lock file"
fi
fi
(remove "else")> else
Replace with:> touch /var/run/signal.lock
# Put my PID into the lock file.
print $$ >/var/run/signal.lock
(remove "fi")> 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
-
Heiner Steven #4
Re: ksh script - Preventing multiple runs of a particular script
Andreas Kahari wrote:
I think there is no 100% way to implement this using> 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.
only shell scripts. This doesn't mean we cannot try
to make it as good as possible... ;-)
A lock file is one way, a symbolic link is another way to>>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.
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.
The lock file should be removed by a "trap" handler, too.>>>>>>#!/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> else>> echo "lock file there"
>> exit 0
> 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
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



Reply With Quote

