# Run as service from c++ program

**URL:** <https://discourse.nodered.org/t/run-as-service-from-c-program/66653>\
**Category:** General\
**Created:** [19 August 2022 08:58 UTC](https://discourse.nodered.org/t/run-as-service-from-c-program/66653 "2022-08-19T08:58:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)\
**Post date:** [19 August 2022 08:58 UTC](https://discourse.nodered.org/t/run-as-service-from-c-program/66653/1 "2022-08-19T08:58:29Z")

</div>

Hi, I'm facing this problem, even if I know it isn't strictly related with node-red itself.  
I have installed node-red on debian with the raccomanded script, so I can run it as service.  
My problem is that I don't have to start it at boot (so I disabled its service). What I need is to start it from a C++ program., but I cannot find the right command, even because calling it as service it requires the password.  
I tryed this command but it works only from comman line, not from within the c++ program

> echo psw | sudo -S sudo systemctl start node-red.service

Thanks

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [19 August 2022 09:11 UTC](https://discourse.nodered.org/t/run-as-service-from-c-program/66653/2 "2022-08-19T09:11:11Z")

</div>

Have you already tried

`echo psw | sudo -S systemctl start nodered`

---

<div class="post-metadata">

**Author:** ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)\
**Post date:** [19 August 2022 09:22 UTC](https://discourse.nodered.org/t/run-as-service-from-c-program/66653/3 "2022-08-19T09:22:37Z")

</div>

yes, nothing. The strange thing is that it still shows the string after "echo", so pratically it show (as for your suggestion)

> psw | sudo -S systemctl start nodered

The code is:

```auto
int main(void){
	char *args[]={(char *)"echo",
				  (char *)"psw",
				  (char *)"|",
				  (char *)"sudo",
				  (char *)"-S",
				  (char *)"sudo",
				  (char *)"systemctl",
				  (char *)"start",
				  (char *)"node-red.service",
					NULL};
	printf("\nstart Nodered...");				
	execvp(args[0],args);
	printf("Nodered running!");
	return 0;
}

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 August 2022 09:50 UTC](https://discourse.nodered.org/t/run-as-service-from-c-program/66653/4 "2022-08-19T09:50:15Z")

</div>

First, you need to give the user running the C++ program the authority to run the systemctl command without a password. You can do that using the command `sudo visudo` and give the user the appropriate permissions by adding something like  
`the_user ALL=NOPASSWD:/bin/systemctl start nodered.service`

Then you can test that by logging on as that user and running  
`sudo systemctl start nodered.service`  
and it should not ask for a password.

Second note that it is nodered.service, not node-red.service.

Third, in the command you run it is best to give the full path to systemctl in case the path is not setup in the environment you run the command.

---

<div class="post-metadata">

**Author:** ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)\
**Post date:** [19 August 2022 11:11 UTC](https://discourse.nodered.org/t/run-as-service-from-c-program/66653/5 "2022-08-19T11:11:00Z")

</div>

Thanks, it helps a lot. OK. Now I have this situation:

- from command line it works correctly without asking for the password
- I created a new service different from the default one, this why nodered.service
- putting the full patch in the command it asks for the password:  
sudo systemctl start /etc/systemd/system/nodered.service
- without full path  
sudo systemctl start nodered.service  
it works but: the program stops and exit after execute the execvp command. I Don't know if it is normal, but aswell it doesn't print on video the printf line

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 August 2022 11:31 UTC](https://discourse.nodered.org/t/run-as-service-from-c-program/66653/6 "2022-08-19T11:31:53Z")

</div>

> [@Lupin\_III](#):
>
> putting the full patch in the command it asks for the password:  
> sudo systemctl start /etc/systemd/system/nodered.service

That is because the command parameters are different to the ones you specified. I meant the full path to the command (systemctl) not the service.

---

<div class="post-metadata">

**Author:** ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)\
**Post date:** [19 August 2022 13:24 UTC](https://discourse.nodered.org/t/run-as-service-from-c-program/66653/7 "2022-08-19T13:24:26Z")

</div>

Thanks so much. Solved and working great

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [2 September 2022 13:24 UTC](https://discourse.nodered.org/t/run-as-service-from-c-program/66653/8 "2022-09-02T13:24:58Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
