Raspberry Pi Tutorial
Estimated reading: 4 minutes 25 views

🧠 Raspberry Pi – Programming & Scripting: Python, Shell, Cron Jobs & Services

🧲 Introduction – Bring Your Raspberry Pi to Life with Code

Raspberry Pi isn’t just a microcomputerβ€”it’s a playground for programming. Whether you’re automating sensors, writing Linux services, or scheduling scripts, Python, Bash, and Java are powerful tools to drive your Pi projects.

🎯 In this guide, you’ll learn:

  • How to control GPIO with Python
  • How to run Java programs on the Pi
  • How to write shell scripts for automation
  • How to use cron to schedule tasks
  • How to manage background services with systemd

πŸ“˜ Topics Covered

πŸ”Ή TopicπŸ“– Description
🐍 Raspberry Pi – Python with GPIOUse Python to control GPIO pins and hardware
β˜• Raspberry Pi – Java ProgrammingInstall JDK and run Java applications
πŸ–₯️ Raspberry Pi – Shell Scripting BasicsAutomate tasks with Bash scripts and system utilities
πŸ§ͺ Raspberry Pi – Automation Scripts & Cron JobsSchedule Python, Bash, or cleanup jobs using cron
πŸ” Raspberry Pi – Services & systemd UsageCreate and manage services to run scripts at boot or in the background

🐍 Raspberry Pi – Python with GPIO

Python is the go-to language for Raspberry Pi. You can use the RPi.GPIO or gpiozero libraries to interact with the GPIO pins.

πŸ”Ή Basic LED Blink (using RPi.GPIO)

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)

while True:
    GPIO.output(17, GPIO.HIGH)
    time.sleep(1)
    GPIO.output(17, GPIO.LOW)
    time.sleep(1)

βœ… Run with:

sudo python3 led.py

β˜• Raspberry Pi – Java Programming

Install Java and run full Java apps on your Pi:

πŸ”Ή Install OpenJDK:

sudo apt install default-jdk

πŸ”Ή Compile and Run:

Hello.java:

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello from Raspberry Pi!");
    }
}
javac Hello.java
java Hello

βœ… Use Java for backend services, desktop GUIs, and cross-platform apps.


πŸ–₯️ Raspberry Pi – Shell Scripting Basics

Shell scripts allow you to automate tasks, such as backups, updates, or GPIO toggles.

πŸ”Ή Example: toggle_led.sh

#!/bin/bash
echo "17" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio17/direction
echo "1" > /sys/class/gpio/gpio17/value
sleep 2
echo "0" > /sys/class/gpio/gpio17/value

Make it executable:

chmod +x toggle_led.sh

Run with:

sudo ./toggle_led.sh

πŸ§ͺ Raspberry Pi – Automation Scripts & Cron Jobs

Use cron to schedule scripts or commands to run at specific intervals.

πŸ”Ή Open crontab:

crontab -e

πŸ”Ή Schedule Script:

0 7 * * * /home/pi/scripts/temperature_log.py

This runs the script every day at 7 AM.

βœ… Add logging to monitor task success:

0 0 * * * /path/script.sh >> /home/pi/logs/backup.log 2>&1

πŸ” Raspberry Pi – Services & systemd Usage

Use systemd to run scripts or applications as services at startup.

πŸ”Ή Create a Service:

/etc/systemd/system/myservice.service

[Unit]
Description=My Custom Python Service
After=network.target

[Service]
ExecStart=/usr/bin/python3 /home/pi/my_script.py
WorkingDirectory=/home/pi
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

Enable & start it:

sudo systemctl enable myservice
sudo systemctl start myservice

βœ… Check status:

sudo systemctl status myservice

πŸ“Œ Summary – Recap & Next Steps

From blinking LEDs in Python to full system services in Bash or Java, Raspberry Pi offers a rich programming environment that supports automated, scalable, and interactive projects.

πŸ” Key Takeaways:

  • Python is ideal for GPIO and project scripting
  • Java can be used for more structured applications
  • Bash scripts automate system tasks and GPIO control
  • cron helps schedule recurring tasks
  • systemd enables background services that start on boot

βš™οΈ Real-World Applications:

  • Smart lighting or alert systems
  • Scheduled sensor readings and data logging
  • Running servers, APIs, or bots
  • Automating boot-time checks or tasks

❓ Frequently Asked Questions

❓ Can I run multiple Python scripts on Raspberry Pi?
βœ… Yes, but monitor CPU/RAM usage and avoid GPIO pin conflicts.


❓ How do I debug a script that runs at boot?
βœ… Use logs:

journalctl -u myservice.service

❓ How often can a cron job run?
βœ… Every minute using:

* * * * * /path/to/script.sh

❓ Which is better for GPIO – RPi.GPIO or gpiozero?
βœ… gpiozero is easier for beginners; RPi.GPIO offers lower-level control.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

🧠 5. Raspberry Pi – Programming & Scripting

Or Copy Link

CONTENTS
Scroll to Top