bash Script to Change /etc/hosts on Reboot

Hi I have small Bash script which should change IP on System reboot.

#!/bin/bash

#hostname=hostname
HOSTNAME=$(hostname)
IPV4=$(ifconfig eth0 | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')

# Set the host name
hostname $HOSTNAME
echo $HOSTNAME > /etc/hostname

# Add fqdn to hosts file
cat<<EOF > /etc/hosts
# Host File Get modified by the script
127.0.0.1 localhost localhost.localdomain
$IPV4 $HOSTNAME
EOF



While running the script standalone, it is working fine but when I use cron to run on system reboot it is not putting IP before hostname

Example :   Running Script standalone
# Host File Get modified by the script
127.0.0.1 localhost localhost.localdomain
192.168.1.52  ip-192-168-1-52

Example :
  Running Script with cron
# Host File Get modified by the script
127.0.0.1 localhost localhost.localdomain
 ip-192-168-1-52
sivaatluriAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sivaatluriAuthor Commented:
I want the script functionality to work while working with cron
0
sivaatluriAuthor Commented:
#!/bin/bash


IPV4=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/local-ipv4`

# Set the host name
hostname $HOSTNAME
echo $HOSTNAME > /etc/hostname

# Add fqdn to hosts file
cat<<EOF > /etc/hosts
# This file is automatically genreated by ec2-hostname script
127.0.0.1 localhost
$IPV4 $HOSTNAME

EOF
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gheistCommented:
You can add your own host name to 127.0.0.1 in case IP is dynamic, that should help most applications that depend on hostname lookup locally.
0
simon3270Commented:
If it works on the command line but not in cron, the two most common problems are environment (including PATH and other variables set up during login), and tty (some commands only work properly if there is a terminal attached - cron commands don't have one).

In this case, it may be that ifconfig is in, for example, /sbin, which probably isn't  in cron's default path.  The fix for this is to put the full path to commands (e.g. /sbin/ifconfig) in scripts run via crontab.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.