Timer + screen on Raspbian

Written by pmd - - no comments

Sometimes, you wanna try some risky things such as playing with Network on your Raspberry through SSH. If you lose the connection, and you are not next to the Raspberry, you lose it for good. Sad but true :(

Timer

A timer to restart Raspberry just in case shit happens while playing dumb through ssh:

sudo nano /home/pi/sshcheck.sh
#!/bin/bash
for (( c=$1; c>1; c-- ))
do
   echo -n "Reboot in $c minute(s)...  "
   date +%H:%M
   sleep 1m
done
echo "Reboot in 1 minute(s)..."
sleep 1s
for (( c=59; c>0; c-- ))
do
   echo -e "\e[101mReboot in $c second(s)... (CTRL+C to cancel rebooting)\e[49m"
   sleep 1s
done
echo -e "\e[101mReboot in $c second(s)...\e[49m"
echo -e "\e[101mREBOOT!!!\e[49m"
sudo reboot

Screen

This needs to be started in a screen. If not, you lose the SSH link, and the timer script is stopped (meaning no restart of Raspberry...).

Here are some screen tips.

  1. Start a screen :
    $ screen
    Then start your script within the screen :
    $ bash /home/pi/sshcheck.sh number_of_minutes
    Once timer is in place, you can detach the screen by doing CTRL + a, then d
  2. To resume your screen session use the following command :
    $ screen -dr
  3. In case you have multiple screen sessions running on you machine you will need to append the screen session ID after the r switch. To find the session ID list the current running screen sessions with:
    $ screen -ls
    There is a screen on:
            14704.pts-1.raspberrypi (21/12/18 15:15:26)     (Attached)
    1 Socket in /run/screen/S-pi.
    $ screen -r 10835

Source

Comments are closed.