No, there's no general way to do that. Processes are meant to be isolated from each other.
Classic approach: You can implement something like a pidfile, which is simply a file that your program checks for existence (by opening it in a mode that forbids creation of the file if it already exists):
- If it doesn't exist, your program locks the file and writes its own process ID into, and starts. If it does exist, your program checks whether the process specified within still exists.
- If the lockfile exists and the ID within still belongs to a process, your program exits. If the process doesn't exist, your program deletes the pidfile, creates a new one, locking it and writing its own process ID to it, and starts.
Generally, you often avoid this hassle, recognize that it's not your program's problem, and it might be totally valid to run the same program twice in parallel if defined to work on different data (e.g. you can have two web servers running at the same time, if they don't both try to bind to the same TCP port, say 80), and you trying to be "clever" causes problems for your user. Instead, you would make the running of only one instance a problem of your system's service manager. If you're on a mainstream Linux distro, for example, systemd services have excellent methods for dealing with this. You can always only activate one instance of a service.
/proc/${PID}directory. In this directory,statandstatuscontain base name of process, whileexeis symlink to executable. If you want to be more precise about whether another process was really started from the same executable, then compare target ofexe(/usr/bin/somethingand/usr/local/bin/somethingwould have the same name instatandstatus, but differentexesymlinks).find /proc -ls