Sunday, November 29, 2015

Set Pi to Auto Reboot using Cron


So I have just about finished up the work on restarting. Auto starting programmes on boot etc.
To me this is one of the beauties of the Raspberry Pi and linux.

I have often wanted to use Cron - a system where by we can make certain functions take place at a certain time. There are occasions when my music server loses its authentication to google music which is remedied through a restart. I have now set my system to restart at 4.30 am which should fix this. Tutorial here

The  editing of the file wasn't to difficult see here
Initiate the process with this command which means that you will be editing for the root user.

sudo crontab -e 

The line I added is below

30 4 * * * /sbin/shutdown -r now

30 mins
     4 am
       * every day of month 1-31
           * every week of month
              * every day mon-sun

When it is 30 mins past 4 the system restarts

Next Cron jobs will be 
  • incremental backup of work google drive with rotations
  • backup of pi hardrive to mirror drive
  • back up of pi sd card
  • initiate time lapse photography of plant (may be other programme is better)





Autostart a remote session with the Pi VNC

Having VNC means that I don't have to have a display connected to the Raspberry Pi and still experience what would be on a screen if one were connected.

On my Mac it looks like this. Sometimes it wouldn't be needed as you using the command line would be fine. This can be useful for

  • Scratch programming 
  • Programmes that display something like pictures, directions etc


I included how to set up VNC in this post on one of my other Blogs

What I wanted to achieve as well was for the VNC connection to be started at boot time so that I could immediately connect via VNC rather than having to go through a SSH command line to start it.

I adapted instructions from here having tried others that didn't work for me. I am including them below for my own record and any visitors

Run at boot

Create a file to host the information for start up
sudo nano /etc/init.d/vncboot
Paste the following into it 
#!/bin/sh
### BEGIN INIT INFO
# Provides: vncboot
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start VNC Server at boot time
# Description: Start VNC Server at boot time.
### END INIT INFO
 
USER=pi
HOME=/home/pi

export USER HOME

case "$1" in
 start)
   echo "Starting VNC Server"
   #Insert your favoured settings for a VNC session
   /usr/bin/vncserver :1 -geometry 1280x800 -depth 24
   ;;

 stop)
   echo "Stopping VNC Server"
   /usr/bin/vncserver -kill :1
   ;;

 *)
   echo "Usage: /etc/init.d/vncboot {start|stop}"
   exit 1
   ;;
esac

exit 0
Modify the file permissions so it can be executed
chmod 755 /etc/init.d/vncboot
Enable dependency based boot sequencing
update-rc.d vncboot defaults
Reboot your Raspberry PI and you should find a vncserver already started.




Autostart

One of the things I have found with the Pi is that when it starts I want it to do somethings without having to go and run them each time.

With the photobooth project I want the pi to start this running once it has booted up

The method I am using involves editing the file /etc/rc.local

I did this using

Sudo nano /etc/rc.local

I added the command to run the python code which was in my case

python /home/pi/photoblink.py which I inserted after the fi




This seems to work when I test it with a reboot

shutdown -r now

The photoblink programme is ready go from the start.