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?
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 thewhich
command to determine the absolute path of an executable. – crasic Nov 08 '17 at 15:52