0

I have a project and it is running a c program on the PI. The program is meant to start running at startup.

So far I have tried to do this with rclocal, but this is depricated. Afterwards I tried using SystemD, but I kept getting startup issues when the service tried to start. Now I would like to try crontab, but this is only documented for python.

Anybody who can help me out/got a solution?

J. Joly
  • 1
  • 2
  • 2
    If the program should run at startup, and keep running - like a service - then you need to solve your issues with systemd or switch to another init system. – Gerard H. Pille May 25 '18 at 07:39
  • 2
    You can't "run a c program"; you can run a compiled program, and the language is irrelevant. crontab will run ANY executable. If you want an answer you need to say what YOU tried – Milliways May 25 '18 at 08:59
  • Seems you have problems with any way to start your program at boot. The best way is to use systemd so I would suggest to fix the problems with it. What in detail was it? – Ingo May 25 '18 at 10:59
  • We made a service with systemd, Enabeld it, so it should start running. But when the program started, it crashed. Afterwards we tried to run the program in the terminal. This worked fine. – J. Joly May 25 '18 at 11:10
  • What did systemctl status yourservice and journalctl --pager-end --unit=yourservice say? How looks the yourservice.service file? Please address me with @ingo. Otherwise I don't get a notification about your comment. – Ingo May 25 '18 at 13:26
  • @J.Joly said, "I would like to try crontab, but this is only documented for python." Begging your pardon, but cron will work equally well for any executable program as it does for Python. What documentation are you viewing? – Seamus May 25 '18 at 14:17
  • @Seamus Ah sorry, your question from comment above was to me? @J.Joly asked: "How to run a C program at boot time" and he has problems with all solutions doing that. I have read man 5 crontab. It says: "Please note that startup, as far as @reboot is concerned, is the time when the cron(8) daemon startup. In particular, it may be before some system daemons, or other facilities, were startup. This is due to the boot order sequence of the machine." So you may run into problems if your program needs specific services. systemd is made for booting and can check dependencies. – Ingo May 26 '18 at 08:06
  • @Ingo - actually, my message wasn't to you, but I'm happy to have that information - thanks. Oddly, I've never had an issue running anything from @reboot, but it's good to know! – Seamus May 26 '18 at 13:24
  • @Seamus the problem is that with systemd the starting order of services isn't defined. So programs running now with @reboot, may fail in future. – Ingo May 26 '18 at 20:32
  • I can definitely see how that could happen. Some cron implementations don't even implement the @reboot option - perhaps that's why? Mea culpa: Without doing any research, I assumed those that did implement @reboot did so with a "guarantee" that cron would be the last daemon to be started. A faulty assumption! But after all of that, it would seem that this is a reasonable work-around to the vagaries of when cron gets started in the boot process: @reboot ( sleep 30 ; sh /home/pi/script.sh ). Agree? – Seamus May 27 '18 at 14:43

1 Answers1

0

I don't understand what you mean when you suggest crontab is only documented for Python. You clearly have not looked at the documentation as there is no mention of Python.

man 1 crontab

man 5 crontab

The documentation should always be looked at first.

Add an entry such as the following to your crontab (crontab -e).

@reboot /home/pi/myprog

Where myprog is your program and needs to be executable (chmod +x myprog).

If myprog references files make sure you give the full path (or cd into the expected working directory in the crontab command).

joan
  • 71,024
  • 5
  • 73
  • 106
  • I've looked up some stuff for the raspberry pi. If you follow this link: link there stands documentation Cron for linux. So that was how I made my conclusion. – J. Joly May 25 '18 at 11:17
  • 2
    The linked page does say "for example". The man pages are always worth studying. – joan May 25 '18 at 11:25