in a shell script i need to find out whether a specific application is still running or not. this would be a simple task to do if our application name would not contain any Umlauts (äöüàéè...). how can i reliably "grep" for my process in question?
the shell script gets the application name as parameter, "amétiq siMed Büro.app" in this example. There are several customized copies running at the same time, they are named differently, and the script should check only a specific application (the one it gets via param) and ignore the others.
no hits at all when using grep for the specific app-name (param):
bash> ps ax | grep "amétiq siMed Büro.app"
bash>
too many hits:
bash> ps ax | grep "/[A]pplications/am"
4335 ?? S 5:19.01 /Applications/ame?M^Atiq siMed Bu?M^Hro.app/Contents/MacOS/siMed2
10188 ?? S 0:03.18 /Applications/ame?M^Atiq siMed SUPPORT.app/Contents/MacOS/siMed2
again no hits when trying to manually narrow grep:
bash> ps ax | grep "/[A]pplications/am" | grep "Büro"
bash>
it seems that grep stops working after the position of the first occurrence of an Umlaut character.
i also tried lsof
- no success. any idea what to try next?
running OS X 10.7-10.9
it seems i was too quick in solving my problem using osascript/AppleScript - i was able to filter my process in question in the terminal, but for some reason it didn't work in my script...
so here's what i found to work around the problem: if i cannot reliably "grep" the application path using commands like ps, lsof, ... matching the path my script gets as param, then i simply need to re-generate it with the help of a new process.
again, my problem in short:
my script gets an application path as parameter. this path contains umlauts. furthermore, there are several variants of the application, named differently, several of them might be running at the same time, but the script needs to filter exactly the one it gets as param.
/Applications/amétiq siMed Büro.app/Contents/MacOS/siMed2
using ps, lsof etc. i get garbled output, no matter what locale i had set, it never matched my param:
bash> ps ax | grep "/[A]pplications/am"
70202 ?? S 1:56.38 /Applications/ame?M^Atiq siMed Bu?M^Hro.app/Contents/MacOS/siMed2
75164 ?? U 0:01.75 /Applications/ame?M^Atiq siMed MASTER SN.app/Contents/MacOS/siMed2
grep fails as soon as there's an Umlaut involved in the string:
bash> ps ax | grep "/[A]pplications/amétiq siMed Büro.app"
(empty result)
my solution is to start a "tail &" process on a file existing in the application package, then do a bit of ps, cut and awk, to get the pid of the application i am looking for:
cd "/Applications/amétiq siMed Büro.app" # path the script gets as param
tail -f ./Contents/MacOS/helperfile.txt &
helperpid=$! # pid of tail process
gr="`lsof -p $helperpid | cut -d'/' -f 2- | grep '/Contents/MacOS/' | sed 's:/Contents/MacOS.*$::' | head -1`"
kill $helperpid # helper process no longer needed
finalpid=`lsof | grep "$gr" | grep "app/Contents/MacOS" | awk '{print $2}'`
# $finalpid contains the pid of the process in question
please note that i had to set LC_ALL and LANG to "en_US.UTF-8" (possibly setting one of them might not be required, i did not dig into this any further...).
i know this is only a workaround, it would be much nicer to have a oneliner... at least this solution does the trick for me. thanks again for anyone involved in the discussion of this problem!