0

I'm trying to create basic script which will be run every minute from cron. Script script.sh is:

#!/bin/bash
DATE=`date +"%Y-%m-%d %H:%M:%S"`
IP=`ifconfig | grep "inet addr" | awk --field-separator ':' '{print $2}' | awk '{print $1}' | head -1`
echo "$DATE $IP" >> test.log

When I run this script by typing "./script.sh" I've got IP address in test.log in such format (and it's ok):

2017-11-08 16:33:33 10.0.0.1
2017-11-08 16:34:33 10.0.0.1
2017-11-08 16:35:33 10.0.0.1

However when I create such a cron job:

* * * * * /path/to/my/script.sh

In test.log I have only date:

2017-11-08 16:36:13
2017-11-08 16:37:13
2017-11-08 16:38:13

But why? Why I have no IP address inside? Do you have any idea?

  • 2
    cron sometimes runs in a different shell and usually in a stripped down environment compared to your user shell, could you try the same script but replace every call with the absolute path (e.g. awk -> /usr/bin/awk) , you can use the which command to determine the absolute path of an executable. – crasic Nov 08 '17 at 15:52

1 Answers1

4

date is probably /bin/date and is in the default $PATH for cron jobs, which is not the same as the $PATH set on user login.

ifconfig is likely /sbin/ifconfig which is not in the $PATH.

Change ifconfig to an explicit full path (such as /sbin/ifconfig) to run ifconfig within a cron.

user4556274
  • 8,995
  • 2
  • 33
  • 37