Running custom scripts on reboot as the root user

Custom scripts on groov EPIC and RIO are extremely useful and powerful, especially when ran as the system administrator, but how do you get them to start up again after a power cycle?
In this post I’ll address one way of doing just that using a simple Python script, crontab, and some careful use of sudo.

Proceed with caution!

As with all things shell, before I dive in I do have to give a warning about SSH access, starting processes at boot, and especially about using root / sudo permissions. This IS very powerful and with great power there must also come great responsibility.
If a custom program set to run on reboot crashes the system your only hope is a bent paperclip to wipe the device – so always thoroughly test your scripts before using cron to start them up, and thoroughly test the cron job before putting it into production. Also be very cautious about how many resources the script consumes since shell has to share the RAM and CPU with control, groov View, Ignition, Node-RED, and everything else running on the device.
One final tip is to have some way of killing it from outside shell, if you have to open SSH, search for the process ID, and kill it manually that’s not very maintainable. Something as simple as a scratch pad area check makes it possible to kill the script from groov Manage or any other program.


OK, with that out of the way, here are the steps I took:

  1. Log into the root user.
sudo su
  1. Set the default editor, replace <editor> with nano or vim.
export EDITOR=/usr/bin/<editor>
  1. Edit the cron job list. If you don’t have any jobs yet, it will create a new crontab for you.
crontab -e
  1. Add your command with the @reboot keyword in front, and be sure to run it in the background. Also make sure you use the entire path to the script for it to work. In this example I just use a Python script in the unsecured file area.
@reboot sudo nohup python /home/dev/unsecured/test.py & 
  1. Log out of the root user.
exit
  1. Cycle power to confirm it’s working, and double check resource usage.
htop

And that’s it! I tested this with a simple script that uses Python to get the current epoch time and write it to the string scratch pad area every five seconds, but only while a scratch pad integer was zero. That way I can write 1 to that integer any time with either groov Manage or the REST API and the script will stop.

If you end up using this method in your own application, and especially if you modify it in some way please drop a line in the thread below. And as always, happy coding!