Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,
I have a script file to reboot machine and i want to change it into a c++ program

<pre>echo "Reboot System"
			echo ""
			echo -n "This will end the demo.  Continue (Y/n)? "

			read continue
			echo ""
			if [ "$continue" != "n" ] && [ "$continue" != "N" ]; then
				cat <<-EOF
				Set reboot scope to System
				Command: echo system > /sys/firmware/zynqmp/shutdown_scope
				EOF

				echo `echo system > /sys/firmware/zynqmp/shutdown_scope`
				echo ""
				echo "Command: reboot"
				echo ""
				reboot
				while true; do
					read dummy
				done
			fi
			;;


What I have tried:

C++
<pre>/*

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <iostream>


/*Function to display available options in this application*/


void Reboot_System()
{
    char option;
    printf("\nReboot System\n");
    printf(".................");
    printf("\n This will end the demo. Continue (Y/N)?");
    scanf("%c", &option);
    if (option ==  'y' || option == 'Y')
    {
        std::string sysfspath ("/sys/firmware/zynqmp/shutdown_scope");

        printf("\n Reboot\n");

    }


}


int main()
{

        printf("\nPower Management");
        printf("\n................");

        Reboot_System();

}
Posted
Updated 19-Jun-21 2:51am
v2

1 solution

Assuming y are on linux, you can use the reboot() system call: see here[^]
Alternatively, you can use the system() e.g.
C++
system("/usr/sbin/reboot");
Depending on your use case, maybe /usr/sbin/shutdown might be a better option. The shutdown command has additional arguments to allow a grace period before the system goes down so
shutdown -r +5
tells the system to reboot (-r flag) in 5 minutes, and sends a warning to all users about the impending reboot.
 
Share this answer
 
Comments
Member 15253975 19-Jun-21 9:27am    
Thanks, but i need to go to that path given in the script file then it will reboot...
k5054 19-Jun-21 14:10pm    
Your script does not "go" anywhere. Maybe you mean you need to do the equivalent of
echo `echo system > /sys/firmware/zynqmp/shutdown_scope`
. If so, there a 2 options. Either use the system() call, or open,write,close the file programatically e.g.
// use system
system("echo system > /sys/firmware/zynqmp/shutdown_scope");

// open, write, close FILE
FILE *scope = fopen("/sys/firmware/zynqmp/shutdown_scope", "w");
fprintf(scope, "system\n");
fclose(scope);
Note that I have not done any error checking, so that should probably be something you should consider. If the system() call might produce an error or other feedback, take a look a pope().
Member 15253975 20-Jun-21 9:07am    
Thank you very much....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900