Bash script error

Discussion in 'macOS' started by opusthe2nd, Feb 5, 2008.

  1. opusthe2nd macrumors 6502

    opusthe2nd

    Joined:
    Sep 5, 2005
    #1
    This is it:

    #! /bin/bash
    cd /Users/opus/Documents/escan/
    rm mwav.*
    wget http://update3.mwti.net/download/tools/mwav.exe


    I can run it all manually, but when it runs with cron I get:

    /Users/opus/Documents/escan/mwave.sh: line 4: wget: command not found.

    I've tried it with full path and get same error. /usr/sbin/wget

    What am I missing here?
     
  2. jamesarm97 macrumors 65816

    Joined:
    Sep 29, 2006
    #2
    You have to download and compile wget for os x. I had the same problem.

    - James

    Edit:
    You can try this, but it is old:
    http://www.versiontracker.com/dyn/moreinfo/macosx/13923&vid=59439

    or


    Get the source from ftp://ftp.gnu.org/pub/gnu/wget/wget-1.9.tar.gz
    Unpack the tarball: tar xzf wget-1.9.tar.gz
    This creates a folder named wget-1.9. Change the working folder to it: cd wget-1.9/
    Configure the build: ./configure
    Build: make
    Install: sudo make install
    The wget binary should now be in your /usr/local/bin folder.
     
  3. opusthe2nd thread starter macrumors 6502

    opusthe2nd

    Joined:
    Sep 5, 2005
    #3
    Ha...well of course I have already done that. Like I said, if I run the command manually it works.

    This is Leopard, I forgot to mention. There is no /usr/local/bin.
     
  4. tyr2 macrumors 6502a

    tyr2

    Joined:
    May 6, 2006
    Location:
    Leeds, UK
    #4
    When you run scripts from cron they generally don't have a PATH as it doesn't get the environment initialized in the same way as when you're in a shell.

    Type 'which wget' to get the path to wget. Then put the full path in your script, it should then work fine.
     
  5. opusthe2nd thread starter macrumors 6502

    opusthe2nd

    Joined:
    Sep 5, 2005
    #5
    I have and it doesnt. If I put it in manually it will, but the script, when run with cron, spits out that error. wget not found. If I run the script manually, it works fine. I am doing this all as user and not sudo. I shouldnt have to put it in the su crontab....I dont think.
     
  6. Mr.Texor macrumors regular

    Joined:
    Apr 20, 2007
    #6
    have you tried curl? it's included on all macs and it should do the job..
    It would look something like this:

    Code:
    ! /bin/bash
    cd /Users/opus/Documents/escan/
    rm mwav.*
    curl -L http://update3.mwti.net/download/tools/mwav.exe >> /dev/null
    I added the /dev/null so you dont get mails with the outputs..
     
  7. opusthe2nd thread starter macrumors 6502

    opusthe2nd

    Joined:
    Sep 5, 2005
    #7
  8. Mr.Texor macrumors regular

    Joined:
    Apr 20, 2007
    #8
    I'm sorry, I'm stupid.. it should be
    -O not -L.. so

    Code:
    curl -O http://update3.mwti.net/download/tools/mwav.exe
    the -O means to write the output of what ever you download to a local file name that is the same as the remote file name..

    And there's nothing wrong with wget. In fact, I prefer wget... but, if you had too many problems with wget, curl is built in.... :cool:




    edit: you could use the -s option to make it silent too..
     
  9. dvd macrumors regular

    dvd

    Joined:
    Oct 12, 2007
    Location:
    Massachusetts
    #9
    I'd second the curl recommendation. I wasn't able to reproduce your original error.. I received the 'wget: command not found' error like you did until I provided the full path.. after that the script worked fine. Perhaps a typo?

    If you still can't get it to work after trying curl, please post a copy of your script that contains the full path along with the output from:

    which wget

    When you say it works if you run it manually, do you mean if you run the script from the command line, or if you run each command one at a time from the command line?

    You could also try explicitly defining your PATH at the top of your script. Type the following to grab the current PATH:

    env | grep ^PATH

    which should give you something like:

    PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

    and add the PATH= line as line 2 of your script.

    The default path that is used in the cron environment is very minimal:

    PATH=/usr/bin:/bin
     
  10. opusthe2nd thread starter macrumors 6502

    opusthe2nd

    Joined:
    Sep 5, 2005
    #10
    GNU Wget 1.9+cvs-dev

    I'll change the path and see what happens. I find this very odd.
     
  11. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #11
    Maybe when it runs as a cron job, it doesn't have rights to access wget.

    Determine what user the cron jon runs as and make sure that user can execute wget.
     
  12. opusthe2nd thread starter macrumors 6502

    opusthe2nd

    Joined:
    Sep 5, 2005
    #12
    I would think cron is running as user, seeing thats how I created it. It seems to be working now with the PATH changed as above.

    I'm a FBSD guy that has moved over to OS X, somethings seem to be different.
     
  13. dvd macrumors regular

    dvd

    Joined:
    Oct 12, 2007
    Location:
    Massachusetts
    #13
    Cool. This means the wget command is in one of those additional directories, and perhaps not where you originally thought it was.. you're good to go, but if you want to bottom out on this:

    When I said 'which wget', I meant run the 'which wget' command and tell me what it comes back with. Should be something like /usr/local/bin/wget. Then do a 'file /usr/local/bin/wget' to make sure it's an executable and not yet another script that may have issues itself.
     
  14. opusthe2nd thread starter macrumors 6502

    opusthe2nd

    Joined:
    Sep 5, 2005
    #14
    Wget did not work after all. "which wget" = /usr/sbin/wget. Which is what I have been using. I tried it with curl and that worked.

    My script runs, yet it cant find wget....even though the path is there. Still odd. It should run as user, as I have run it as user.

    Its got to be solely a cron issue. I have run the script manually, I have run each line in the script manually, etc. I could sudo crontab -e but I shouldnt have to.
     
  15. dvd macrumors regular

    dvd

    Joined:
    Oct 12, 2007
    Location:
    Massachusetts
  16. opusthe2nd thread starter macrumors 6502

    opusthe2nd

    Joined:
    Sep 5, 2005
  17. dvd macrumors regular

    dvd

    Joined:
    Oct 12, 2007
    Location:
    Massachusetts
    #17
    Good, so it's not a broken script itself, at least.

    within cron, your path is preset to /usr/bin:/bin

    Try this at the command line to emulate that environment:

    your-prompt$ PATH=/usr/bin:/bin yourscriptname

    This will force your script to run with that PATH, and should give the same error it does in cron. Also, please post your entire script as it looks when it fails using the above syntax.
     
  18. opusthe2nd thread starter macrumors 6502

    opusthe2nd

    Joined:
    Sep 5, 2005
    #18
    I ran:

    PATH=/usr/bin:/bin /Users/opus/Documents/escan/mwave.sh

    and it worked.

    Maybe I can put wget in /usr/bin?
     
  19. dvd macrumors regular

    dvd

    Joined:
    Oct 12, 2007
    Location:
    Massachusetts
    #19
    That would fix it, too.

    Just to satisfy my curiosity, could you post the script again as it is right now?
     
  20. opusthe2nd thread starter macrumors 6502

    opusthe2nd

    Joined:
    Sep 5, 2005
    #20
  21. dvd macrumors regular

    dvd

    Joined:
    Oct 12, 2007
    Location:
    Massachusetts
    #21
    sounds like you've got it working. Still a mystery to me, but oh well. Only other thing I can think of is an alias to wget or something in your .bashrc that works for your interactive shell but not for your non-interactive shell in cron.
     
  22. opusthe2nd thread starter macrumors 6502

    opusthe2nd

    Joined:
    Sep 5, 2005
    #22
    Yeah, its really weird. Makes no sense. Thanks for all the input though. :)
     

Share This Page