How do I run only one instance of a program

Cameron Simpson cs at zip.com.au
Mon Sep 15 06:13:12 UTC 2008


On 14Sep2008 17:19, Antonio Olivares <olivares14031 at yahoo.com> wrote:
| Dear fellow Fedora users,
| 
| I have a little problem when starting KDE.  I have multiple versions of gkrellm running at the same time, and I configured it to "not run multiple instances".  When I run top, I see several gkrellm(s), how do I prevent this?
| 
| I have run this for a while and it is getting tiring
| 
| $ killall -9 gkrellm

Gah! NEVER reach for "-9" first. It is a signal of last resort.

| $ gkrellm &

The trivial way is this:

  pidfile=$HOME/.grkrellm.pid
  if [ -s "$pidfile" ] && pid=$(<"$pidfile") && kill -0 "$pid" 2>/dev/null
  then
    echo GKrellM already running, pid=$pid, not starting a new one.
  else
    gkrellm &           # start gkrellm
    echo $! >"$pidfile" # save the pid
  fi

It is slightly racy, but not on a scale you are likely to encounter in
typical use.

Frankly, "killall" is a command I try NEVER to go near. It is like
trying to break an egg by letting off a grenade.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Careful and correct use of language is a powerful aid to straight thinking,
for putting into words precisely what we mean necessitates getting our own
minds quite clear on what we mean.      - W.I.B. Beveridge




More information about the fedora-list mailing list