24

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?

7 Answers 7

16

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.

4
  • 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
    Commented Mar 1, 2015 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. Commented Apr 13, 2017 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
    Commented Feb 14, 2018 at 11:28
  • The link to raspberrypi.org in the answer is dead. Some instructions for using rc.local are here: learn.sparkfun.com/tutorials/… Please add a new comment if you find better instructions.
    – Bob Brown
    Commented May 15 at 21:18
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

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

3
  • I think just a little more detail is required here
    – Darth Vader
    Commented Apr 10, 2016 at 9:00
  • Worked fine for me!
    – jbyrnes
    Commented Aug 6, 2017 at 14:08
  • Thanks @Palmeta ! I had one program in autostart but actually forgot where I had set it. I looked everywhere. With the configuration tool you described I finally could locate it again and deactivate it :D ! 100 points
    – turbopasi
    Commented Feb 22, 2019 at 15:59
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.

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

1
  • 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
    Commented Dec 19, 2017 at 2: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!

Not the answer you're looking for? Browse other questions tagged or ask your own question.