check if a process exists

I’m trying to right a simple little script that will check to see if my irc server daemon is running. it shows up as ircd in the process viewer. The idea is to save this script as a stay open run only application so that if for what ever reason my server goes down while I’m at work the script notices it and relaunches it. The part of the script that launches the daemon works fine but the part that is suposed to check to see if it is already running does not. I’m sure this is simple. I’ve probably just got it slightly wrong I don’t get why it’s not working. Here’s what I have so far.

property ircdNotRunning : false

on idle {}
	tell application "System Events"
		if process named "ircd" exists then
			display dialog "ircd is running"
			set ircdNotRunning to false
		else
			set ircdNotRunning to true
		end if
	end tell
	if ircdNotRunning then
		tell application "Terminal"
			do script "cd /usr/local/ircd/bin" in window 1
		end tell
		
		tell application "Terminal"
			do script "./ircd" in window 1
		end tell
	end if
	return 180
end idle

Replace:

if process named “ircd” exists then

with:

if “ircd” is in name of processes then

Well that didn’t work either. I tried it your way and every other variation of that statement that I could think of and nothing works. Oh my god I’m so at my wits end. I even tried this below:

property ircdNotRunning : false

on idle {}
	tell application "System Events"
		set n to count of processes
		repeat with i from 1 to n
			if ((name of process i) as string) is (("ircd") as string) then
				set ircdNotRunning to false
			else
				set ircdNotRunning to true
			end if
		end repeat
		if ircdNotRunning is false then
			display dialog "ircd is running" buttons {"OK"} default button 1 giving up after 3
		else if ircdNotRunning then
			display dialog "ircd is NOT running" buttons {"OK"} default button 1 giving up after 3
		end if
	end tell
	return 5
end idle

I’d run that with the process viewer open and I can see that the process “ircd” is running but the script keeps saying it’s not. What the heck?! Anybody got any ideas? :?:

Hi PolarBear,

Sometimes, the name of the process you get is not the same as the name that is displayed. Try this with the app running:

tell app “System Events”
name of every process whose visible is true
end tell

Look at the result. Don’t just look for “irrcl”, but also for something similar. There was a similar name thing with an app called WeatherPop a while back where the naming of the process was really wierd. I forget what the solution was, but it might come back or is in one of my scripts.

Edit: oops, editted the statement to get the name.
Editted again: oops, and take out the visible is true part.

gl,

interesting. I tried,

on run {}
	tell application "System Events"
		get the name of every process
	end tell
end run

and the list of processes it returned seems much shorter than the list that I see in the process viewer. Much was excluded including “ircd” or anything like “ircd”. hmmm

I still can’t figure this out. Does nobody have an idea why “System events” would only be aware of 25 processes while the process viewer is aware of 58?

Well, for any body who has this problem in the future. I got a hold of a friend of mine that has a degree in computer science and I haven’t been able to stump this guy yet. Any way he said that the “System Events” app only has the processes from applications that have GUIs. He gave the following peice of code which works fo me. So anybody needing to check the status of a process that isn’t listed with “System Events” can use this and just swap out “ircd” for the name of what ever process your looking for.

on run {}
	
	if (do shell script "ps ax") contains "ircd" then
		set ircdRunning to true
		return ircdRunning
	else
		set ircdRunning to false
		return ircdRunning
	end if
end run

Actually, you should also use the “c” parameter with “ps” so you just get the process names, not the full paths which may be truncated. So, that can be boiled down to:

set ircdRunning to process_running("ircd")

on process_running(process_name)
	return (do shell script "ps axc") contains process_name
end process_running

Jon

hehe Where where you yesterday?