Bash maintenance script help

Discussion in 'Mac OS X 10.3 (Panther) Discussion' started by mkrishnan, Jan 31, 2005.

  1. mkrishnan Moderator emeritus

    mkrishnan

    Joined:
    Jan 9, 2004
    Location:
    Grand Rapids, MI, USA
    #1
    Hi all, I am trying to get a script working that I can execute from command line every one or two weeks to take care of a lot of maintenance issues at once, systematically. Right now, the script looks like this:

    Code:
    COMMAND_LINE_INSTALL=1 export COMMAND_LINE_INSTALL
    
    echo "Now running system maintenance tasks. This computer will shut down
    after completion of all tasks."
    echo ""
    PERIODIC DAILY
    PERIODIC WEEKLY
    PERIODIC MONTHLY
    diskutil repairpermissions /
    update_prebinding -root / -force
    softwareupdate -i -a
    shutdown -h now
    The script works fine (I run it via sudo), for the most part. I use this command line:

    sudo ./cleanup >logs/20050130.log (or whatever the date is)

    One issue is that right now, update_prebinding does not seem to provide an output to stdout that gets piped into the log file. Is there any way to force it to do this? Or is there a log file somewhere, the contents of which I can cat into my log file? I didn't see anything in /var/log that looked like it belonged. But I know that there is output in the console normally when doing an update_prebinding.

    Thanks anyone for help! :)
     
  2. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #2
    Maybe redirecting the standard error will help. See Link.

    I read on these forums somewhere that "PERIODIC MONTHLY" and "periodic monthly" aren't quite the same. The second syntax is the one that's actually defined in the system. The first syntax is somehow mapped to the second one, but the mapping isn't complete. The same thing applies with the other periodic commands. Do you know anything about this?
     
  3. mkrishnan thread starter Moderator emeritus

    mkrishnan

    Joined:
    Jan 9, 2004
    Location:
    Grand Rapids, MI, USA
    #3
    Thanks for the lead!

    Oh, yes, I do, know the explanation for the periodic issue. I have to defer thanks to someone else on MR who explained it to me. MacOS attempts to remove case sensitivity from everything. The commands are actually defined as periodic daily etc, but to MacOS, periodic = PERIODIC. Unlike in most other Nixes. And it also interprets the parameter daily = DAILY, except, that there's a file somewhere which redirects output of daily to a log file in the /var/log, but only "daily" and not "DAILY." So there are cleaner ways to do it, but if you type periodic daily, you will not get output to stdout, and if you type PERIODIC DAILY, you will.... :rolleyes:

    So, with regard to redirecting stderr, can I use > to redirect stdout and 2> to redirect stderr to the same file? Is the correct syntax something like

    sudo ./cleanup > 2> logs/20050130.log

    or

    sudo ./cleanup > logs/20050130.log 2> logs/20050130.log

    ?
     
  4. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #4
    Unfortunately, that doesn't work. What you have to do instead is tell BASH to send stderr to the same place as stdout, ie...

    sudo ./cleanup > logs/20050130.log 2>&1

    This way, both the output and errors get recorded. Otherwise, the errors will overwrite the output, which probably isn't what you had in mind.
     
  5. mkrishnan thread starter Moderator emeritus

    mkrishnan

    Joined:
    Jan 9, 2004
    Location:
    Grand Rapids, MI, USA
    #5
    Sorry that these questions are so dumb. :)

    That seemed to do the trick. Although then, I guess, if I > and 2> the whole thing, I need some other way to get the stuff I would normally echo onto the screen. Oh, also, I read that &> xxx is the same as > xxx 2>&1.

    In Bash, do I understand correctly that if I use the format:

    sudo ./cleanup logs/20050202.log

    That the phrase "20050202.log" is passed as $1 to the script? I guess this means that I can use this format, and then add

    &> $1

    On a separate line to the shell, which will redirect everything to $1, which is the desired log file.

    One more stupid question :D ... what is the Bash command to reset all redirects so that stderr and stdout both go to console?
     
  6. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #6
    I've never seen the &> xxx syntax before, so I can't really comment...

    $1 will only work like that when used inside the script because the variable $1 isn't defined outside the script.

    You don't have to reset redirects. They're set on a command-by-command basis. This means that if you

    cd ~

    Then

    ls > out.txt

    Then

    du

    The ls output gets sent to out.txt, while the du output goes to the screen (since it isn't being redirected).
     

Share This Page