Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


How can I run an app if another app is running?

I need to run a Unix application if a different application is running, from within a shell script. How do I do this?


Dave's Answer:

If you know the name of the application and you are sure that only instantiation of it can be running at a given time, then the solution is pretty simple:

running="$(ps -aux | grep yourappname)"

if [ -n "$running" ] ; then
  launch your secondary application
fi

Obviously, you'd need to change 'yourappname' to the appropriate name of the app you seek, but that's all you need to do.

If you could have more than one version of the process running or otherwise don't have an easy way to identify it uniquely, then you have a greater challenge. For example, if you wanted to check for the Apache Web server (process name "httpd") but wanted to make sure that you weren't seeing a child process but the parent, then you'd also need to take into account the parent process ID (ppid) because the ppid of child processes is the parent process (which makes sense!): in the case of child Apache processes, their ppid's are the process ID (pid) of the parent Apache process.

And that's exactly how you do it. Use a flag like '-j' to get the pid and ppid, use grep to pull out the matching processes by name, then check the ppid to identify which has the odd ppid out. Let me show you. Here's a set of processes shown in this format:

$ ps -jax | grep http
root    6706     1     0 Ss    ??    0:03.43 /usr/local/apache/bin/httpsd
www     6707  6706     0 I     ??    0:11.91 /usr/local/apache/bin/httpsd
www     6708  6706     0 S     ??    0:13.22 /usr/local/apache/bin/httpsd
www     6709  6706     0 S     ??    0:13.46 /usr/local/apache/bin/httpsd
www     6710  6706     0 I     ??    0:13.82 /usr/local/apache/bin/httpsd
www     6711  6706     0 S     ??    0:12.36 /usr/local/apache/bin/httpsd
www    19003  6706     0 S     ??    0:00.09 /usr/local/apache/bin/httpsd
www    19046  6706     0 I     ??    0:00.01 /usr/local/apache/bin/httpsd
www    19429  6706     0 S     ??    0:00.02 /usr/local/apache/bin/httpsd
www    19432  6706     0 S     ??    0:00.04 /usr/local/apache/bin/httpsd
www    19531  6706     0 I     ??    0:00.00 /usr/local/apache/bin/httpsd
www    19612  6706     0 S     ??    0:00.01 /usr/local/apache/bin/httpsd
www    19615  6706     0 S     ??    0:00.00 /usr/local/apache/bin/httpsd
www    19616  6706     0 S     ??    0:00.00 /usr/local/apache/bin/httpsd
www    19622  6706     0 S     ??    0:00.01 /usr/local/apache/bin/httpsd
www    19624  6706     0 Z     ??    0:00.00  (httpsd)

Notice that the second column is the process ID and the third is the parent process id: as you can see, the parent process is 6706 in this instance.

You can identify this in your script (I've written about it here before) and then screen for a process by ID rather than by name (use the '-p process' flag to ps) then use the same basic test shown earlier to launch your secondary application, as needed.

Hope that helps you out!


More Useful Unix and Linux Help Articles:
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
✔   Can I script renaming files based on an XML data map?
I have a folder full of files which are named with four digits and a file extension e.g. 0312.file and an XML-file describing...
✔   Test for valid numbers in a Bash shell script?
In a different discussion on this site [see Redirecting input in a shell script] a visitor commented that "I was too busy trying...
✔   Review: iSSH for the iPad/iPhone
If you're running an online business like I am, there are times when you need to connect and log in to the server...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  





Categorized: Shell Script Programming , Unix and Linux Help   (Article 5229, Written by )
Tagged: linux, shell script programming, unix
Previous: Are there any good collaborative editing tools online?
Next: How can I avoid having Apple kill my shareware app?




Reader Comments To Date: 2

Ray said, on January 9, 2006 2:22 PM:

The first simple solution won't work.

On all Unix boxes that I have ever used,

running="$(ps -aux | grep yourappname)"

will match the grep in addition to yourappname, running will always be non-zero length and whatever is in the if will always run.

There are a could of ways of dealing with this. You can either pipe the grep into a second grep -v

running="$(ps -aux | grep yourappname | grep -v grep)"

or use brackets on the appname

running="$(ps -aux | grep [y]ourappname)"

Both of these will remove the grep process from running, but
either case will fail if there is also an application called yourappnamemonitor or such that would cause the grep to succeed even if the process that you were interested in wasn't running.

monsieur said, on May 17, 2006 5:00 AM:

use pgrep instead :

if pgrep name >/dev/null ; then echo "running";done

even better is to launch the process and save the PID so you don't have to guess, like this :

program &
mypid=$!

then to check if the process is running, you can use:
if kill -0 $mypid ; then echo "running"; done

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!
Powered By
Linux Journal: Free Issue!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.