Is there a script or a way in linux that when I try to execute a shell script/process, if the same is running, it will prompt that same is running and will exit otherwise it will continue.
You can use this one liner to do what you want:
Say I had this script:
If we use our one liner:
We run it again:
Waiting 10 seconds and running it again, it again works:
Note this answer is orientated toward a self checking script, for manually checking if a process is running before attempting to run it from the command line, see slm's answer.
The easiest way is to use
If not, you can use a combination of
It is more robust to use a lock file though as it is always possible that the process is running under a different name. Here is an example using
There are many ways to do that. I prefer that the script prevents from running same copies by itself by storing it's pid.
One convenient method is
there are a lot of other scripts. For example solo
The fact that doing something like this reliably in Unix in general is impossible was one of the things that sparked off the development of tools like systemd (it uses the Linux-specific cgroups feature to cage processes, and thus keeping them under tight control, including restarting as needed). You can cobble something together using ps(1) or pgrep(1), stashing away a PID in a file and checking, and so on. But all of those are subject to races, cheating, or plain mistakes.
You should go over your needs and see how critical it is if it goes awry, how repetitive this is (is it worth automating, or is it something done on and off?), how interesting a target it is for potential attackers, and so on.