Question: I want to make a sh script that will only run at most once at any point. 

Say, if I exec the script then I go to exec the script again, how do I make it so that if the first exec of the script is still working the second one will fail with an error. I.e. I need to check if the script is running elsewhere before doing anything. How would I go about doing this??

The script I have runs a long running process (i.e. runs forever). I wanted to use something like cron to call the script every 15mins so in case the process fails, it will be restarted by the next cron run script.

Answer: 
You can use the `run-one` package, which provides `run-one`, `run-this-one` and `keep-one-running`.

The package: <https://launchpad.net/ubuntu/+source/run-one>

The blog introducing it: <http://blog.dustinkirkland.com/2011/02/introducing-run-one-and-run-this-one.html>
```
(
        if flock -n 9
        then
                echo 'Not doing the critical operation (lock present).'
                exit;
        fi

        # critical section goes here

) 9>'/run/lock/some_lock_file'
rm -f '/run/lock/some_lock_file'

```

From example in flock(1) man page. Very practical for using in shell scripts.