Page 1 of 1
Howto Auto launch a C Program on startup.
Posted: Mon Dec 23, 2013 11:02 pm
by lilzz
I am making a C program and would like to auto run on startup.
How I can accomplish that?
Also on my program I have a loop checking forever to see if certain pin becoming low.
Code: Select all
while (1)
{
if (bcm2835_gpio_eds(PIN))
{
// Now clear the eds flag by setting it to 1
bcm2835_gpio_set_eds(PIN);
printf("low event detect for pin 15\n");
}
// wait a bit
delay(500);
}
Re: Howto Auto launch a C Program on startup.
Posted: Mon Dec 23, 2013 11:08 pm
by DougieLawson
Add a script called /etc/init.d/scriptnamehere
Activate it with update-rc.d scriptnamehere enable
There's a skeleton /etc/init.d/skeleton to show how to construct those scripts.
Re: Howto Auto launch a C Program on startup.
Posted: Tue Dec 24, 2013 1:16 am
by lilzz
Hi, Mine is a C Program not a script, would that still apply from above info?
Re: Howto Auto launch a C Program on startup.
Posted: Tue Dec 24, 2013 5:23 am
by shuckle
Just call it from /etc/rc.local
Re: Howto Auto launch a C Program on startup.
Posted: Tue Dec 24, 2013 8:33 am
by DougieLawson
shuckle wrote:Just call it from /etc/rc.local
That's ok when you have one simple thing to call, but when you add the code to run your fifth, tenth or twentieth process you're going to curse not doing it the "right" way.
rc.local is a tactical solution, init scripts are the strategic way.
It doesn't matter what programming language it runs like a bash shell script with root (uid0) privileges.
Re: Howto Auto launch a C Program on startup.
Posted: Tue Dec 24, 2013 9:26 am
by hampi
The daemons like 'httpd' or 'atd' are programs like that. My program 'pipicpowerd' is one example of a DIY daemon:
https://github.com/oh7bf/PiPIC/tree/master/pipicpowerd
The process for creating own daemon is about following: First write a short manual page to describe what the program is supposed to do. This can be updated later when you modify the code or add new features to it. Then write the skeleton for the daemon. This has the logging function that is needed for the debugging. Write a short Makefile to compile the code. Compile the code and try that it works. The logfile should have some comment like
Code: Select all
2013 Dec 24 10:00:00 mydaemon started
and
Code: Select all
2013 Dec 24 10:00:30 mydaemon stopped
when you kill the daemon process with
. Now you are ready to write the first code that is doing something useful. Add only one new feature at a time and test it immendiately to see that the program is still working as it should.