0

So I am designing a feature to redirect log file if there are multiple processes of this very program are running.

I guess this requires me to somehow get to know whether there exists yet unfinished same program(executable?). I wonder is this achievable within user mode? How?

5
  • 2
    Solutions would be specific to operating systems. Do you use GNU/Linux? If yes, then you can find some informations about any process in /proc/${PID} directory. In this directory, stat and status contain base name of process, while exe is symlink to executable. If you want to be more precise about whether another process was really started from the same executable, then compare target of exe (/usr/bin/something and /usr/local/bin/something would have the same name in stat and status, but different exe symlinks). Commented Jul 29 at 1:48
  • @Arfrever many thanks mate, I am gonna follow and check. Commented Jul 29 at 2:16
  • 2
    If you're just trying to avoid log-file collision, why not just put the PID (or the date & time, e.g. YYYYMMDD-HHMMSS) and maybe a counter in the filename? There'd still be a tiny risk of collision, but it's easier than trawling /proc, and provides more consistency in file naming that having a special case for when other processes with the same name are running. And solving the collision would also be easy - just check to see if the file already exists. if it does, increment the counter and try again. Commented Jul 29 at 5:16
  • BTW, unless it's running as root, your program won't have permission to scan all /proc/PID directories, just a subset. There's also the minor annoyance of short-lived processes - by the time you get a list of all PIDs and start processing them, some will have terminated and some new ones might have started. You can see this most easily by running something like find /proc -ls Commented Jul 29 at 5:20
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Jul 29 at 6:02

1 Answer 1

5

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.

2
  • Indeed seems promising, I'll go see whether I can create an extra tmp file to achieve it. Commented Jul 29 at 12:20
  • 3
    @PkDrew again, I'd encourge you to not do that. Rather than protecting from two instances of your program running (which would fail anyways, in many cases, because one might simply not run in 100% the same filesystem namespace and have its own temporary directory – generally a very good idea for security reasons). Let a service manager handle running exactly one instance, and if you need to protect multiple instances from accessing the same resource concurrently, build a lock that protects the actual resource, not the whole program. Commented Jul 29 at 13:22

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.