Auto-Start Programs at Boot Using Crontab with Shell Access

If you have custom code on a groov EPIC that you want to run automatically every time the system boots or reboots, consider using the crontab tool to auto-start it for you.

You can view existing cron jobs with crontab -l , and make changes using the crontab -e command, which will open the crontab file with the text editor vi.

If you’re unfamiliar with vi you should know that it launches in “command mode”, you’ll need to press the i key to enter “insert mode”, make any changes to the file, and then press Esc to go back to command mode. From there you can type :x to save and close the editor, or :q! to force quit without saving.

To add a new cron job that will start at boot, put @reboot in front of your command, and end with an ampersand (&) to run it in the background. Make sure that you include the full path to any programs or files you reference. For example, I put a simple Python script in the unsecured file area, which has the path /home/dev/unsecured, so I added the following line to my crontab to make it run at boot:

@reboot python /home/dev/unsecured/script.py &

Using i to start inserting new text, then Esc and finally :x to save and quit. Once it’s added, double check it with crontab -l and then reboot through groov Manage to test it.

If you start a process in the background that you want to end later, you can use htop or pgrep to find the process ID (PID) and kill $PID to kill the process. For example, I found my Python PID with the command pgrep -f script.py which returned 3310, so a simple kill 3310 stopped it running.

Happy coding!

2 Likes

Is there any other way?

The only other reasonable alternative to starting programs at boot time is systemd, but as near as I can tell, Opto chose not to support it.

What are you trying to run at boot?
What privileges do you need?

We actually have two programs.

First, we have written a program that allows us to send and receive numeric tables to/from an ARCNET of older Opto controllers.

We also have a program that allows a legacy app to communicate with the new groov using the very obsolete “HostWords” interface.

Both of these programs create server sockets.

What are your concerns about using crontab?
Just trying to understand what you are doing and why crontab won’t work for you.

I am in a similar situation where I want to use crontab to run a python script on startup. However, it doesn’t appear to be working. I’ve tried a few things such as checking permissions and specifying paths, but nothing has worked yet. Are there any other special considerations I should think about as far as setting up the crontab file and my own scripts?

Welcome to the forums!

Not really no.
As Terry’s first post shows, its critical that the full path be specified.
It should not matter which user you make the cronjob under.
I mean to be super sure, throw it in root by doing a su and becoming root on the groov device.

Is your script using a specific version of Python? You may need to call it out in a path as well if it is.

For trouble shooting, try running via a cronjob a few minutes into the future so you can watch some logs to see why its not starting (rather than rebooting the device every time - quicker and easier to see your job in the logs)

1 Like

Thanks for replying so quickly!

I did take a look at the logs, and here is the situation:

I have python3.7 installed, so I’ve been putting that in the place of python in my crontab file and in the path at the top of my python scripts (screenshots to follow). When I do that, it gives me the error that python3.7 command can’t be found. But when I use just python in the command and the path on my python scripts, that error disappears, but it can’t find the module optommp, which I’m importing in one of my files. That makes me think that maybe it’s trying to run my python scripts with an older version, but I only have optommp installed for python3.7.

Any thoughts on what may be going wrong here?

There are a lot of moving parts here, but the first thing that comes to mind is that the optommp Python library was originally written for and tested with Python 2.7 – you’ll need to make some changes to have it work with 3.7.
I’ve already tested a modification that will hopefully help in your situation as well: Optommp error :No module named 'O22SIOUT'

If you haven’t already tried that, please give it a go and let us know if that gets past the 3.7 command being unable to find optommp, and we can go from there.


Also, is there a specific reason you’re using python3.7? I’m just curious which packages your script relies on besides optommp.

I’m using 3.7 because someone from OPTO that I talked to had me do that when packages like numpy and scipy were not able to be installed on the already-present version of python.

To clarify, my crontab isn’t recognizing python3.7, only python. It’s when I use just python that optommp is not found.

Also, I just tried the modification above for O22SIOUT, but that didn’t change anything

@keanecs94 What do you see if you enter which python, which python3, and which python3.7 ? Sometimes the pathing / links don’t always resolve the way I expect them to, it’d be helpful to see how your system is reaching “python”.
Also, if you have tried uninstalling any other versions of python or pip; that can break other parts of the EPIC software – they can be recovered by doing a system restore, but I’d definitely suggest that you do a system backup before you delete anything via SSH.

I haven’t done any uninstalling, I just installed python3.7 along with the rest.

image

Have you tried your @reboot cron using that last path to point to the exact python3.7 binary you need to run?

1 Like

I guess I have not, let me try it.

That worked great! Thanks a lot

2 Likes

Awsome!

Cron is really picky about having exact pathing.
Glad we got there in the end.
Thanks for coming back and letting us know - super helpful to close the loop.