The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - General (http://hintsforums.macworld.com/forumdisplay.php?f=16)
-   -   launchd WatchPaths not triggered by CGI file change (http://hintsforums.macworld.com/showthread.php?t=87273)

hsthompson 03-14-2008 07:24 PM

Should it be executable or anything?

tlarkin 03-14-2008 07:28 PM

Quote:

Originally Posted by hsthompson (Post 458170)
Should it be executable or anything?

no, it doesn't need to be executable as far as I can tell. The documentation I have read never indicated it must be executable, but it must be in the standard plist format of

domain.mycompany.plist

for example

mark hunte 03-15-2008 06:13 AM

Quote:

Originally Posted by tlarkin (Post 458152)
if his paths are right Hayne, the ondemand switch in the plist should keep the launchd item always monitoring that path.


I notice for 10.5 that the onDemand has been deprecated
Quote:

OnDemand <boolean>
This key was used in Mac OS X 10.4 to control whether a job was kept
alive or not. The default was true. This key has been deprecated and
replaced in Mac OS X 10.5 with the more powerful KeepAlive option.
KeepAlive
Quote:

KeepAlive <boolean or dictionary of stuff>
This optional key is used to control whether your job is to be kept con-tinuously continuously
tinuously running or to let demand and conditions control the invocation.
The default is false and therefore only demand will start the job. The
value may be set to true to unconditionally keep the job alive. Alterna-tively, Alternatively,
tively, a dictionary of conditions may be specified to selectively con-trol control
trol whether launchd keeps a job alive or not. If multiple keys are pro-vided, provided,
vided, launchd ORs them, thus providing maximum flexibility to the job to
refine the logic and stall if necessary. If launchd finds no reason to
restart the job, it falls back on demand based invocation. Jobs that
exit quickly and frequently when configured to be kept alive will be
throttled to converve system resources

mark hunte 03-15-2008 06:43 AM

I am not familiar with CGI scripts, but I get the impression that the script is modifying a file rather than moving files, Is this correct?

When I test a folder that is being watched, it will trigger if a file is moved To or From its path, but will not trigger if a file has been updated/modified

This appears to be the intended behaviour for watchPaths.

*edit*
Maybe adding something like this to the end of the script..
Code:

mv /Users/username/Desktop/foo_folder/foo.txt /Users/username/Desktop/foo_folder/mvFolder/foo.txt;mv /Users/username/Desktop/foo_folder/mvFolder/foo.txt /Users/username/Desktop/foo_folder/foo.txt

tw 03-16-2008 04:17 PM

Quote:

Originally Posted by hsthompson (Post 458159)
I think the one I logged was set up in /Library/LaunchDaemons.

I just tried editing that file again through the CGI. It worked once, triggering the rsync script; then it failed to trigger it subsequent times. What does it mean?!

launchd will automatically terminate a task if it runs into errors (certain kinds of errors, anyway). since it worked once, this makes me think there's something buggy in your shellscript. is it short enough to post here?

tw 03-16-2008 04:22 PM

Quote:

Originally Posted by mark hunte (Post 458246)
When I test a folder that is being watched, it will trigger if a file is moved To or From its path, but will not trigger if a file has been updated/modified...

launchd is supposed to pick up on any change to a file path, including changes to files (in fact, I've used that feature before...). now it's possible that watchpath that's set to watch a folder might not pick up changes to a file within that folder - I've never tried that before. is that what you were doing?

Hal Itosis 03-16-2008 05:04 PM

There's also a binary at /bin/wait4path which should provide the same (or similar) action. [see man wait4path]

From a very limited test last night, mine seems to stay asleep forever (well, longer than I felt like waiting).

-HI-

hsthompson 03-17-2008 01:21 PM

Quote:

Originally Posted by tw (Post 458476)
launchd will automatically terminate a task if it runs into errors (certain kinds of errors, anyway). since it worked once, this makes me think there's something buggy in your shellscript. is it short enough to post here?

OK.

Code:

#! /bin/sh
rsync -avz --delete-after ~/Desktop/testfolder myserverdomain@ssh.myserverdomain.com:./public_html/


hsthompson 03-17-2008 01:25 PM

Quote:

Originally Posted by mark hunte (Post 458246)
I am not familiar with CGI scripts, but I get the impression that the script is modifying a file rather than moving files, Is this correct?

When I test a folder that is being watched, it will trigger if a file is moved To or From its path, but will not trigger if a file has been updated/modified

This appears to be the intended behaviour for watchPaths.

*edit*
Maybe adding something like this to the end of the script..
Code:

mv /Users/username/Desktop/foo_folder/foo.txt /Users/username/Desktop/foo_folder/mvFolder/foo.txt;mv /Users/username/Desktop/foo_folder/mvFolder/foo.txt /Users/username/Desktop/foo_folder/foo.txt

The CGI script is written in Runtime Revolution, which I'm not very familiar with. The author of the CGI script told me running shell commands from within that environment was not possible. I don't know enough about it to tell.

mark hunte 03-17-2008 02:48 PM

I was thinking, maybe an approach could be to to have one watchpath to look at the folder. when a new file is added it writes the path of the new file to a second watchpath which watches the files.
The second one should then trigger on changes the the file.

I have tested parts of this idea and it seems it would work.
The write part is the easy bit,

Code:

defaults  write ~/Library/LaunchAgents/watchpath  "WatchPaths" -array-add '<string>/Users/username/Desktop/foo_folder/foo.file</string>'
But still working on getting the list of items in the folder and then comparing to what is already in the second watchpath plist

hsthompson 03-17-2008 03:00 PM

mark -- that's awesome. I tested watching the individual file, rather than the folder as I had been, and it does trigger launchd when the CGI edits the file. Keep me posted, and I'll research a solution, too.

mark hunte 03-17-2008 04:04 PM

This is the code I have been working on, to add into the (folder) watchpath plist.

Code:

#!/bin/bash
##Bash's Internal File Separator (IFS). Normally the separator is any of space, tab, or newline. 'Find' (and 'ls' ) produce new-line separated lists,
#so changing the IFS to just new-line does the trick to cope with the spaces in file names
IFS="
"
#search for files in the given folder, do not use recursive  beyond this level
thenames=`find /Users/username/Desktop/foo_folder  -maxdepth 1 -type f ! -name '.*'`

#for each file found, use grep to see if a matching path is found in the (folder) watchpath plist
#return 0 for no match, 1 for a match
for file in $thenames; do
 thegrep=`/usr/bin/grep -ic "$file" ~/Library/LaunchAgents/watchpath.plist`
#####for testing in terminal, you can add echo $thegrep

#check to see if match was made, if yes, do nothing. If No, '0' , then write the path to the (file) watchpath plist
if [ "$thegrep" = "0" ]; then
 /usr/bin/defaults  write ~/Library/LaunchAgents/watchpath "WatchPaths" -array-add \<string\>"$file"\<\/string\>
######for testing in terminal, you can add echo /usr/bin/defaults  write ~/Library/LaunchAgents/watchpath "WatchPaths" -array-add \<string\>"$file"\<\/string\

fi
done

Need to stop it adding hidden files like the .DS_Store, which should not be that hard.

*Note, I only have a small amount of knowledge of shell.

**edit 1**
Changed the find part to ignore hidden files

**edit 2**
added to the script, to cope with spaces in filenames.

Also I have now tested this and it works.

The folder watching watchpath plist calls this shell script when a new file is added.
The shell in turn checks against the paths in the file watching watchpath plist and adds any that are not already in it.

hsthompson 03-20-2008 01:01 PM

Looks good. I'll set set up a sandbox account and try it out later.

Thanks for your help, everybody!


All times are GMT -5. The time now is 05:44 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.