Auto Run Script after power on - Raspberry Pi

1.7k views Asked by At

I'm a total noob at programming, and therefore ran into a problem while problem solving on my Raspberry Pi.

I have a .sh script on my desktop that I would like to run after my Raspberry Pi is booted. So far, I have tried the following...

First, I go to the terminal and type:

sudo nano /etc/xdg/lxsession/LXDE/autostart

From there, I then proceed to add the following line to the end of the 'autostart' script:

@! /bin/bash /home/pi/Desktop/MyFile.sh

Unfortunately, my code isn't running when I do these steps.

Any ideas/help would be tremendously appreciated!!!

1

There are 1 answers

0
theCreator On

You can use crontab to make a script run whenever you want. This can be done at startup or at intervals which you set.

To add an entry to crontab type in terminal

crontab -e

it will ask which editor you want to use, just use nano is the easiest imo. You can then add a line to the file that looks like the following

@reboot sh /home/pi/Desktop/myFile.sh

use ctrl+x and hit yes to save the file. This should run your command at startup, if you want to run it in the background you can add a & at the end of that command.

Also, i forgot to mention to make sure the script is executable by using

chmod a+x /home/pi/Desktop/myFile.sh