run a script as root on a schedule

Discussion in 'OT Technology' started by kamikaze, Oct 24, 2007.

  1. kamikaze

    kamikaze Active Member

    Joined:
    Jan 30, 2005
    Messages:
    33,629
    Likes Received:
    0
    Location:
    Barrie Ontario
    Lets say i've got a small shell script that i want to run every hour, with root privileges. How do i go about this?
     
  2. dorkultra

    dorkultra OT's resident crohns dude OT Supporter

    Joined:
    Oct 14, 2005
    Messages:
    22,736
    Likes Received:
    27
    Location:
    yinzer / nilbog, trollhio
    to run as root, at least in linux i would always type in "su" and it would ask for the root password
    oddly, i can't figure out the root password
     
  3. kamikaze

    kamikaze Active Member

    Joined:
    Jan 30, 2005
    Messages:
    33,629
    Likes Received:
    0
    Location:
    Barrie Ontario
    yea, i can do that is OS X by using sudo and then the command, but it asks me for the password. I want it to do it all by itself, and I want it to run every hour.
     
  4. Euclid

    Euclid New Member

    Joined:
    Apr 19, 2004
    Messages:
    8,139
    Likes Received:
    0
    Location:
    Nashville, TN
    you could cat the password and then use a pipe

    cat password | sudo script

    of course then you'd be storing your root password in the crontab which isn't very wise.

    edit: I mean "echo" not "cat"
     
  5. kamikaze

    kamikaze Active Member

    Joined:
    Jan 30, 2005
    Messages:
    33,629
    Likes Received:
    0
    Location:
    Barrie Ontario
    i'll keep that in mind, but you are right, doesn't seem too wise to store the password in plaintext
     
  6. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    i'm not at my mac, but look into crontab
     
  7. kamikaze

    kamikaze Active Member

    Joined:
    Jan 30, 2005
    Messages:
    33,629
    Likes Received:
    0
    Location:
    Barrie Ontario
    Yea, i'd found that but I've no idea how to use it :o
     
  8. mobbarley

    mobbarley Active Member

    Joined:
    Mar 4, 2005
    Messages:
    9,256
    Likes Received:
    2
    Location:
    Sydney
    sudo pico /etc/crontab
    <pass>
    0 * * * * root /bin/sh /script/to/execute.sh
    <ctrl>+<x>
    Y
     
  9. mobbarley

    mobbarley Active Member

    Joined:
    Mar 4, 2005
    Messages:
    9,256
    Likes Received:
    2
    Location:
    Sydney
    0 * * * * means evey time the minute hand hits 0

    root = user to run as

    /bin/sh /script.sh = your command
     
  10. kamikaze

    kamikaze Active Member

    Joined:
    Jan 30, 2005
    Messages:
    33,629
    Likes Received:
    0
    Location:
    Barrie Ontario
    now if it's just a single command can I substitute it for the .sh file?
     
  11. mobbarley

    mobbarley Active Member

    Joined:
    Mar 4, 2005
    Messages:
    9,256
    Likes Received:
    2
    Location:
    Sydney
    0 * * * * root command
     
  12. mobbarley

    mobbarley Active Member

    Joined:
    Mar 4, 2005
    Messages:
    9,256
    Likes Received:
    2
    Location:
    Sydney
    the only thing is you may need to put the full path to any binaries (not sure) post the command if you need help.
     
  13. kamikaze

    kamikaze Active Member

    Joined:
    Jan 30, 2005
    Messages:
    33,629
    Likes Received:
    0
    Location:
    Barrie Ontario
    cool, i'm gunna try it tomorrow when i get back to work. It's for my hackintosh, it won't keep time, so I wanna run ntpdate every hour or so to keep it at the right time. ntpdate requires root access to update the time. The auto update only seems to work when I reboot or come out of sleep, but that machine never sleeps and I don't reboot it for weeks at a time.
     
  14. kamikaze

    kamikaze Active Member

    Joined:
    Jan 30, 2005
    Messages:
    33,629
    Likes Received:
    0
    Location:
    Barrie Ontario
    Ok, I finally got this to work, I had to add the following to my crontab file for it to work:

    Code:
    SHELL=/bin/sh
    PATH=/bin:/sbin:/usr/bin:/usr/sbin
    HOME=/var/log
    
    Thanks :bigthumb:
     
  15. DouggieJ

    DouggieJ OT Supporter

    Joined:
    Nov 1, 2007
    Messages:
    8,126
    Likes Received:
    0
    Location:
    New York, NY USA
    You can set the root password using

    sudo passwd root
    <enter your password>
    <enter new root password>
    <confirm>

    Then you can su to root and remain there instead of using sudo all the time.
     
  16. kamikaze

    kamikaze Active Member

    Joined:
    Jan 30, 2005
    Messages:
    33,629
    Likes Received:
    0
    Location:
    Barrie Ontario
    Thanks for the tip, but for the odd time that I need root access, sudo will suffice :bigthumb:
     

Share This Page