Linux/bash Script only working if executed from shell prompt

Hi,

maybe I'm asking a VERY dumb question, but would anybody out there tell me, why this f****** script won't work if executed as a cronjob, but works fine if executed from a shell prompt?

#! /bin/bash

set PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

date >> /var/log/cronlog
ping -c 3 10.0.0.1 2>>/var/log/cronlog 1>/dev/null || (service ipsec restart >>/var/log/cronlog && exit 0)
ping -c 3 172.16.1.1 2>>/var/log/cronlog 1>/dev/null || service ipsec restart >>/var/log/cronlog

The purpose of the script is self-explaining. It pings two remote vpn gateways and restarts ipsec, if one of the pings fails. Timestamps, errors and ipsec restart are logged.

Please, help!

b.

The command:

set PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

is something that you would use in a csh script; not bash . In a bash script, that command sets the 1st positional parameter for the current shell execution environment to PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin ; it does not change the value of the PATH environment variable for that script. Try changing it to:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

If that doesn't solve your problem, please answer the following questions:

  1. Who are you logged in as when the script succeeds when you run it from your shell?
  2. Whose crontab is the script run from when cron runs the job? What is the entry in that crontab that starts this script?
  3. What is the output from the command?: ls -ld /var/log /var/log/cronlog
  4. What is the output from the command?: type date ping service

Three lousy letters! Removed the 'set' and it worked.. ..

:slight_smile:

Found it in the google
Thank you! Have exactly the same problem there!

1 Like