Skip to main content
Cut out a different language, added some progress
Source Link
Gunter
  • 157
  • 1
  • 7

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 "Mac"${n} nichtis erreichbar"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

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 "Mac nicht erreichbar"
    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

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

Source Link
Gunter
  • 157
  • 1
  • 7

How to log a cronjob?

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 "Mac nicht erreichbar"
    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