Enable autostart

From Rsewiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "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 ...")
 
(Hostname update)
 
(2 intermediate revisions by one user not shown)
Line 18: Line 18:
 
  cd /home/local/svn/log
 
  cd /home/local/svn/log
 
  # save the last reboot date
 
  # save the last reboot date
  echo "Rebooted" >> rebootinfo.txt
+
  echo "Rebooted" >> /home/local/rebootinfo.txt
  date >> rebootinfo.txt
+
  date >> /home/local/rebootinfo.txt
 +
# start IP display (and button start) task
 
  ../robobot/ip_disp/build/ip_disp &
 
  ../robobot/ip_disp/build/ip_disp &
  # save PID for debugging
+
  # save PID for IP display task for debugging
  echo "ip_disp started with PID:" >> rebootinfo.txt
+
  echo "ip_disp started with PID:" >> /home/local/rebootinfo.txt
  pgrep -l ip_disp >> rebootinfo.txt
+
  pgrep -l ip_disp >> /home/local/rebootinfo.txt
 
  exit 0
 
  exit 0
 +
  
 
Use crontab to run this script at reboot
 
Use crontab to run this script at reboot
Line 39: Line 41:
 
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''.
 
The script ''rename_host.bash'' is:
 
The script ''rename_host.bash'' is:
 +
 
  #!/bin/bash
 
  #!/bin/bash
 
  hn=`hostname`
 
  hn=`hostname`
 
  f="/home/local/svn/log/robotname"
 
  f="/home/local/svn/log/robotname"
 
  if [ -f $f ];  
 
  if [ -f $f ];  
  then
+
  then # file exist
 +
  # get (new) name in file to variable nn
 
   nn=`cat $f`
 
   nn=`cat $f`
   yy= $(expr length $nn)
+
  echo "found name " $nn
 +
   yy=$(expr length $nn)
 
   if [ $yy -gt 2 ];
 
   if [ $yy -gt 2 ];
   then
+
   then # length is longer than 2 characters
 
     if [ $hn != $nn ];  
 
     if [ $hn != $nn ];  
     then
+
     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
 
       hostnamectl set-hostname $nn
      # seems like /etc/hosts is not updated, but /etc/hostname is
 
 
       sed -i "s/$hn/$nn/g" /etc/hosts
 
       sed -i "s/$hn/$nn/g" /etc/hosts
    else
 
      echo Same hostname, all is fine.
 
 
     fi
 
     fi
 
   fi
 
   fi
Line 61: Line 66:
 
  fi
 
  fi
  
The script needs to be run as root, it is therefore set to run as part of the boot process.
+
The script needs to be run as root; it is, therefore set to run as part of the boot process.
  
 
Create the script in /etc/init.d
 
Create the script in /etc/init.d
Line 67: Line 72:
 
  sudo nano /etc/init.d/host_rename.sh
 
  sudo nano /etc/init.d/host_rename.sh
  
Enter the following lines into the new file
+
Enter the following lines into the new file.
  
 
  #!/bin/bash
 
  #!/bin/bash
Line 78: Line 83:
  
 
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.
 
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.
 +
 +
The script makes a note in the file '/home/local/svn/robobot/setup/rebootinfo.txt (owned by 'root') if the name is changed.

Latest revision as of 10:23, 22 December 2023

Back to Robobot B

[edit] 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 IP of raspberry on the Teensy display.
mkdir -p /home/local/svn/log
cd /home/local/svn/log
# save the last reboot date
echo "Rebooted" >> /home/local/rebootinfo.txt
date >> /home/local/rebootinfo.txt
# start IP display (and button start) task
../robobot/ip_disp/build/ip_disp &
# save PID for IP display task for debugging
echo "ip_disp started with PID:" >> /home/local/rebootinfo.txt
pgrep -l ip_disp >> /home/local/rebootinfo.txt
exit 0


Use crontab to run this script at reboot

crontab -e

If asked, select the preferred editor (suggesting nano).

Add this line at the end:

@reboot /home/local/on_reboot.bash

[edit] 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; it is, therefore set to run as part of the boot process.

Create the script in /etc/init.d

sudo nano /etc/init.d/host_rename.sh

Enter the following lines into the new file.

#!/bin/bash
bash /home/local/svn/robobot/setup/rename_host.bash

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/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.

The script makes a note in the file '/home/local/svn/robobot/setup/rebootinfo.txt (owned by 'root') if the name is changed.

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox