Running Shell Script after boot on Raspberry PI

10,122

Solution 1

The reason it wasn't working was because the script needed to be run as the user pi.

I changed the code in the rc.local script to this: su - pi -c "bash /home/pi/logon.sh &"

This makes the script run as the user pi and the ampersand is used to make the script run separate to the rc.local script by forking it. (http://hacktux.com/bash/ampersand)

Solution 2

Put this in your crontab

@reboot /path/to/script

Edit it using

#crontab -e
Share:
10,122
Blind Trevor
Author by

Blind Trevor

I've been programming for as long as I can remember! It all started with BASIC on dos! I now write in PHP, ASP, VB.Net, Java, SQL, VBS, good old fashioned Batch scripts... and loads more too boring to mention here - so that will do for now! I live in Somerset, England with my partner and daughter and work as a network technician for a large national transport company (which I can't name here due to my contract of employement!). I look after the network and the systems, and along side that I write scripts for updates and programmes to attempt to speed my life up! In my spare time I play the drums, piano, guitar, sing and spend a great deal of time watching stand up comedy... and of course spending time with my lovely partner and wonderful daughter :) So there you have it, a fairly generic geek with a musical background and a love of stand up!

Updated on June 07, 2022

Comments

  • Blind Trevor
    Blind Trevor almost 2 years

    I'm making a web kiosk display board using a raspberry pi and I want to send some key strokes to the browser window 2 minutes after it's loaded. The script sends the logon details for a webserver.

    I've got a script that sends the keystrokes which works fine from the telnet console:

    #!/usr/bash
    username="username"
    password="password"
    echo "Setting Display"
    export DISPLAY=:0
    echo "Sending Username"
    for char in $(sed -E s/'(.)'/'\1 '/g <<<"$username"); do
        xdotool key $char
    done
    xdotool key Tab
    echo "Sending Password"
    for char in $(sed -E s/'(.)'/'\1 '/g <<<"$password"); do
        xdotool key $char
    done
    xdotool key Return
    echo "Waiting 5 Seconds"
    sleep 5
    echo "Setting Remember Password"
    xdotool key Tab
    xdotool key Tab
    xdotool key Return
    echo "Finished"
    

    I've tried to add bash /home/pi/logon.sh to the rc.local file - but it doesn't send the keystrokes to the browser?

    Does any one know why that would be? As I say - it works fine from the telnet window if I run the script, but it doesn't work when run from boot.

    I had sleep 120 on the line before it to stop if firing right away and wait until the browser has loaded - and I know the script is running from rc.local, because when I remove the sleep command, I see the echos from the script.

    Any ideas?