XYMer's Home away from Home

When http://bbs.xlr8yourmac.com is down (i.e. always)
It is currently Fri Jun 23, 2017 4:13 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: shell script
PostPosted: Fri May 23, 2014 6:35 pm 
Offline
User avatar

Joined: Thu May 15, 2008 8:13 pm
Posts: 9269
Location: Caught between the moon and NYC
My wonderful power monopoly destroyed more computer hardware on me, and my replacement external hard drive cases want to put the drives to sleep unless they get regular activity. So I thought I'd create a simple script that would check to see if a file exists then touch it if it does. I can then add it via crontab or whatever I want to kick it off every 2 minutes to avoid having the drives constantly spin down and up.

But I've been spending the entire week helping out at work with our yearly audit and my brain has apparently been turned into a fine mush (so many spreadsheets, so many database reports, so much data to be merged :shock: ), because even this simple task seems beyond me. I've made a script that works, that simply touches the files and exits, but anything that involves checking to see if the file exists (in case I dismount a drive) before touching it isn't working.

This seems pretty straightforward to me. I've inserted some echos so I can track its progress.

#!/bin/bash
touchfile="/Volumes/Volume1/keepalive.touch"
echo "$touchfile"
if [ -f "$touchfile" ]
then
touch "$touchfile"
echo "$touchfile touched."
fi
echo "end if"
touchfile="/Volumes/Volume2/keepalive.touch"
echo "$touchfile"
if [ -f "$touchfile" ]
then
touch "$touchfile"
echo "$touchfile touched."
fi
echo "end if"


And this is the output:
/Volumes/Volume1/keepalive.touch
end if
/Volumes/Volume2/keepalive.touch
end if

So $touchfile gets set, it gets echoed, but it doesn't find $touchfile so it exits the if. I've tried various operands inside the brackets, -e for example, with no change. I initially didn't use the variable at all, just the full path to the file, and that didn't work either.

By comparison, this script works fine:
#!/bin/bash
touch /Volumes/Volume1/keepalive.touch
touch /Volumes/Volume2/keepalive.touch

Ugh. I think I'm going to bed early tonight.


Oh, and this is running on 10.5.8, but I figured a bash script is a bash script. I'm also kicking it off via crontab, in case that matters.

*/2 * * * * ~/KeepAlive.sh


Top
 Profile  
 
 Post subject: Re: shell script
PostPosted: Fri May 23, 2014 9:20 pm 
Offline
Benevolent Dictator
User avatar

Joined: Mon Apr 21, 2008 2:03 am
Posts: 14481
Any help hereā€¦

http://hints.macworld.com/article.php?s ... 4160033481

http://hints.macworld.com/article.php?s ... 6190817357


Top
 Profile  
 
 Post subject: Re: shell script
PostPosted: Sun May 25, 2014 7:56 pm 
Offline
User avatar

Joined: Thu May 15, 2008 8:13 pm
Posts: 9269
Location: Caught between the moon and NYC
Not really.

It appears my problem is caused by volumes with spaces in their name. Once I removed the space from one of them the if branching started working.

Looks like I can just nix the escaping (Sample\ Volume\ Name) and just wrap the entire thing in quotes. Which I was already doing, because it spit out errors if I didn't... once wrapped in quotes the escaping became superfluous and would stop it from matching.

Now I need to figure out launchctl to avoid crontab spitting errors into the system log. I actually had a good plist before but trashed it since the script wasn't working, and the one I came up with from your link doesn't work.

At least I know why the script wasn't working though, that had me a little baffled.

For what it's worth, the script as written at the start of this thread does work, because my example volume names don't have spaces in them. If you replace the volume names with ones that contain spaces, just type it normally, don't use backslashes. You can edit or comment out (#) the echo lines, since they were sanity checking on my part so I could see where the script was stepping.


Top
 Profile  
 
 Post subject: Re: shell script
PostPosted: Sun May 25, 2014 8:22 pm 
Offline
User avatar

Joined: Thu May 15, 2008 8:13 pm
Posts: 9269
Location: Caught between the moon and NYC
I managed to work out the plist from my original source:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<false/>
<key>Label</key>
<string>net.monkey.touch</string>
<key>Program</key>
<string>/Users/me/KeepAlive2.sh</string>
<key>StartInterval</key>
<integer>120</integer>
<key>RunAtLoad</key>
<true />
<key>StandardErrorPath</key>
<string>/dev/null</string>
<key>StandardOutPath</key>
<string>/dev/null</string>
</dict>
</plist>


Top
 Profile  
 
 Post subject: Re: shell script
PostPosted: Sun May 25, 2014 8:44 pm 
Offline
Benevolent Dictator
User avatar

Joined: Mon Apr 21, 2008 2:03 am
Posts: 14481
Great work, thanks for the report! :)


Top
 Profile  
 
 Post subject: Re: shell script
PostPosted: Sun May 25, 2014 10:24 pm 
Offline
User avatar

Joined: Thu May 15, 2008 8:13 pm
Posts: 9269
Location: Caught between the moon and NYC
It all makes perfect sense when you stop and think about it for a second, but I always escape volume names instead of using quotes, and, well, that didn't work - the if branch treated all spaces, even escaped spaces, as a break between operands. Add quotes, remove the escape characters, and voila, it works fine.

Also, anyone using this script/plist would need to manually create the keepalive.touch file in the root of each HD. A simple "touch /Volumes/Volumename/keepalive.touch" is all that's needed to create the (0 byte) file. The script/plist looks for the presence of this file and won't create it if it's missing, because that's my safety net for dismounted volumes - if the file isn't present, the volume isn't mounted, so skip creating a file under /Volumes.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group