Enable autostart: Difference between revisions

From Rsewiki
Line 10: Line 10:


This file is something like:
This file is something like:
@todo missing Teensy_interface


  #!/bin/bash
  #!/bin/bash
  # script to start applications after a reboot
  # script to start applications after a reboot
  #
  #
  # Run the app to show Raspberry's IP on the Teensy display and start button.
  # Run the app to show Raspberry's IP on the Teensy display.
  mkdir -p /home/local/svn/log
  mkdir -p /home/local/svn/log
  cd /home/local/svn/log
  cd /home/local/svn/log
Line 22: Line 20:
  echo "Rebooted" >> rebootinfo.txt
  echo "Rebooted" >> rebootinfo.txt
  date >> rebootinfo.txt
  date >> rebootinfo.txt
  ../robobot/ip_disp/build/ip_disp &
  ../robobot/ip_disp/build/ip_disp 2>/dev/null >ip_disp.out &
  # save PID for debugging
  # save PID for debugging
  echo "ip_disp started with PID:" >> rebootinfo.txt
  echo "ip_disp started with PID:" >> rebootinfo.txt
sleep 0.1
  pgrep -l ip_disp >> rebootinfo.txt
  pgrep -l ip_disp >> rebootinfo.txt
  #
  #
  # start camera server
  # start camera server
  cd /home/local/svn/robobot/stream_server
  cd /home/local/svn/robobot/stream_server
  /usr/bin/python3 stream_server.py &
  /usr/bin/python3 stream_server.py 2>stream_server.err >stream_server.out &
  echo "python3 cam streamer started with PID:" >> rebootinfo.txt
  echo "python3 cam streamer started with PID:" >> /home/local/svn/log/rebootinfo.txt
  pgrep -l python3 >> 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
  exit 0


Use crontab to run this script at reboot.
Use crontab to run this script at reboot as the user "local".


  crontab -e
  crontab -e
Line 43: Line 52:
  @reboot /home/local/on_reboot.bash
  @reboot /home/local/on_reboot.bash


====Hostname update====
==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.
 
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 &
# echo "mission ended"
exit 0
 
This file should be changed if the mission app is elsewhere.
 
It will create files with the text normally printer to the console into the "log_out.txt" file in the Python directory.
 
 
 
 
 
==Hostname update==


A script updates the Raspberry Pi hostname, with the name in ''~/svn/log/robotname''.
A script updates the Raspberry Pi hostname, with the name in ''~/svn/log/robotname''.
Line 93: Line 123:
If the name changes, the script notes it in the file '/home/local/svn/log/rebootinfo.txt' (owned by 'root').
If the name changes, the script notes it in the file '/home/local/svn/log/rebootinfo.txt' (owned by 'root').


==== host_rename.sh ====
=== host_rename.sh ===


The script looks like this:
The script looks like this:

Revision as of 13:55, 9 January 2025

Back to Robobot B

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

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

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.

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 &
# echo "mission ended"
exit 0

This file should be changed if the mission app is elsewhere.

It will create files with the text normally printer to the console into the "log_out.txt" file in the Python directory.



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

The script needs to be run as root; therefore, it is set to run as part of the boot 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 full 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.bash
sudo ln -s /etc/init.d/host_rename.bash /etc/rc2.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

The script looks like this:

#!/bin/sh
#
# /etc/init.d/host_rename.sh
# Subsystem file for "Robobot" host rename function
#
# chkconfig: 2345 95 05
# 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 &
# neither start nor stop is used
# but need to be defined

start() {
  echo -n $"Starting rename, not $prog:"
  # 	/home/local/svn/robobot/setup/rename_host.bash &
  # 	RETVAL=$?
  # 	[ "$RETVAL" = 0 ] && touch /var/lock/subsys/$prog
  echo
}

stop() {
  echo -n $"Stopping $prog:"
  pkill $prog -TERM
  RETVAL=$?
  echo
}

reload() {
  echo -n $"Reloading $prog:"
  pkill $prog -HUP
  RETVAL=$?
  echo
}

case "$1" in
	start)
	    #start
		;;
	stop)
		#stop
		;;
	restart)
		#stop
		#start
		;;
	reload)
		#reload
		;;
	condrestart)
		if [ -f /var/lock/subsys/$prog ] ; then
			#stop
			# avoid race
			sleep 3
			#start
		fi
		;;
	status)
		status $prog
		RETVAL=$?
		;;
		echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
		RETVAL=1
esac
exit $RETVAL