DAKboard: Raspberry Pi cron tips

When setting up a Raspberry Pi to be used as a DAKboard you may want to ensure that some tasks happen on a regular basis. The cron job scheduler can be used to achieve this and in this post I will show you how you can schedule turning the display off and restarting the Raspberry Pi. The post finishes with a couple of tips that make working with cron easier.

Switching off the display each night

To turn off the display I used the tvservice command.

#!/bin/sh 
/opt/vc/bin/tvservice --off

Save the script above as hdmi-off.sh in the pi home directory: /home/pi and make it executable by amending the Execute access control to “Only owner and group” as shown below

In order to run this script every day at 20:00 every day, a new cron job is required. The next steps will show you how to set this up.

Execute the following command

crontab -e

and add the following entry

0 20 * * * /home/pi/hdmi-off.sh

An explanation of the cron command format is shown in cron tips section below but briefly this entry runs the hdmi-off.sh script every day at 20:00.

Restart each day

To restart the Raspberry Pi every day at 07:00 I first executed this command

sudo cronbtab -e

Restarting a device is a powerful privilege so this is something added to the cron jobs of the root user.

Next I added the time and the command to run

0 7 * * * /sbin/shutdown -r now

See below for an explanation on how the time section works. The command uses the full path to shutdown. The -r option indicates the Pi will rebooted after it has been brought down. The now argument is used because I know it will be safe to restart immediately.

Miscellaneous cron tips

Adding a new cron entry

Adding a new entry to cron is straightforward once you understand the format. The first part, the five asterisks represent temporal information; when do you want the job to run. The diagram above shows what each asterisk represents.

The second part is the command that you would like to run. Always include the full path to the command that you want to run because you can’t rely on environment variables being available.

One of cron entries shown earlier should now be easier to understand.

0 7 * * * /sbin/shutdown -r now

What is cron doing?

If your cron job is not doing what you expect, it is worth checking the following log file.

/var/log/syslog

syslog is a general system log file so other process also record information here. To cut down the noise and just see the cron information use the following command.

grep CRON /var/log/syslog

Acknowledgements

This post is source of the method I used for switching off the display whilst the method I have used for the rebooting the Raspberry Pi came from this post. Many thanks to both of the authors for their generosity.

The information about viewing the cron logs came from this answer to a question on AskUbuntu

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.