Run a Script on Edison Boot

Edison Base Block

Image courtesy of sparkfun.com

This one took me forever to figure out, and thanks to some posts over at the Intel Edison forums, I finally managed to get a JavaScript program to run as soon as the Edison starts. I know that the XDK also runs a service to execute JavaScript programs on boot, but I wanted a more general solution. One could configure the systemd service file to run any program/script on boot (assuming the Edison has the right interpreter).

We’ll use a simple JavaScript program:

In myscript.js, write the following (‘i’ to insert text):

Save and exit (‘esc’ and enter “:wq”). To have this script run on boot, we’ll need to create our own service in systemd:

In this file, enter:

Save and exit. Note that we are specifically calling Node to run our script, and we have to set the environment variables manually. Additionally, we are waiting for Intel’s XDK service to start before we run our script (this ensures that all the necessary Linux modules were loaded, as the XDK service is one of the last things to run at boot).

Have systemd scan for our new service and enable the service to have it run on boot. Then, reboot.

You won’t see anything appear in the terminal, as the systemd service outputs to syslog (and not the console). However, we can check the output by entering the command:

You should see:

Output of systemd service

Here are some other helpful commands:

  • systemctl stop myscript.service to stop the service
  • systemctl start myscript.service to start the service
  • systemctl restart myscript.service to restart the service

 

Leave a Reply