0

I have multiple scripts running. All the scripts have the same name but the commands they execute are doing different things.

I am trying to figure out if a certain instance of the script is running and if so I don't want it to run again. This is difficult because all of the scripts have the same name so it could be a false positive by using pgrep.

My idea was if there is any attribute or description I could attach when it is first run, then I can grep for that unique description and tell which instance is running, is there any way to do this? Does anyone have any other ideas?

Thanks

Good Person
  • 1,437
  • 2
  • 23
  • 42
user1490083
  • 361
  • 2
  • 7
  • 21
  • Use `lsof -p ` to find out which process is what. Then rename your scripts to something meaningful. – twalberg Jan 07 '14 at 20:00
  • The suggestion of "give your scripts more meaningful names" is a good one; failing that, you could elaborate in your question why they're all named the same thing, as the reasoning behind that decision might illuminate suggestions on how to resolve the downstream issue created by that decision. – TML May 15 '14 at 17:09

1 Answers1

0

You can implement below logic to check weather script is already running or not.

if [ -f Script1.lck ];then

        echo "ALREADY INSTANCE RUNNING `date`" 
        echo "EXITING" 
else
         echo "NO INSTANCE RUNNING "
         touch  Script1.lck
         #Your Script code here.............
         rm -f Script1.lck
fi

*I used concept that every time it runs checks for Script1.lck file if file not exists than it means no instance is running. So it creates file and start executing your code. Suppose in between you executed the script then it checks for .lck file and and .lck already exists due to previous instance.

*In last i removed the .lck file.

*By using different lck file names for different script you can check which script is running.

Tajinder
  • 2,248
  • 4
  • 33
  • 54