Check if process exists, and it's "state"

Hi there,

I am trying to design an applescript that checks whether Time Machine is busy doing a backup. I’ve done a lot of searching, but none of the “solutions” worked in this case.

There are a few parts to this.

1. Check if the process “backupd” exists.
Time Machine will shut dow this process a few minutes after finishing a backup. I cannot get this to work properly.
Just for testing purposes, I tried:

tell application "System Events"
    set ExistingProcs to (name of every process)
end tell
if (ExistingProcs contains "backupd") then
    display alert "Process exists"
    display alert "Process does not exist"
end if

This only seems to work for processes that are run by my user, not by ‘root’, which “backupd” is. It does work for “Safari” and "loginwindow’, for example. Any ideas on how to check for processes owned by root?

2. If it exists, check it’s state

When I run top
in Terminal, the Time Machine process “backupd” shows up, having one of these states (column STATE):
sleeping – when it is not backing up
stuck – when it is backing up
runnning – when it is backing up

So I want my Applescript to check whether or not the STATE of this process is “sleeping”.

Something like:

tell application "System Events"
    set TMActive to the state of process named "backupd"
end tell

does not work. I can’t find a good reference to retrieving properties of a running process.

Can anybody help me? Thanks a lot!

Kind regards

Model: MacBook Pro 17" unibody mid 2009
AppleScript: 2.1
Browser: Safari 531.9
Operating System: Mac OS X (10.6)


all processes listed in System Events are only applications (including AppKit.framework), UNIX processes aren’t listed at all.

This line returns the state of backupd

set backupdState to (do shell script "/bin/ps -arxo state,comm | /usr/bin/grep backupd | /usr/bin/cut -c 1")

Wow, thanks a lot. This even takes care of the first problem. Awesome.