harishreddy
Posts: 21
Joined: Wed Jul 11, 2018 9:37 am

Autorun C program on Raspberry Pi

Wed Jul 11, 2018 9:48 am

Hi,

We are using Raspbian whezzy for windows system on raspberry pi 1 model .we are unable to run c program automatically please share lx terminal commands

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 20917
Joined: Sat Jul 30, 2011 7:41 pm

Re: Autorun C program on Raspberry Pi

Wed Jul 11, 2018 10:55 am

Not sure what you are trying to achieve, but somethings that might help/

Windows doesn't run on the Pi, and Window's programs do not run on the Pi.

If you have a C file, you need to compile it before it will run. Have you done that?

Do you mean run automatically when the system starts up?
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Please direct all questions to the forum, I do not do support via PM.

harishreddy
Posts: 21
Joined: Wed Jul 11, 2018 9:37 am

Re: Autorun C program on Raspberry Pi

Thu Jul 12, 2018 7:30 am

#Do you mean run automatically when the system starts up?
Yes

jahboater
Posts: 3257
Joined: Wed Feb 04, 2015 6:38 pm

Re: Autorun C program on Raspberry Pi

Thu Jul 12, 2018 8:13 am

One simple way is to put the full path of your executable in a file called
/etc/rc.local
place it just before the final "exit 0" line.
It will be run as "root", so be careful.
There are other methods, a systemd service or a crontab entry.

User avatar
RaTTuS
Posts: 10120
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: Autorun C program on Raspberry Pi

Thu Jul 12, 2018 8:16 am

why are you using wheezy ?
it make more sense to make a new Stretch version but
viewtopic.php?t=7192
will help
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

DirkS
Posts: 9478
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Autorun C program on Raspberry Pi

Thu Jul 12, 2018 8:17 am

jahboater wrote:
Thu Jul 12, 2018 8:13 am
There are other methods, a systemd service.
OP is running Wheezy, so no systemd...

harishreddy
Posts: 21
Joined: Wed Jul 11, 2018 9:37 am

Re: Autorun C program on Raspberry Pi

Fri Aug 24, 2018 7:36 am

Hi,

We are not using phython for programming.we are using geany editor for c programming.
kindly provide the procedure for program run automatically when the system starts up

DirkS
Posts: 9478
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Autorun C program on Raspberry Pi

Fri Aug 24, 2018 7:40 am

harishreddy wrote:
Fri Aug 24, 2018 7:36 am
Hi,

We are not using phython for programming.we are using geany editor for c programming.
kindly provide the procedure for program run automatically when the system starts up
Irrelevant. Once your program is compiled you use it in the same way as any other program.
Use the links others above have provided.

And you really should drop Wheezy.

bzt
Posts: 206
Joined: Sat Oct 14, 2017 9:57 pm

Re: Autorun C program on Raspberry Pi

Wed Aug 29, 2018 11:11 am

Hi,

jahboater is right, the simplest way (if your application does not need a GUI) to simply execute the compiled binary in /etc/rc.local, like

Code: Select all

/usr/bin/yourcommand
exit 0


On the other hand, if you need a GUI too, then start with a raspbian-lite image, and install these packages:

Code: Select all

sudo apt-get install lightdm openbox
Edit the lightdm configuration in /etc/lightdm/lightdm.conf:

Code: Select all

autologin-user=pi
autologin-user-timeout=0
This will automatically log in as user pi, and will start the script /home/pi/.xsessionrc. In that script file, you'll need:

Code: Select all

export DISPLAY=":0"
xset s off
xset s noblank
xset -dpms
openbox --sm-disable &
bash -c "while true; do /usr/bin/yourcommand; done"
That's all.

The first line redirects the windows to your local display (the Linux GUI, called X11, supports remote network displays as well, so it's mandatory to set).

The xset lines turns off screensaver and screen-blanking (could be important in some cases, for example a kiosk).

The line openbox starts the window manager in the background, which will manage the windows opened from your application. Without a window manager, there's no guarantee where your windows will be placed, and which one is focused. (You can ommit the window manager only if: you have one window, which is as big as the screen, and you programatically move it to (0,0) with XMoveWindow and focus it with XFocusWindow; or if you use SDL with a single fullscreen window. Considering that with most frameworks (GTK, QT etc.) all popups, selectboxes and menulists are implemented as undecorated X windows, it's very likely that your application will need a wm to handle which one is focused.)

Finally, the last line executes your command and starts your compiled application. It also makes sure of it to restart if your app crashes for some reason. This is very important because as soon as your .xsessionrc exited, the control is returned to lightdm, which will not autologin again. (We've started the window manager in the background so that we can run your application in the foreground. Normally it is the wm that's running in the foreground, and your session ends when you exit the window manager.)

I'd like to point out that .xsessionrc is not a standard shell script file, not executed by bash, rather it's sourced from the session manager's script file. Therefore DO NOT use "#!/bin/bash" in the first line, and you need to call bash directly with -c to support "while; do done" loops and other fancy shell stuff.

NOTE1: alternatively you can use xorg-xinit, xhost, sudo packages and an .xinitrc script, but that solution requires more experience to set up correctly.
NOTE2: always use the lastest raspbian build, forget wheezy for your own sake.
NOTE3: if you need a window manager (probably) but want a pure window only without the decorations (icon, title, minimize, maximize, close buttons), you'll have to create /home/pi/openbox_rc.xml with the following:

Code: Select all

<?xml version="1.0" encoding="UTF-8">
<openbox_config xmlns="http://openbox.org/3.4/rc" xmlns:xi="http://www.w3.org/2001/XInclude">
  <applications>
    <application class="*">
      <decor>no</decor>
    </application>
  </applications>
</openbox_config>
This will turn off window decorations for all windows, but you'll be still able to select the focused window with your mouse, and move windows with [ALT]+[Left click] and resize with [ALT]+[Right click].

Cheers,
bzt

jahboater
Posts: 3257
Joined: Wed Feb 04, 2015 6:38 pm

Re: Autorun C program on Raspberry Pi

Wed Aug 29, 2018 12:28 pm

Yet another method is cron

Add a line to /etc/crontab similar to this:-

Code: Select all

@reboot root echo 0 > /sys/class/leds/led1/brightness
(which turns off the power led on a Pi3+).

Return to “C/C++”