0

I made a shell script to reboot my machine. The script sends an email with some information of the host and then reboots.

The script will be run by cron.

When I run the script from the terminal it runs pretty well, but when it runs using cron, the IP address and "servicos" are missing.

My script is:

#!/bin/bash

# Carrega uptime
up=("$(uptime -p)")

# Carrega nome do host
host=("$(hostname)")

# Carrega IPs do host
meu_ip=("$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')")

# Carrega data/hora atual
data=("$(date +"%Y-%m-%d")")
hora=("$(date +"%T")")

# Carrega servicos iniciados durante o boot, ordenados alfabeticamente
servicos=("$(ls -1 /etc/rc$(runlevel| cut -d" " -f2).d/S* | awk -F'[0-9][0-9]' '{print " " $2}' | sort -k 3)")

### Define parametros de e-mail ###
email="myemail@address.com" # E-mail do destinatario do alerta
assunto=$host": [Alert] Restart ["$meu_ip"]" # Assunto do email

# Envio de email de alerta
printf "%b\n" "Sistema [$host] ($meu_ip) reiniciado em $data.\n
Uptime: $up\n
Carregado na inicializacao:\n$servicos" | /usr/bin/mail -s "$assunto" "$email"

# Aguarda 5s para que o email seja enviado corretamente
sleep 5

# Reinicia o equipamento
/sbin/reboot

I already tested awk too:

meu_ip=("$(ifconfig | awk '/inet addr/{print substr($2,6)}' | awk 'NR==1{print $1}')")

But still not working...

Example of a received email after cron (automatic) exec:

Sistema [raspberrypi] () reiniciado em 2016-09-26.

Uptime: up 1 minute

Carregado na inicializacao:

Example of a received email after terminal (manual) exec:

Sistema [raspberrypi] (192.168.8.121) reiniciado em 2016-09-26.

Uptime: up 30 minutes

Carregado na inicializacao:
 avahi-daemon
 bluetooth
 bootlogs
 cron
 dbus
 dhcpcd
 dphys-swapfile
 exim4
 motd
 ntp
 plymouth
 rc.local
 rmnologin
 rsyslog
 ssh
 triggerhappy

I'm currently using Raspbian based on Debian Jessie.

  • Are you setting an appropriate PATH anywhere? cron's default one is limited (commands like runlevel and ifconfig may be in /sbin) – steeldriver Sep 26 '16 at 23:43
  • @steeldriver, got it! Now it works great! Thank you! – Cadoso Sep 27 '16 at 0:24
1

Defined the PATH inside the script and made some "cosmetic" changes.

#!/bin/bash

# Define caminho dos binarios
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Carrega uptime
up=("$(uptime -p | awk '{$1=""; sub("  ", " "); print}')")

# Carrega nome do host
host=("$(hostname)")

# Carrega IPs do host
meu_ip=("$(ifconfig | awk '/inet addr/{print substr($2,6)}' | awk 'NR==1{print $1}')")

# Carrega data/hora atual
data=("$(date +"%Y-%m-%d")")
hora=("$(date +"%T")")

# Carrega servicos iniciados durante o boot, ordenados alfabeticamente
servicos=("$(ls -1 /etc/rc$(/sbin/runlevel| cut -d" " -f2).d/S* | awk -F'[0-9][0-9]' '{print " Servico :-> " $2}' | sort -k 3)")

### Define parametros de e-mail ###
email="myemail@address.com"        # E-mail do destinatario do alerta
assunto=$host": [Alert] Restart ["$meu_ip"]" # Assunto do email

# Envio de email de alerta
printf "%b\n" "Sistema [$host] ($meu_ip) reiniciado em $data.\n
Uptime: $up\n
Carregado na inicializacao:\n$servicos" | mail -s "$assunto" "$email"

# Aguarda 10s para que o email seja enviado corretamente
sleep 10

# Reinicia o equipamento
reboot

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.