Enable autostart: Difference between revisions
| Line 175: | Line 175: | ||
This script is also starting off_by_mqtt: | This script is also starting off_by_mqtt: | ||
#!/bin/bash | |||
# to be called at boot. | |||
# Replaces hostname with name in <robotname>. | |||
# Get old and new hostname | |||
hn=`hostname` | |||
# echo "renaming" $hn "again" >/home/local/aaa.txt | |||
# read new name in the file robotname | |||
f="/home/local/svn/log/robotname" | |||
cd /home/local/svn/log | |||
date >> rename_info.txt | |||
if [ -f $f ]; | |||
then # file exist | |||
# get name in file to variable nn | |||
nn=$(tr -d '\r\n' < "$f") | |||
# and length of this name | |||
echo "found name " $nn | |||
yy=$(expr length $nn) | |||
echo "length of" $nn "in" $f "is" $yy | |||
if [ $yy -gt 2 ]; | |||
then # length is longer than 2 characters | |||
if [ $hn != $nn ]; | |||
then # replace old hostname with new | |||
#echo new name $nn, so rename host from $hn | |||
echo New name is $nn - so rename host from $hn | |||
echo New name is $nn - so rename host from $hn >> rename_info.txt | |||
hostnamectl set-hostname $nn | |||
# sed -i "s/$hn/$nn/g" /etc/hostname | |||
# seems like /etc/hosts is not updated, but /etc/hostname is | |||
sed -i "s/$hn/$nn/g" /etc/hosts | |||
else | |||
# just add reboot date to file | |||
echo Same hostname $hn - all is fine. | |||
echo Same hostname $hn - all is fine. >> rename_info.txt | |||
fi | |||
fi | |||
else | |||
echo "File >" $f "< not found." >> rename_info.txt | |||
fi | |||
# listen for MQTT power off | |||
sleep 10 | |||
/home/local/svn/robobot/off_by_mqtt/build/off_by_mqtt 2>>off_by_mqtt2.txt >>off_by_mqtt1.txt & | |||
# test if we are at DTU | |||
sleep 10 | |||
ssid="DTUdevice" | |||
echo Test access to SSID "$ssid" >> rename_info.txt | |||
aa=$(nmcli dev wifi | grep "$ssid") | |||
aal=$(expr length "$aa") | |||
# echo "length of $aa is $aal" | |||
if [ "$aal" -gt 20 ]; | |||
then | |||
echo SSID $ssid is available | |||
# trying to ping default router using 'ip r' to find router IP | |||
ping -q -t 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null | |||
if [ "$?" -ne 0 ]; | |||
then | |||
echo We are not connected, trying to connect to "$ssid" >> rename_info.txt | |||
nmcli device wifi connect "$ssid" password robots4ever ifname wlan0 | |||
else | |||
echo We \("$HOSTNAME"\) are connected to "$ssid" >> rename_info.txt | |||
fi | |||
else | |||
echo no $ssid SSID found >> rename_info.txt | |||
fi | |||
#iwconfig 2>/dev/null >> rename_info.txt | |||
exit 0 | |||
Latest revision as of 15:56, 18 April 2026
Back to Robobot B
Mission start
The function ip_disp started by the on_reboot script also listens to the start button and will call the mission_start.bash script.
Copy the script to the home directory:
cd cp svn/robobot/setup/mission_start.bash .
The default version of this script is:
#!/bin/bash # echo -e "\nStarting mission\n" cd /home/local/svn/robobot/mqtt_python #/usr/bin/python3 mqtt-client.py >log_out.txt 2>log_err.txt & /usr/bin/python3 mqtt-client.py >>log_out.txt 2>>log_err.txt & # echo "mission ended" exit 0
This file should be updated if the mission app is elsewhere or has a different name.
It will create or append a file named "log_out.txt" in the Python directory. Anything that would have been printed to the console is directed to this file. The 2>>log_err.txt drects any error to this file (usually empty).
Autostart
Start app to display IP on Regbot display, log CPU temperature and synchronize hostname.
Make an on-reboot script in the home directory.
cp ~/svn/robobot/setup/on_reboot.bash ~/
This file is something like:
#!/bin/bash # script to start applications after a reboot # # Run the app to show Raspberry's IP on the Teensy display. mkdir -p /home/local/svn/log cd /home/local/svn/log # save the last reboot date echo "Rebooted" >> rebootinfo.txt date >> rebootinfo.txt ../robobot/ip_disp/build/ip_disp 2>/dev/null >ip_disp.out & # save PID for debugging echo "ip_disp started with PID:" >> rebootinfo.txt sleep 0.1 pgrep -l ip_disp >> rebootinfo.txt # # start camera server cd /home/local/svn/robobot/stream_server /usr/bin/python3 stream_server.py 2>stream_server.err >stream_server.out & echo "python3 cam streamer started with PID:" >> /home/local/svn/log/rebootinfo.txt sleep 0.1 pgrep -l python >> /home/local/svn/log/rebootinfo.txt # # start teensy_interface cd /home/local/svn/robobot/teensy_interface/build ./teensy_interface -d -l 2>out_err.txt >out_console.txt & echo "Teensy _interface started with PID:" >> /home/local/svn/log/rebootinfo.txt sleep 0.1 pgrep -l teensy_i >> /home/local/svn/log/rebootinfo.txt # date >> /home/local/svn/log/rebootinfo.txt exit 0
The file need to be executable:
chmod +x on_reboot.bash
Use crontab to run this script at reboot as the user "local".
crontab -e
If asked, select the preferred editor (suggesting nano).
Add this line at the end:
@reboot /home/local/on_reboot.bash
Hostname update
A script updates the Raspberry Pi hostname, with the name in ~/svn/log/robotname. The script rename_host.bash is:
#!/bin/bash
hn=`hostname`
f="/home/local/svn/log/robotname"
if [ -f $f ];
then # file exist
# get (new) name in file to variable nn
nn=`cat $f`
echo "found name " $nn
yy=$(expr length $nn)
if [ $yy -gt 2 ];
then # length is longer than 2 characters
if [ $hn != $nn ];
then # replace old hostname with new
echo ++++++++++ >> rebootinfo.txt
date >> rebootinfo.txt
echo new name is $nn, so rename host from $hn >> rebootinfo.txt
hostnamectl set-hostname $nn
sed -i "s/$hn/$nn/g" /etc/hosts
fi
fi
else
echo "File >" $f "< not found."
fi
# listen for MQTT power off
sleep 10
/home/local/svn/robobot/off_by_mqtt/build/off_by_mqtt 2>off_by_mqtt2.txt >off_by_mqtt1.txt &
exit 0
The script needs to be run as root; therefore, it is set to run as part of the boot process.
Shutdown process
The shutdown process off_by_mqtt also need to run as root - to be able to call sudo shutdown now.
This process listens to an off command on MQTT, then shutdown.
Make rename and shutdown run as root
Runlevel 2 and 3 are the time during boot when all services are started. Another script has been added to rename and start the power-off process.
Copy a service script (host_rename.sh) to /etc/init.d
sudo cp ~/svn/robobot/setup/host_rename.sh /etc/init.d/
The important line in this script is just after the initial (but needed) comments:
/home/local/svn/robobot/setup/rename_host.bash
The entire script is further down.
Make the file executable and add it to run-level 2 (before networking is started)
sudo chmod +x /etc/init.d/host_rename.sh sudo ln -s /etc/init.d/host_rename.sh /etc/rc3.d/S99host_rename
The script is then executed when the Raspberry boots, and if the hostname does not match that of the Teensy, then the hostname is modified.
If the name changes, the script notes it in the file '/home/local/svn/log/rebootinfo.txt' (owned by 'root').
host_rename.sh listing
The script looks like this:
#!/bin/sh # # /etc/init.d/host_rename.sh # Subsystem file for "Robobot" host rename function # # chkconfig: 2345 95 05 # NOTE, these initial comments are needed! # description: Robobot rename service # # processname: none # config: /home/local/svn/robobot/setup/none # pidfile: /var/run/Robobot.pid # source function library #. /etc/rc.d/init.d/functions # pull in sysconfig settings # [ -f /etc/sysconfig/mySystem ] && . /etc/sysconfig/mySystem # RETVAL=0 prog="teensy_interfac" /home/local/svn/robobot/setup/rename_host.bash & # exit $RETVAL
rename_host.bash listing
This script is also starting off_by_mqtt:
#!/bin/bash
# to be called at boot.
# Replaces hostname with name in <robotname>.
# Get old and new hostname
hn=`hostname`
# echo "renaming" $hn "again" >/home/local/aaa.txt
# read new name in the file robotname
f="/home/local/svn/log/robotname"
cd /home/local/svn/log
date >> rename_info.txt
if [ -f $f ];
then # file exist
# get name in file to variable nn
nn=$(tr -d '\r\n' < "$f")
# and length of this name
echo "found name " $nn
yy=$(expr length $nn)
echo "length of" $nn "in" $f "is" $yy
if [ $yy -gt 2 ];
then # length is longer than 2 characters
if [ $hn != $nn ];
then # replace old hostname with new
#echo new name $nn, so rename host from $hn
echo New name is $nn - so rename host from $hn
echo New name is $nn - so rename host from $hn >> rename_info.txt
hostnamectl set-hostname $nn
# sed -i "s/$hn/$nn/g" /etc/hostname
# seems like /etc/hosts is not updated, but /etc/hostname is
sed -i "s/$hn/$nn/g" /etc/hosts
else
# just add reboot date to file
echo Same hostname $hn - all is fine.
echo Same hostname $hn - all is fine. >> rename_info.txt
fi
fi
else
echo "File >" $f "< not found." >> rename_info.txt
fi
# listen for MQTT power off
sleep 10
/home/local/svn/robobot/off_by_mqtt/build/off_by_mqtt 2>>off_by_mqtt2.txt >>off_by_mqtt1.txt &
# test if we are at DTU
sleep 10
ssid="DTUdevice"
echo Test access to SSID "$ssid" >> rename_info.txt
aa=$(nmcli dev wifi | grep "$ssid")
aal=$(expr length "$aa")
# echo "length of $aa is $aal"
if [ "$aal" -gt 20 ];
then
echo SSID $ssid is available
# trying to ping default router using 'ip r' to find router IP
ping -q -t 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null
if [ "$?" -ne 0 ];
then
echo We are not connected, trying to connect to "$ssid" >> rename_info.txt
nmcli device wifi connect "$ssid" password robots4ever ifname wlan0
else
echo We \("$HOSTNAME"\) are connected to "$ssid" >> rename_info.txt
fi
else
echo no $ssid SSID found >> rename_info.txt
fi
#iwconfig 2>/dev/null >> rename_info.txt
exit 0