Auto run script on LInux

173 views Asked by At

I am trying to understand why my script hasn't fully run on startup. I am working on embedded linux (USing petalinux). my script is very simple:

#!/bin/sh

echo "Hello PetaLinux World"

ifconfig eth0 192.168.1.120

Unfortunately, i see that only echo command has been executed, and I see 'Hello PetaLinux World' on boot log. When trying to run the script manually after boot ifconfig is also executed. Is there a reason for ifconfig command (or any other commands) not to be executed while boot?

update i modified the script:

    #!/bin/sh
 
echo "Hello PetaLinux World"
 
# Use the full path to ifconfig and capture errors
/sbin/ifconfig eth0 192.168.1.120 > /tmp/ifconfig.log 2>&1
 
# Check for errors and log them
if [ $? -ne 0 ]; then
    echo "Error running ifconfig. Check /tmp/ifconfig.log for 
details."
fi

in boot i see:

Hello PetaLinux World
INIT: Entering runlevel: 5
Configuring network interfaces... udhcpc: started, v1.31.0

when checking ifconfig log i get:

root@brm1553_intr:~# cat /tmp/ifconfig.log .bash_history
ifconfig
rclear
ckear
clear
reboot
1

There are 1 answers

0
MJKing On

System startup scripts, such as CRON, do not have environment variables defined as they are in the console. So the solution is usually to put the full path to the executable, or define the PATH variable before executing the command.