cron job not running a script

Discussion in 'Mac Basics and Help' started by nicoisconfusion, Sep 27, 2011.

  1. nicoisconfusion macrumors newbie

    Joined:
    Sep 27, 2011
    #1
    Hi guys i cant figure this out. I am pretty new to mac and have been lumped with supporting the macs at our university (i am starting to enjoy it actually)

    Anyway i want to impliment a script to run ever X minutes to shutdown a machine if nobody is using it.

    I have created a PowerSchedule.sh file in /Library/Scripts (this is where the login and logout script are located so i put it in the same place)
    I have run a chmod 755 and the script is now executable
    I have also run a chgrp admin so that it is the same as the other scripts

    Now i am trying to get this to run ever 1 minute at the moment just to see if it works. I am using chronnix to create the cron job
    I have it set to every 1 minute and the file i have it opening is /Library/Scripts/PowerSchedule.sh

    However it just doesnt seem to be working, if i restart a machine and stay at the login screen it never shuts down the machine. is it possible it only wants to run as the user i was logged in with when i ran cronnix? How can i just get it to run as root?

    Anyones help would be greatly appreciated
    Here is the script


    #!/bin/sh

    # Sleep or shutdown script
    # tryin' to be 'green'.....

    # look for exception file
    if [ -f "/var/db/.dontSleep" ]; then
    exit 0
    fi

    # if we're a laptop, exit.
    # No shutting down laptops (or waking them up unbidden!)
    IS_LAPTOP=`/usr/sbin/system_profiler SPHardwareDataType | grep "Model" | grep "Book"`

    if [ "$IS_LAPTOP" != "" ]; then
    exit 0
    fi
    # check the time; exit if it's between 5 am and 10 am **CHANGED TO 10 for testing**
    current_hour=`/bin/date +%H`
    if [ $current_hour -gt 5 -a $current_hour -lt 10 ]; then
    exit 0
    fi

    # now check idle time;
    # exit if we've been idle less than 20 minutes
    idleTime=`ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=int((pop @F)/1000000000); print $idle,"\n"; last}'`
    if [ $idleTime -lt 1200 ]; then
    exit 0
    fi

    # tell Power Manager to wake us up or turn us on at 6am M-F
    pmset repeat wakeorpoweron MTWRF 06:00:00

    # check to see if a user's logged into the console
    login_status=`/usr/bin/who | /usr/bin/awk '{ print $2 }'`
    for i in $login_status; do
    if [ $i = "console" ]; then
    # someone's logged in, sleep
    osascript -e 'tell application "System Events" to sleep'
    exit 0
    fi
    done

    # if we got this far, it's OK to shut down.
    /sbin/shutdown -h now
    exit 0
     
  2. Intell macrumors P6

    Intell

    Joined:
    Jan 24, 2010
    Location:
    Inside
    #2
    In Cronnix there's Open button, click it and enter "root". That's the root user.
     
  3. nicoisconfusion thread starter macrumors newbie

    Joined:
    Sep 27, 2011
    #3
    Thanks a lot i was able to create a new cron job for root. it was interesting to see mcafee had a job in there also.

    However this has not fixed my issue. The cronjob is still pointing to that PowerSchedule.sh and set to run every 2 minutes but when a machine is sitting at the login screen i have left it for 5 minutes and the machine has not shutdown.

    Is there a log for cron that shows if it was able to run the task or not?

    From cronnix do i need anything more than /Library/Scripts/PowerSchedule.sh ?
     
  4. Intell macrumors P6

    Intell

    Joined:
    Jan 24, 2010
    Location:
    Inside
    #4
    Why don't you use launchd? Its not depreciated like cron is. Look into the app Lingon.
     
  5. nicoisconfusion thread starter macrumors newbie

    Joined:
    Sep 27, 2011
    #5
    Thanks for that i will look into lingon

    Can launchd be configured on one machine and then have those settings deployed to others?

    I was hoping to get this working on one machine and then deploy the cron file out to the rest of the fleet.
     
  6. Intell macrumors P6

    Intell

    Joined:
    Jan 24, 2010
    Location:
    Inside
    #6
    Yes, it's just a plist and the script/exe that would need to be deployed. A hint when using it, where is asks for the script that it will run enter it as such: "/bin/sh /path/to/script" Remember, the stuff in the quotes only.
     

Share This Page