LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-07-2014, 02:19 PM   #1
karank
LQ Newbie
 
Registered: Aug 2014
Posts: 1

Rep: Reputation: Disabled
Control only one instance of script for particular command line argument


Hello

I want to allow only one instance of shell to execute but with one particular command line argument
Thus if the same shell script is executing with another command line argument it shall be be allowed

Ex.

while following is executing
Code:
./outer.sh argument_1
This shall not be allowed
Code:
./outer.sh argument_1
However folowing shall be allowed
Code:
./outer.sh argument_2

AS of now I have got it worked by following methods [1] & [2]; But I want to achieve it using method [3], which is not working for me

[1]
Here I am checking-inserting-deleting the 'command line argument' in a file(say flag or lock file)
However I am looking for better option rather than depending on file external file to control this

Code:
#!/bin/bash

arg=$1

grep ${arg} /tmp/arg.txt

if [ $? -eq 0 ]; then
    echo "another instance of the script is already running"
    exit
	
   if [ $(pidof -x outer.sh| wc -w) -gt 2 ]; then
        echo "another instance of the script is already running"
       exit
   fi
fi

echo ${arg} >> /tmp/arg.txt

while read p; do
sleep 60
    /tmp/inner.sh $p
done < $1

sed -i "/$arg/d" z.txt
[2]

Code:
#!/bin/bash
scriptname=`basename $0`


#if [ `ps -ef|grep ${scriptname}|grep "log1.txt"|awk '{print $2}'|wc -l` -gt 2 ]; then

if [ `ps -ef|grep ${scriptname}|grep ${1}|awk '{print $2}'|wc -l` -gt 2 ]; then
     echo "another instance of the script is already running"
exit
fi

while read p; do
sleep 60
    /tmp/inner.sh $p
done < $1
[3]
The following works only when the "command line argument" is hardcoded in the shell script but not as a variable

Hardcoding the command line argument of inner script in calling i.e. outer script works (as below)
Code:
cat outer.sh
#!/bin/bash

scriptname=`basename $0`

pidfile=/tmp/${scriptname}.pid

if [ -f ${pidfile} ]; then

   OLDPID=`cat ${pidfile}`

# here the command line argument "log1.txt" is hardcoded

   RESULT=`ps -ef | grep ${OLDPID} | grep ${scriptname} | grep "log1.txt"`

   if [ -n "${RESULT}" ]; then

     echo "another instance of the script is already running"

     exit 255

   fi
fi



PID=`ps -ef | grep ${scriptname} | head -n1 |  awk ' {print $2;} '`

echo ${PID} > ${pidfile}

while read p; do

sleep 60

    /tmp/inner.sh $p

done < $1

if [ -f ${pidfile} ]; then
    rm ${pidfile}
fi
Please suggest

Thanks in Advance

Kind Regards
Karan
 
Old 09-07-2014, 02:49 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
All three of your solutions have the same race condition. If another instance is started between doing the test and creating the blocker, then you will have two instances running. You need an atomic operation which both tests and blocks at the same time. Maybe something like:

Code:
BLOCKER_FILE=something based on argument
PID=$$
# Create my pid file
echo $$ >/var/run/$PID
# Try to create a link to it
ln -s /var/run/$PID $BLOCKER_FILE
if [ $? -ne 0 ] ; then
    # failed to create $BLOCKER_FILE
    echo "Already running"
    unlink /var/run/$PID
    exit 1
fi

# Do stuff
...
# All done
unlink $BLOCKER_FILE
unlink $PID
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Create an IBM Cloud instance with the Linux command line LXer Syndicated Linux News 0 01-06-2011 07:10 AM
command line argument in c vipin_jss Linux - Newbie 6 05-11-2009 06:47 PM
bash script 'for each command line argument' true_atlantis Linux - Newbie 3 01-28-2009 01:51 PM
Trying to get a script to accept a command line argument gmccammon Linux - Newbie 1 06-24-2007 09:51 PM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 04:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration