lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

Howto Auto launch a C Program on startup.

Mon Dec 23, 2013 11:02 pm

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);
    }

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Howto Auto launch a C Program on startup.

Mon Dec 23, 2013 11:08 pm

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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

Re: Howto Auto launch a C Program on startup.

Tue Dec 24, 2013 1:16 am

Hi, Mine is a C Program not a script, would that still apply from above info?

shuckle
Posts: 565
Joined: Sun Aug 26, 2012 11:49 am
Location: Finland

Re: Howto Auto launch a C Program on startup.

Tue Dec 24, 2013 5:23 am

Just call it from /etc/rc.local

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Howto Auto launch a C Program on startup.

Tue Dec 24, 2013 8:33 am

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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

hampi
Posts: 223
Joined: Fri May 31, 2013 11:29 am
Contact: Website

Re: Howto Auto launch a C Program on startup.

Tue Dec 24, 2013 9:26 am

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

Code: Select all

kill -s SIGTERM
. 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.

Return to “General discussion”