π§ 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 GPIO | Use Python to control GPIO pins and hardware |
β Raspberry Pi β Java Programming | Install JDK and run Java applications |
π₯οΈ Raspberry Pi β Shell Scripting Basics | Automate tasks with Bash scripts and system utilities |
π§ͺ Raspberry Pi β Automation Scripts & Cron Jobs | Schedule Python, Bash, or cleanup jobs using cron |
π Raspberry Pi β Services & systemd Usage | Create 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 taskssystemd
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 :