1

I have this Main script:

#!/bin/bash

#Array of Mac hostnames separated by spaces
my_macs=( Mac111 Mac121 Mac122 Mac123 Mac124 Mac125 Mac126 Mac127 Mac128 Mac129 )

#Steps through each hostname and issues SSH command to that host
#Loops through the elements of the Array

for n in "${my_macs[@]}"
do
    # -q quiet
    # -c nb of pings to perform

    ping -q -c3 "${n}" > /dev/null

    if [ $? -eq 0 ]
    then
        ssh admusr@"${n}" 'sudo bash -s' < ./documents/ShutdownUPTIME.sh
    else
        echo "${n} is not available"
    fi

done

exit 0

The main script runs a seperate script. This is ShutdownUPTIME.sh:

#!/bin/bash
BOOT_TIME=$(sysctl -n kern.boottime | sed -e 's/.* sec = \([0-9]*\).*/\1/')
CURR_TIME=$(date +%s)
MAX_UPDAYS=1 #Days

DAYS_UP=$(( ( $CURR_TIME - $BOOT_TIME) / 86400 ))
if [ $DAYS_UP -ge ${MAX_UPDAYS} ];then
    echo Mac is going to shutdown 
    sudo shutdown -h now
else
        echo No shutdown for Mac needed
fi

Now I have 2 problems:

First problem:

I made a cronjob (with crontab -e: 30 23 * * * ./documents/shutdownsimple.sh). I want to log this cronjob, either through the script or the cronjob itself (I want to know when it runs and what exactly it does; for more information look at the echo in the scripts), how can I do that?

Second problem:

If I log the script it will just say Mac is going to shutdown but I want it to say Mac111 is going to shutdown or No need to shutdown Mac125. The names do not show though, any idea how I can accomplish that?

If you find any mistakes, feel free to edit my post, english is not my first language

  • At the top of my main script you can see the array with different Mac names. Theses should be filled in instead of only "Mac" – Gunter Jan 4 '17 at 10:43
  • 1
    cron sends email about every action as default. no need for logging twice. – Ipor Sircer Jan 4 '17 at 10:44
0

Firstly to get the script to show the name of the machine two things:
a) Change the way you remotely execute the script to run it directly on the remote host. This requires that you copy the script to the remote machine. I like to store my personal utility scripts in a directory called "do" in my home directory, so I would have it as
~/do/ShutdownUPTIME.sh

Now change the line which reads
ssh admusr@"${n}" 'sudo bash -s' < ./documents/ShutdownUPTIME.sh

to this:
ssh admusr@"${n}" sudo do/ShutdownUPTIME.sh $n

Basically you are passing $n, the name of the machine, to it as parameter.

b) Then change the ShutdownUPTIME.sh script to use the passed argument. Where you have Mac put $MACNAME. Near the start of the script add the line
MACNAME=$1

The script will now look like this:

#!/bin/bash
MACNAME=$1
BOOT_TIME=$(sysctl -n kern.boottime | sed -e 's/.* sec = \([0-9]*\).*/\1/')
CURR_TIME=$(date +%s)
MAX_UPDAYS=1 #Days

DAYS_UP=$(( ( $CURR_TIME - $BOOT_TIME) / 86400 ))
if [ $DAYS_UP -ge ${MAX_UPDAYS} ];then
    echo Notice: $MACNAME is going to shutdown 
    sudo shutdown -h now
else
        echo Info: No shutdown for $MACNAME needed
fi

I also added "Notice" and "Info" to the output commands to give them some level of importance - this helps with later log filtering.

For bonus make the script handle the situation where you did not supply a Mac Name by having a default value. For example the MACNAME value assignment becomes

MACNAME=${1:-$(hostname)}

About logging: Cron will send you an email with the output of every job, but this can be really inconvenient. The simplest solution is to redirect the output of the commands to a local log file.

For this simply add the redirection on the "done" line of the loop, so it becomes

done | tee -a $LOGFILE 2>&1

And this of course requires you to set up a name in the LOGFILE variable earlier in the script, so add a line like this near the start:
LOGFILE=/var/log/refreshing.log
(Note: Specify a path to a file which you have permission to write to)

In addition you may want to regularly add a timestamp to this log, so just before the loop add the line
echo Running Refresh at $(date) | tee -a $LOGILFE

Logging output from the cron command means your logging only happens when your script is executed from cron. Making the script manage it's own logging is therefore preferred. I changed the script to use tee in stead of redirection so that when you run it on the command line you can still see the output.

  • Thank you very much! It works perfectly exactly how I imagined it Edit> – Gunter Jan 4 '17 at 12:12
  • Edit: it doesn't log properly, I apparently don't have permission to write on that file.... I checked the privileges and it says for everyone Read&Write. – Gunter Jan 4 '17 at 12:18
  • You can do one of two things. Change the log file destination to a path where you do have permissions to write, or else create the log file (as root) and grant your user write access to the file. – Johan Jan 4 '17 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.