Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General

Thread Tools Rate Thread Display Modes
Old 03-29-2012, 02:12 PM   #1
Join Date: Jul 2002
Location: FLA
Posts: 49
Cron weirdness

Some of my shell scripts don't launch from cron. It's weird.
First off all the scripts work fine when run via terminal.

The script also runs fine when it's run through cron this way: /user/bin/open /path/script.command
But fails when input in cron as /path/script.sh
(Both the .sh and .command work from terminal)

I have about 1/2 a dozen cron jobs running as /path/script.sh SOME RUN FROM THE SAME DIRECTORY! They call and execute from within cron without flaw.

This one script is driving me nuts. I can get it to work by changing it to .command but I want to understand WHY it's not working.

Here is a copy of the script (It's nothing spectacular, I don't really rely on it or need it per se. I just got a bug up my a$$ and decided I wanted it displayed on the desktop (geek tool) for the next few weeks till I find a good deal.)

cd ~/Pictures/

if [ -e ~/Pictures/223_556_reloadable.png ]
then mv 223_556_reloadable.png 223_556_reloadablebk.png

# Image location http://gun-deals.com/dailyPrices/223_556_reloadable.png
wgetapp=$(which wget)
$wgetapp -c http://gun-deals.com/dailyPrices/223_556_reloadable.png

if [ -e ~/Pictures/223_556_reloadable.png ]
then rm ~/Pictures/223_556_reloadablebk.png
else mv ~/Pictures/223_556_reloadablebk.png ~/Pictures/223_556_reloadable.png; exit 0

/opt/local/bin/convert ~/Pictures/223_556_reloadable.png -transparent white -negate ~/Pictures/223_556_reloadable.png

exit 0
Any clues?
MVasilakis is offline   Reply With Quote
Old 03-29-2012, 09:59 PM   #2
Site Admin
Join Date: Jan 2002
Location: Montreal
Posts: 32,459
The usual cause of this kind of problem is that the execution environment for cron scripts is not the same as that of the Terminal. In particular, the PATH variable is not the same.
So the usual recommendation is to always use full paths for all executables and files referred to in your scripts. And that means to not use ~ for the home folder.
Instead hard-code the path, or (after checking that this works) use $HOME
hayne is offline   Reply With Quote
Old 03-31-2012, 05:12 AM   #3
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,191
Originally Posted by hayne
And that means to not use ~ for the home folder.
Instead hard-code the path, or (after checking that this works) use $HOME

~ is just a convenient shortcut for $HOME. If $HOME works, so will ~.

Try it for yourself:

echo ~/xyzzy

~name is, of course, a different animal. That does a dynamic lookup of name's home folder.
ganbustein is offline   Reply With Quote
Old 04-20-2012, 02:29 PM   #4
Triple-A Player
Join Date: Jun 2002
Posts: 63
exactly. cron referring to ~ is not going to work as expected when run from your user account. full path.
neuralstatic is offline   Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -5. The time now is 03:06 AM.

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.