# Script lock functionality

**URL:** <https://community.unix.com/t/script-lock-functionality/338309>\
**Category:** Shell Programming and Scripting\
**Created:** [January 4, 2014, 6:13am UTC](https://community.unix.com/t/script-lock-functionality/338309 "2014-01-04T06:13:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohanalakshmi](https://community.unix.com/letter_avatar/mohanalakshmi/32/6_449c533f586939b6e5f246632a46421f.png) [@mohanalakshmi](https://community.unix.com/u/mohanalakshmi)\
**Post date:** [January 4, 2014, 6:13am UTC](https://community.unix.com/t/script-lock-functionality/338309/1 "2014-01-04T06:13:37Z")

</div>

Hi All,

My requirement is i have a script A.sh .When a person A runs the script A.sh it should get locked and person B should not run the script.  
i followed the below code

```nohighlight
f_script_lock()
{
process=$1
user=`ps -ef | grep -i "$process" | grep -i 'ksh' | awk '{print $1;}'` 
	 if [! -z $user]; then
        echo "Recovery is handled by $user. Hence cannot proceed in Recovery" 
	 exit
	 else
	 continue
	 fi
}

```

The above is working fine in normal conditions.  
But when person A runs the query and press Ctrl+C intermitently the code stops running.  
When person B or even person A tries to execute the A.sh again after sometime it says it has been accessed by the user and not able to execute.  
Please help me on this.

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [January 4, 2014, 6:47am UTC](https://community.unix.com/t/script-lock-functionality/338309/2 "2014-01-04T06:47:12Z")

</div>

You might want to work with a lock file that is written to the filesystem and check if it exists or not. Maybe write the PID into it and if the file is existent, check if a process is still running with that PID etc.  
Additionally you can use `trap` to intercept signals like that of ctrl+c.
