Edison Base Block
Affiliate Disclosure: Some links on this blog are affiliate links, meaning I earn a commission at no extra cost to you. I only recommend products and services I trust and use myself.

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:

cd /home/root/
vi myscript.js

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

setInterval( function() {
    console.log("Hello");
}, 1000);

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

vi /lib/systemd/system/myscript.service

In this file, enter:

[Unit]
Description=My service
After=xdk-daemon.service
Requires=xdk-daemon.service

[Service]
TimeoutStartSec=1
ExecStart=/usr/bin/node /home/root/myscript.js
Environment="NODE_PATH=/usr/lib/node_modules/"
Environment="NODE_ENV=production"
Environment="AVAHI_COMPAT_NOWARN=1"
Environment="HOME=/home/root"

[Install]
WantedBy=multi-user.target

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.

systemctl daemon-reload
systemctl enable myscript.service
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:

systemctl status myscript.service

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 Comment

Your email address will not be published. Marked fields are required.