A
A
Alkop2012-11-10 22:54:25
linux
Alkop, 2012-11-10 22:54:25

echo $variable >> /var/log/file.log from script in CRON not showing up in file.log?

There is a script which is started under the task in cron (root 'ovy).
Including in this script there are such lines

var1=$(command1 | command2) <br>
echo "text text "$var1"" >> /var/log/file.log<br>

So, if the script was launched via a CRON task, then "echo" in file.log is not completely written "text text", without a variable.
If the script is run manually, then “echo” is written to file.log in full: “text text Result_of_VAR1”
The script itself
#! /bin/bash<br>
ISP2=eth1<br>
ISP1=eth2<br>
gw2=X.X.X.X # Gateway 1, preferred<br>
gw1=X.X.X.X # Gateway 2, backup<br>
config_2="/re/config.boot_eth1"<br>
config_1="/re/config.boot_eth2"<br>
ping_ip=8.8.8.8<br>
ping_ip2=8.8.4.4<br>
hold_preferred=YES <br>
vyatta_cfg=/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper<br>
function check_inet<br>
{<br>
        /bin/ping -c 3 $ping_ip >> /dev/null 2>/dev/null<br>
        echo $?<br>
}<br>
status=$(check_inet)<br>
current_gw=$(route -n 2>/dev/null | /bin/grep 0.0.0.0 | /bin/grep UG | awk '{ print $2 }')<br>
echo "[`date`] Detecting current gateway..." >> /var/log/switch_IPS.log<br>
echo "[`date`] Current default gateway is "$current_gw"" >> /var/log/switch_IPS.log<br>
echo "[`date`] Checking Internet connection..." >> /var/log/switch_IPS.log<br>
  check_inet<br>
  if [ $status -eq 0 -a "$current_gw" == "$gw1" ]; then<br>
        echo "[`date`] Internet connection is OK, exiting." >> /var/log/switch_IPS.log<br>
    exit<br>
  elif [ $status -eq 0 -a "$current_gw" == "$gw2" ]; then<br>
    echo "[`date`] You are using a working backup line." >> /var/log/switch_IPS.log<br>
      if [ "$hold_preferred" == "YES" ]; then<br>
        echo "[`date`] Checking primary line..." >> /var/log/switch_IPS.log<br>
          /bin/ping -c 3 -I $ISP1 $ping_ip2 >> /dev/null<br>
            if [ $? -eq 0 ]; then<br>
              echo "[`date`] Primary line is online. Switching back." >> /var/log/switch_IPS.log<br>
                /usr/bin/tail -n30 /var/log/switch_IPS.log | mail --no-user-config -s "ETH2 on Vyatta-HO is Up Again: Switching back to main interfaces: ISP1" [email protected] > /dev/null 2> /dev/null<br>
                $vyatta_cfg begin<br>
                $vyatta_cfg load $config_1<br>
                $vyatta_cfg commit<br>
                $vyatta_cfg save<br>
                /sbin/ip route del $ping_ip2 >> /dev/null<br>
          exit<br>
            else<br>
              echo "[`date`] Primary line is offline. Staying on backup line." >> /var/log/switch_IPS.log<br>
            exit<br>
    fi<br>
  fi<br>
  elif [ $status -ne 0 ]; then<br>
        if [ "$current_gw" == "$gw1" ]; then<br>
            echo "[`date`] Primary line is faulty !ERROR!. Switching to backup line." >> /var/log/switch_IPS.log<br>
                /usr/bin/tail -n30 /var/log/switch_IPS.log | mail --no-user-config -s "ETH2 on Vyatta-HO is Down: Switching to backup interface: ISP2" [email protected] > /dev/null 2> /dev/null<br>
                $vyatta_cfg begin<br>
        $vyatta_cfg load $config_2<br>
        $vyatta_cfg commit<br>
        $vyatta_cfg save<br>
        /sbin/ip route add $ping_ip2 via $gw1 >> /dev/null<br>
      exit<br>
    if [ $(check_inet) -eq 0 ]; then<br>
        echo "[`date`] Backup line is operable." >> /var/log/switch_IPS.log<br>
    else<br>
        echo "[`date`] Backup line is *NOT* operable !ERROR!. Call your ISP." >> /var/log/switch_IPS.log<br>
        echo "[`date`] Switching back to primary line." >> /var/log/switch_IPS.log<br>
        $vyatta_cfg begin<br>
        $vyatta_cfg load $config_2<br>
        $vyatta_cfg commit<br>
        $vyatta_cfg save<br>
        exit<br>
    fi<br>
    elif [ "$current_gw" == "$gw2" ]; then<br>
        echo "[`date`] Backup line is faulty !ERROR!. Switching to primary line." >> /var/log/switch_IPS.log<br>
            /usr/bin/tail -n30 /var/log/switch_IPS.log | mail --no-user-config -s "ETH3 on Vyatta-HO is Down: Switching to interface: ISP1" [email protected] > /dev/null 2> /dev/null<br>
      $vyatta_cfg begin<br>
      $vyatta_cfg load $config_1<br>
      $vyatta_cfg commit<br>
      $vyatta_cfg save<br>
    exit<br>
    if [ $(check_inet) -eq 0 ]; then<br>
        echo "[`date`] Primary line is operable." >> /var/log/switch_IPS.log<br>
    else<br>
        echo "[`date`] Primary line is *NOT* operable !ERROR!. Call your ISP." >> /var/log/switch_IPS.log<br>
        echo "[`date`] Switching back to backup line." >> /var/log/switch_IPS.log<br>
      $vyatta_cfg begin<br>
      $vyatta_cfg load $config_2<br>
      $vyatta_cfg commit<br>
      $vyatta_cfg save<br>
    exit<br>
    fi<br>
  fi<br>
fi<br>

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
s1dney, 2012-11-10
@Alkop

not at the beginning of the PATH script, but directly in the cron task, set PATH for those command1|command2 that are in the var1 variable
or yes, call them as "/path/to/command1" instead of command1

S
Spamkit, 2012-11-11
@Spamkit

Insert at the beginning of the script:
#!/bin/sh
PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin

Y
Yuri, 2012-11-10
@ploop

Maybe so:
var=`(command1 | command2)`

N
NiGHt_LEshiY, 2012-11-10
@NiGHt_LEshiY

command1 and command2 must be called with full paths. Or you need to prescribe PATH at the beginning of the script.

A
Alkop, 2012-11-10
@Alkop

Not only that ... he is after the line

echo "[`date`] Checking Internet connection..." >> /var/log/switch_IPS.log

completely stopped :(

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question