Running a bash script as root on boot
7,807
i ended up doing changing the content of rc.local, and like @user2563336 we don't need root access because login script have root access
#!/bin/bash
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
writeinterfacefile()
{
cat << EOF > $1
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
allow-hotplug eth0
iface eth0 inet static
address $2
#network 192.168.1.0
netmask $3
#broadcast 192.168.1.255
gateway $4
dns-nameservers 8.8.8.8
EOF
#don't use any space before of after 'EOF' in the previous line
}
file="/etc/network/interfaces"
current_ip=`sudo ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $3}'|sed 's/Bcast://'`
IP=`cut -f1,2,3 -d"." <<< $current_ip`
lIP=`cut -f4 -d"." <<< $current_ip`
lIP=`expr $lIP - 36`
ip=$IP"."$lIP
mask=`sudo ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $4}'|sed 's/Mash://'`
mask=`cut -f2 -d":" <<< $mask`
gateway=$(/sbin/ip route | awk '/default/ { print $3 }')
#/bin/rm $file
/bin/mv $file /etc/network/BAKinterfaces
/usr/bin/touch $file
writeinterfacefile $file $ip $mask $gateway
/etc/init.d/networking restart
exit 0
Related videos on Youtube
Author by
Souhaieb
Updated on September 18, 2022Comments
-
Souhaieb almost 2 years
i have a script that calculate an ip address (using gateway address) than change,
the script work when i launch it manually with sudo
sudo ./changework.sh
i want this script to run during bootup time
i
sudo cp changework.sh /etc/init.d/changework sudo chmod+x /etc/init.d/changework sudo update-rc.d changework defaults
but it does not work,
also i tested it using
sudo crontab -e
then added
@reboot sleep 10 && /home/ubunu/changework.sh
it does not work either
changework.sh
#!/bin/bash #set interface interface="enp0s5" #read current IP address on interface current_ip=`ifconfig $interface 2>/dev/null|awk '/inet addr:/ {print $3}'|sed 's/Bcast://'` IP=`cut -f1,2,3 -d"." <<< $current_ip` lIP=`cut -f4 -d"." <<< $current_ip` lIP=`expr $lIP - 34` IP=$IP"."$lIP #return default gateway gateway=$(/sbin/ip route | awk '/default/ { print $3 }') #check if IP is taken using ping count=`ping -c 1 $IP | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }'` if [ $count -eq 0 ]; then #change IP if available ifconfig $interface down ifconfig $interface $IP up ifconfig $interface #change gateway ip route add default via $gateway else #IP change not possible echo "IP not available" fi
would any one please have a solution or a workaround and thanks for any help or suggestion
-
Souhaieb about 7 yearsthe reason why i'm not using /etc/network/interfaces is that the ip should be calculated and and not fixed, really thanks for answering, i'm starting by changing command to full path ifconfig => /sbin/ifconfig , ping => /bin/ping , ip => /sbin/ip, and i will test it
-
Sandro B. about 7 yearsConsider also that cron usually uses sh as default shell and not bash. To use bash you should put a
SHELL=/bin/bash
in the line before your cronjob -
Sandro B. about 7 yearsIs the cronjob actually executed? Watch out for the /home/ubunTu/changework.sh typo ...
-
Souhaieb about 7 yearsafter sudo crontab -e, SHELL=/bin/bash @reboot sleep 10 && /home/ubunu/changework.sh, but still nothing
-
Sandro B. about 7 yearsHave you checked if the script is actually executed? Check in /var/log/syslog.