Help: Viewing live script output or log file contents.

I am developing an application that runs a number of different shell scripts and would like the output of those scripts to be visible in the application in real-time or something close to it. I’m a bit frustrated as I have spent a good amount of time looking for a solution to the issue without coming across any ideas. In the meantime, I have set the shell scripts to output to a log file and display the log file as a web view before and after the scripts are run. I’m not satisfied with this implementation. My scripts use commands like “softwareupdate”, “curl” and “hdiutil create” that can take a long time to run and have dynamic output. Any help on the best way to approach this would be much appreciated.

One way to do it is to use NSTask and a file handle and readInBackgroundAndNotify then retrieve via notification center. The idea is to pipe the output to your text field or view. There are many tutorials - just found this one

http://macosx.com/forums/software-programming-web-scripting/4522-better-way-read-nstask.html

Other way is to launch your process with your output to log file as you have. run it in background and set up an NStimer to read in from the file every second or less and update your UI that way. Either way you have to parse the output if you want to show the last line always updating - that way it appears in live time, though the whole log won’t be visible. You can look at the log itself updating in real time of course.

Rob

Thanks for the reply robdut. I’ve tested out both solutions and run into issues with each.

When using the NSTask method commands can’t be run with elevated privileges. The “softwareupdate” command requires elevated privileges, as well as “hdiutil create” for folders with restricted permissions. I realize I could probably follow Apple’s best practices and set up an authorized helper to get around this but that’s a bit beyond my current skills. My working solution to get around elevation has been to collect the credentials for an authenticated user at the start of the app and use them when executing “with administrator privileges”.

My application becomes unresponsive during the “do shell script” execution with a spinning beachball. While the app is unresponsive, timers don’t trigger and the interface never gets updated.

To progress any further I need a way to elevate a NSTask or to run “do shell script” in such a way that the application doesn’t hang.

Elevated permissions? You didn’t say that - but this is how I do it in a very simplified way…

run your do shell script in background with the “&” so it won’t interfere with the UI

property NSTimer : class "NSTimer" of current application

property filePathTextField:missing value
property pid:""

on runRsync()
set my pid to do shell script "rsync args /folder/to/backup /Volumes/BackupFolder &> /output.log & echo $!" with administrator privileges

set my rsyncTimer to NSTimer's scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(1, me, "loopCheck:", missing value, true)

end runRsync

on loopCheck_(sender)
if  isProcess_runningAtPid_("rsync",my pid) then  -- I have a low level class for doing this called from isProcess_runningAtPid_
   set outputText to do shell script "tail -n5 /output.log" as text 
   --parse outputText how you want it
    filePathTextField's setStringValue_(outputText)
else
   rsyncTimer's inavlidate()
   --clean up...
end  loopCheck_

Now if by any chance you are on OS 10.9.2, there is a problem with this. See my previous post:
http://macscripter.net/viewtopic.php?id=42225
The UI freezes up with beach ball. I have tried for days and have found no cause, no errors, nor reason for this to happen. I think it has something to do with the authorization doing something with the window server as there are errors about window server not being able to bring my app to the front. So you are out of luck. The shell script does run by the way but doesn’t return control to the main script till after it finishes, at which time the logging will show up.

I have since changed my authorization methods to use SMBJobless ( not for the faint hearted! ) and separate OBJc classes but would like to know if anyone has got do shell script with admin privileges to work on 10.9.2. I am not talking about an applescript but an app with a UI that needs focus.

Rob

I tried using performSelectorInBackground_withObject_ on a whim to see if back-grounding the execution would help but still get the beachball.