poilkjewq
Posts: 16
Joined: Fri Jun 15, 2018 9:50 am

How to automatically run a program in Raspberry Pi?

Mon Jul 16, 2018 1:29 pm

Hi, eveyrone!!!

I have a c file and I have complied the file. Now, I have a exe. file. How can I run this program when Raspberry Pi start up.
Also, I want the program will be able to re-run when the program terminate. That's mean I want the program is always running.

Thank You very much!!!! :D :D :D

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

Re: How to automatically run a program in Raspberry Pi?

Mon Jul 16, 2018 1:38 pm

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

B.Goode
Posts: 6544
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to automatically run a program in Raspberry Pi?

Mon Jul 16, 2018 1:43 pm

Will the resulting executable be running under the control of an Operating System such as Raspbian? (Hint: that sort of detail is worth providing along with your question...)

If so, it is worth knowing that the Raspbian Operating System also has to start up processes when the system is booted, and ensure that some of them are always running. The utility or toolset it uses is systemd. You will probably find that systemd will give you the facilities you need to accomplish your task also.


Or will it be a self-contained executable that runs directly on the RPi hardware in place of a conventional Operating System? In which case your query would be better transferred to the "Programming/Bare Metal" sub-forum.


Edit: I see that in the forum race I was beaten by @RaTTuS who indicated the same solution, but leaving this version as well because of the Bare Metal reference.

poilkjewq
Posts: 16
Joined: Fri Jun 15, 2018 9:50 am

Re: How to automatically run a program in Raspberry Pi?

Tue Jul 17, 2018 2:15 pm

Thank you for your both reply! :D
I am using Raspian OS.
However, I still cannot execute the c file when the raspberry pi start up. The following show how I do:

I have server.service in /etc/systemd/system/

Code: Select all

[Service]
WorkingDirectory=/home/pi/server
ExecStart=node /home/pi/server/server.c
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=server
User=pi
Group=root
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
Then, I type the following in command line:

Code: Select all

$ sudo cp   server.service  /etc/systemd/system/
$ sudo chmod u+rwx /etc/systemd/system/server.service

Code: Select all

$ sudo systemctl enable  propanel

Code: Select all

sudo systemctl start   propanel
After that, I reboot the raspberry pi. It doesn't run. And I found that server.service changed like the following:

Code: Select all

[Service]
WorkingDirectory=/home/pi
ExecStart=node /home/pi/server.c
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=propanel
User=root
Group=root
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
Any ways to solve this or other methods to auto run the C program?

Thanks a lot!

B.Goode
Posts: 6544
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to automatically run a program in Raspberry Pi?

Tue Jul 17, 2018 2:18 pm

Whether you do it under the control of systemd or manually/interactively, you cannot 'run' a .c file.

poilkjewq
Posts: 16
Joined: Fri Jun 15, 2018 9:50 am

Re: How to automatically run a program in Raspberry Pi?

Tue Jul 17, 2018 3:35 pm

B.Goode wrote:
Tue Jul 17, 2018 2:18 pm
Whether you do it under the control of systemd or manually/interactively, you cannot 'run' a .c file.
I don't know whether I am doing it under the control of systemd, any ways to know it?

Thank you very much! :D

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

Re: How to automatically run a program in Raspberry Pi?

Tue Jul 17, 2018 4:17 pm

B.Goode wrote:
Tue Jul 17, 2018 2:18 pm
Whether you do it under the control of systemd or manually/interactively, you cannot 'run' a .c file.
I wonder if this is actually JavaScript?
ExecStart=node /home/pi/server.c

Heater
Posts: 10280
Joined: Tue Jul 17, 2012 3:02 pm

Re: How to automatically run a program in Raspberry Pi?

Tue Jul 17, 2018 4:41 pm

ExecStart=node /home/pi/server/server.c
I suggest that you do not name your javascript programs for node.js with a ".c" suffix. Better to use ".js". Else you will confuse yourself and everybody else.

Also suggest you specify the full path to node in that systemd config line. E.g.

ExecStart=/usr/bin/node /home/pi/server/server.c

I'm not sure if the "/usr/bin" part is correct for Raspbian I always have a custome node installation.

You can find the correct path by using the "which" command:

Code: Select all

$ which node
/usr/bin/node

Heater
Posts: 10280
Joined: Tue Jul 17, 2012 3:02 pm

Re: How to automatically run a program in Raspberry Pi?

Tue Jul 17, 2018 4:49 pm

Oh wait:
$ sudo systemctl enable propanel
Our OP is working from an example of using systemd to start a node.js process that I posted here years ago.

"propanel" is the name of my project that was just given as an example in my old post. Thought I made that clear in the old post.

The command:

$ sudo systemd start propanel

In my example starts a service from a service file called "propanel.service". Specifically /etc/systemd/system/propanel.service

You should change all occurrences of "propanel" in my old example to whatever name your new program will have.

B.Goode
Posts: 6544
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to automatically run a program in Raspberry Pi?

Tue Jul 17, 2018 5:09 pm

jahboater wrote:
Tue Jul 17, 2018 4:17 pm
B.Goode wrote:
Tue Jul 17, 2018 2:18 pm
Whether you do it under the control of systemd or manually/interactively, you cannot 'run' a .c file.
I wonder if this is actually JavaScript?
ExecStart=node /home/pi/server.c


Oh, good catch @jahboater!

I got seduced by the opening post which said
I have a c file and I have complied the file. Now, I have a exe. file. How can I run this program when Raspberry Pi start up.


It never crossed my mind that "a c file" might contain anything other than C-language source code.

I won't be contributing anything further...

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

Re: How to automatically run a program in Raspberry Pi?

Tue Jul 17, 2018 5:39 pm

B.Goode wrote:
Tue Jul 17, 2018 5:09 pm
I got seduced by the opening post which said
I have a c file and I have complied the file. Now, I have a exe. file.
Well that clearly indicates a "compiled" C program to a Windows executable !

Confused ....

poilkjewq
Posts: 16
Joined: Fri Jun 15, 2018 9:50 am

Re: How to automatically run a program in Raspberry Pi?

Tue Jul 17, 2018 6:07 pm

Thank you for your reply!!!

I am sorry that I am not clear to said that. Actually, my server.c is C++ program but I can use "g++ server.c -o server -lwiringPi" to compile it. After that, I got a server exe file.

Is there any methods to run my server.c file when the Pi boots and auto re-run when terminate. The above that I was used only for javascript?

Thanks a lot. :D

poilkjewq
Posts: 16
Joined: Fri Jun 15, 2018 9:50 am

Re: How to automatically run a program in Raspberry Pi?

Tue Jul 17, 2018 6:11 pm

Heater wrote:
Tue Jul 17, 2018 4:49 pm
Oh wait:
$ sudo systemctl enable propanel
Our OP is working from an example of using systemd to start a node.js process that I posted here years ago.

"propanel" is the name of my project that was just given as an example in my old post. Thought I made that clear in the old post.

The command:

$ sudo systemd start propanel

In my example starts a service from a service file called "propanel.service". Specifically /etc/systemd/system/propanel.service

You should change all occurrences of "propanel" in my old example to whatever name your new program will have.
I am so sorry that it's typo. I try to use

Code: Select all

$ sudo systemd start server
but it still can't work.

User avatar
rpdom
Posts: 12948
Joined: Sun May 06, 2012 5:17 am
Location: Ankh-Morpork

Re: How to automatically run a program in Raspberry Pi?

Tue Jul 17, 2018 6:21 pm

poilkjewq wrote:
Tue Jul 17, 2018 6:07 pm
Actually, my server.c is C++ program but I can use "g++ server.c -o server -lwiringPi" to compile it. After that, I got a server exe file.
That would be a "server" executable file. Not an "exe" which is a windows thing.

So, your file should be called server and the line to run it should be

Code: Select all

ExecStart=/home/pi/server/server
No "node" and no ".c".

Heater
Posts: 10280
Joined: Tue Jul 17, 2012 3:02 pm

Re: How to automatically run a program in Raspberry Pi?

Tue Jul 17, 2018 6:26 pm

Please try to answer the questions people have asked here. They are trying to help you but you are not helping them to help you. If you see what I mean.

You have said "c file and I have compiled the file" which implies that you now have an executable that runs on Raspbian. Does that executable run when you run it manually from the command line?

Your service file indicates it is trying to run "service.c". I assume this is a mistake. Your source code may well be called service.c but your executable should not have a .c extension. Perhaps it is is just called "service" ?

Your service file also indicates it is trying to use node to run that thing. That must be a mistake if it is regular executable file. Remove the "node ".

To try and debug this use the command:

$ systemctl status myProg

Where "myProg" is whatever you called your program and is in the service file name. Like "/etc/systemd/system/myProg.service

Also, after you have tried to start the thing have a look in your log files.

$ cat /var/log/syslog

Any error messages should be in there.

poilkjewq
Posts: 16
Joined: Fri Jun 15, 2018 9:50 am

Re: How to automatically run a program in Raspberry Pi?

Tue Jul 17, 2018 6:34 pm

Thank you for your both helpful reply and also sorry for my bad English that confuses you.

I will try it two days later because I placed the raspberry pi at the school.

Return to “General discussion”