Auto-Start Programs at Boot Using Crontab with Shell Access

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.

Hey! I’m back. Last time, we were able to figure out path naming issues. However, I was just running my python file every minute, and that was working. Now I have another situation that doesn’t allow me to run the script every minute. I can only run it at reboot (otherwise, new comm handles are opened every minute and they eventually run out, breaking the connection between the scratchpad and my PAC Control). However, the @reboot command does not appear to be working. I commented out the crontab line that made it run every minute, and now it doesn’t run at all, at startup or restart. Any ideas why that may not work? I’ve already found some linux forums suggesting I add a 60 second sleep period first before running the python script, but that didn’t work.

Are you able to share your crontab, either as a screenshot or text that you are using?

Yep! here that is. I commented out the code that I was using to run it every minute.

@torchard and I have beat on this pretty hard on our end and can not get it to fail.
@reboot works every time, with or without the sleep our test python app run just fine.

BTW, putting a space in Linux file names breaks best practices:

Best practice for Linux file naming

To prevent running into problems with your file paths on your site, we recommend following the best practices for file naming. See the below:

  1. Name all your files lower case.
  2. Instead of using a space, use an ( _ ) or a ( – )
  3. Use consistent file types. Use jpg or jpg. Don’t use both.
  4. Only alphanumeric characters, periods, underscores and hyphens and don’t use symbols like “%”, “$”, and so forth.
  5. Keep the file names short and descriptive.

But that said, we tested a space in the file name and fully pathed out with quotes still worked in cron.

I guess we have the following questions:
What happens when you run the script from the console (and not back-grounding it)?
How do you know the script is not working?
How do you know the script is working?

1 Like

Yes, I agree that’s probably not best practice. I’m not a heavy linux user. That aside, here are my answers to your questions.

  1. When I run the script, it outputs my print statements to the terminal every few seconds, as programmed.

  2. I know it’s not working because when I have the job running each minute, as in the commented code, I am able to send inputs (modbus register information) to my controller and my outputs are updated. That is how the script would behave if it were running. But when I comment the code out and restart the controller, my inputs are not received or turned into updated outputs.

Another note is I have tested the use of specific dates and times for starting the script. I selected a time on crontab just a few minutes into the future. I restarted the controller and did nothing to it, but at that time I selected the code started running again. That is why I feel it’s a reboot-specific problem.

Came across some issues when experimenting with this. It seems as soon as I add a “@reboot” entry and reboot the system the whole crontab file gets overwritten by repeated “^@” (some Unix null character?) Running periodic cron jobs works just fine and will persist during reboot. I add them using crontab -e and use the vim editor just as I would otherwise. I’ve never encoutered this issue on any previous Linux system I’ve used