The homepage of David Hall. This is not necessarily a good thing.

Raspberry Pi Scripts

Here is a collection of Raspberry Pi scripts that I’ve put together over the past few months to make running and recovery easier and things just generally a little brighter when using ssh on my Raspberry Pi.

My Raspberry Pi scripts

Make no bones about it – the Raspberry Pi scripts here are by no means advanced, but they work without errors and help things keep running smoothly.

That said, I like ’em.

Initialisation script

Bar far the handiest of my Raspberry Pi scripts – initialise.sh.

This script is saved in your user’s home directory as initialise.sh and I run it after a reboot or power outage.

I used to have problems with my Owncloud installation as the disk is a USB key and it was having trouble mounting properly so I started with that and built it up from there.

The script does the following (in order):

Start a VNC server with a screen size of 1024×768 in 24bit colour depth and 96dpi.

unmount, mount and check the HFS+ filesystem of my USB key

restart Apache to allow Owncloud to run properly.

After a restart, that gets everything running smoothly again. As I said – handy.

Shell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

#!/bin/sh

### BEGIN INIT INFO

# Provides: myscript

# Required-Start: $local_fs $network

# Required-Stop: $local_fs

# Default-Start: 2 3 4 5

# Default-Stop: 0 1 6

# Short-Description: myscript

# Description: Start VNC and mount OwnCloud Drives

### END INIT INFO

#

# A script to set up a couple of bits on my Pi

# Such as VNC, Owncloud, and to unmount, fsck and mount

# USB Disk and /dev/sda1. Also restarts Apache2.

#

# Define text colours

RED='\033[0;31m'#Red

GR='\033[0;32m'#Green

BL='\033[1;34m'#Blue

NC='\033[0m'# No Color (Au Naturale)

echo""

echo"Raspberry Pi Initialisation Script"

echo"(C) David Hall 2015"

echo""

echo"Script being run by ${GR}$USER${NC} on ${GR}$(date)${NC}"

echo""

echo"${BL}Starting VNC Server${NC}"

echo""

# Start VNC Server

vncserver:1-geometry1024x768-depth24-dpi96>/dev/null

# If loop checks completion status of the last command

# If $? = 0, the command was successful. 1 for failure.

if[$?-eq0];then

echo"${GR}*** VNC Server Started ***${NC}"

echo"${BL}Connect using the form x.x.x.x:1${NC}"

echo""

else

echo"${RED}*** VNC Server Not Started ***${NC}"

echo"${RED}You will not be able to connect to VNC${NC}"

echo""

fi

echo"${BL}Mounting OwnCloud drive${NC}"

echo"${BL}Unmount USB Disk"

# Unmount /dev/sda1

sudo umount/dev/sda1>/dev/null

if[$?-eq0];then

echo"${GR}*** Drive unmounted successfully ***${NC}"

echo""

else

echo"${RED}***Drive unmounted unsuccessfully ***${NC}"

echo""

fi

echo"${BL}Check Filesystem on USB Disk${NC}"

# Check volume on sda1 - HFS+ formatting on this system

sudo fsck/dev/sda1

echo"${BL}Mount USB Disk For OwnCloud${NC}"

# mount /dev/sda1 to /media/owncloud

sudo mount/dev/sda1/media/owncloud>/dev/null

if[$?-eq0];then

echo"${GR}*** Drive mounted successfully ***${NC}"

echo""

else

echo"${RED}*** Drive mounted unsuccessfully ***${NC}"

echo""

fi

echo"${BL}Restarting Apache${NC}"

# Restart apache to enable owncloud to use the USB disk

sudo service apache2 restart>/dev/null

if[$?-eq0];then

echo"${GR}*** Apache started successfully ***${NC}"

echo""

else

echo"${GR}*** Apache started unsuccessfully ***${NC}"

echo""

fi

echo"${BL}You're ${GR}good${BL} to go!${NC}"

echo""

This script will look like this when run:

Sets you up for the day…

MOTD

This script replaces the .bash_profile in your user’s home folder. When it come to Raspberry Pi scripts, the MOTD is the one you will see most often (Especially if you’ve figured out cron!) so it’s a good idea to make it useful. Pretty also helps.

Whenever you log on the your Raspberry Pi using SSH (or when your not using graphical mode), .bash_profile runs by default and the message displayed above the user prompt.

Shell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

### BEGIN INIT INFO

# Provides: Message Of The Day (MOTD)

# Required-Start: $local_fs

# Required-Stop: $local_fs

# Default-Start: 2 3 4 5

# Default-Stop: 0 1 6

# Short-Description:

# Description: Message Of The Day (MOTD) - Includes System information, local weather info and a reminder to run the post reboot script manually.