• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

Rebooting cleanly from a program

I have an application where if everything goes titsup.com i would like to restart the machine.

So far i have found the reboot(LINUX_REBOOT_CMD_RESTART) command, but this is like pressing the reset button, just an instant reset, witout unmounting or closing anything down first.
I've also tried the line system("/sbin/shutdown -a -r now") with the user entred into the shutdown.allow in the /etc/ directory but this doesn't work either in the program or from a terminal window, only if the terminal is switched to root.

Can anyone help with this?

  • 2
1 Solution
What about installing sudo and using that to run the shutdown command?
P_ShepAuthor Commented:
hmm, good idea, but how do you get round the password?
You can use the NOPASSWD tag in your sudoers file, something like
user ALL= NOPASSWD: /sbin/shutdown
One way is to create a new user, give it a name something like "shutmedown" with a UID of zero (ie root) and no password. That user, instead of having /etc/bash as the shell, has /etc/shutmedown as the shell. Create a script /etc/shutmedown:


init 6

Make sure that the script is executable. Now all you have to do is:
su - shutmedown

and the machine runs the script as root and shuts down and reboots.

This idea is particularly useful with other flavours of Unix. For example if the script had init 5 on a Solaris box, anyone can shut the machine down and power it off by logging in as shutmedown.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now