23

I've built a wireless sensor network consisting of two Raspberry Pi's (runnnig Raspbian) with sensors on and a base station Raspberry Pi that collects the data.

Each time I want to run the system I have to log into each sensor unit and run two looping Python programs, one for collecting sensor data and one for transmitting to the base station.

These units are going into awkward locations with no Internet access so taking a monitor, keyboard and mouse with me to get them running isn't exactly easy. Is there any way I can set them up to automatically log in, run program 1 and then run program 2 automatically on startup?

Bex
  • 2,919
  • 3
  • 25
  • 34
Kersakov
  • 415
  • 2
  • 5
  • 9

7 Answers7

15

You could add those 2 programs to the rc.local file. This will run them at startup. More info can be found at http://www.raspberrypi.org/documentation/linux/usage/rc-local.md.

Xer0FyT
  • 186
  • 1
  • 3
  • 3
    A point not in that doc that seems to trip people up: Do not use `sudo` in `rc.local` itself. It is run with root privileges at boot. Be sure to pay attention to the part about using `&` unless your program just does something quickly then exits. – goldilocks Mar 01 '15 at 12:47
  • If you want your programs to run after network is up, and `/path/to/somescript.sh &` to `rc.local`, and in the script add `while ! ping -c 1 -W 1 8.8.8.8; do sleep 1; done;` before starting your programs. – Tor Klingberg Apr 13 '17 at 10:34
  • +goldilocks The problems is that you can't achieve write access to `rc.local` without running sudo beforehand. What do you suggest? – Rincewind Feb 14 '18 at 11:28
15

The answer by Xer0FyT is probably the simplest way to automatically start a program on the PI. But problems occur once the started program crashes for any reason because it won't get restarted, since there no process monitoring. I personally would recommend using daemontools instead. It's pretty simple to setup (and is included in Raspbian). Basically you create a service directory which contains a run shell script that starts your program. daemontools will then make sure that your program is started and restarted should it crash for any reason.

Setting daemontools up is pretty simple. Just

apt-get install daemontools daemontools-run

Then create your service directory containing an executable run script:

# create the service directory
mkdir -p /service/my-service

# create the run script
cat > /service/my-service/run <<EOF
#!/bin/sh
echo "I'm an example service executed by daemontools"
sleep 1
# Replace those 2 lines with a real call to your program like this:
# exec /my/program.py --arguments
EOF

# make it executable
chmod 755 /service/my-service/run

Have a look at /service/my-service/run and edit it, so it starts your own program instead of running echo. Once that's done, symlink that directory into /etc/service so daemontools automatically (re)starts it:

cd /etc/service
ln -s /service/my-service .

After about 5 seconds your program should run. You can start/stop it using

# stop it
$ svc -d /service/my-service

# start it
$ svc -u /service/my-service

It's also possible to log to (for example) syslog. So the output of your program doesn't get lost. I wrote a more complete blog post about this here: https://info-beamer.com/blog/running-info-beamer-in-production

dividuum
  • 376
  • 2
  • 3
9
  1. In Raspian click Menu,Preferences,Main Menu Editor
  2. Click Preferences and check Default applications for LXSession
  3. Click OK and close main menu editor
  4. Now click Menu and Under Preferences click on Default applications for LXSession
  5. LXSession configuration opens
  6. Click Autostart
  7. Under Manual autostarted applications paste in your java command line
  8. click add
  9. close the LXSession configuration application and reboot your pi

    your java app should run after reboot

Palmeta
  • 91
  • 1
  • 1
3

This question was asked nearly 6 years ago, and remains a relevant question. However - the accepted answer is no longer relevant because rc.local has been deprecated.

There are (at least) two alternatives to rc.local that - as of this date - are valid and supported:

  1. systemd

  2. cron

In comparison, cron is simpler, systemd is more precise. In general, either one will work for most requirements.

David
  • 683
  • 4
  • 21
Seamus
  • 20,552
  • 3
  • 31
  • 67
2

You probably already have cron installed for some of the PI's housekeeping tasks.

Search man 5 crontab for @reboot.

0

You might want to research service. You can create an init script that is a part of the system's init, and put it at an appropriate runlevel.

Run man service.

Here is a guide to creating init scripts: http://www.novell.com/coolsolutions/feature/15380.html

Bex
  • 2,919
  • 3
  • 25
  • 34
  • 2
    Sadly, with the lemming-like rush towards `systemd` of most Linux distributions, including Raspbian, this answer will need updating if the OP updates their system so that takes over from sysV `init`... 8-( – SlySven Dec 19 '17 at 02:43
0

Just follow this link.

Assuming you have test.txt on desktop, an example could be:

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
@leafpad /home/pi/Desktop/test.txt

and it works!

Ghanima
  • 15,767
  • 15
  • 60
  • 114