Find the answer to your Linux question:
Results 1 to 4 of 4
Hello All, Is there any way to prevent users to run the same script at the same time, on the same machine? Thanks...
  1. #1
    Just Joined!
    Join Date
    Mar 2006
    Posts
    25

    prevent users to run the same script at the same time, on the same machine

    Hello All,

    Is there any way to prevent users to run the same script at the same time, on the same machine?

    Thanks

  2. #2
    Linux Enthusiast
    Join Date
    Jul 2005
    Location
    Maryland
    Posts
    521
    Try using locks. Create lock file before running the script and remove the lock after it finishes. And create condition (in the script) so that script cannot run if there is a lock.

  3. #3
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    If you use a lock file you should put in the script to trap a ^C to abort. Otherwise you'll have the lock file left behind and you'll have to manually delete the file before anyone can run the script again. Here's a crude example:

    Code:
    #!/bin/ksh -vx
    
    lkfle="/root/test.lk"
    if [ -e $lkfle ]
       then echo "Someone else is running the script"
            exit
       fi
    
    abort_it () {
    echo "Aborting"
    if [ -e $lkfle ]; then rm -fv $lkfle; fi
    exit
    }
    
    trap abort_it INT
    
    touch $lkfle
    sleep 6
    rm -fv $lkfle

  4. #4
    Just Joined!
    Join Date
    Mar 2006
    Posts
    25
    Thank you very much, you guys are my life savers.

Posting Permissions

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