Shutting down Linux with C # and mono

I am working on a cross-platform project with C # / soap and php for Windows and Linux.

The idea is that when the user clicks a button on the web interface, he sends a request for soap, which will stop the server.

This works fine on Windows, but it doesn’t work on Linux, I see no reason why not, as when I run the shutdown command manually on the server, it works fine.

Below is the code I'm using

public bool shutdownServer() { Process process = new Process(); if (CommonTasks.getOperatingSystem() == CommonTasks.OperatingSystemType.Windows) { process.StartInfo.FileName = "shutdown"; process.StartInfo.Arguments = "-s -t 0"; } else if (CommonTasks.getOperatingSystem() == CommonTasks.OperatingSystemType.Linux) { process.StartInfo.FileName = "shutdown"; process.StartInfo.Arguments = "-h now"; } else { return false; } library.logging(classDetails + MethodInfo.GetCurrentMethod().Name, string.Format("Shutting down with the following {0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments)); process.Start(); return true; } 

I see no reason why the linux partition will not perform shutdown, but the Windows version is working fine. What am I doing wrong.

Just to be clear, I'm trying to disconnect the whole server, not the application.

Thanks for any help you can provide.

* UPDATE * Thanks for your suggestions, I think I'm somewhere. I found out that mono works under wwwrun using the janis method.

I added wwwrun: www group to / etc / groups using wwwrun:x:0 , I'm not sure if this bit is correct or not, I don't know why: x: 0.

I added %wheel ALL = (ALL) NOPASSWD: /sbin/shutdown to the sudoers file and restarted apache.

When I start the soap service to perform a shutdown, I get the following error in /var/log/messages

Aug 3 23:28:01 dev-server sudo: pam_unix2 (sudo: auth): conversation failed Aug 3 23:20:27 dev-server sudo: wwwrun: pam_authenticate: Talk error; TTY = unknown; PWD = /; USER = root; COMMAND = / sbin / shutdown -h now

I do not know where to go from here now, sorry if I ask a little question about noob, I am pretty new to soap / linux / mono.

UPDATE 2 Just learned something else, it works on Windows only if the browser is running on a local PC that is shutting down. I tried redirecting the output from the command to see if it returns an error and nothing happens, so I can’t understand why it fails.

UPDATE 3 I just found out something else, I have two computers with a PC, a laptop and a desktop, both Windows 7 x64 and a desktop browser. I can make a soap request to turn off the laptop and vice versa, however, the problem I encountered is related to Windows Server and Linux, even local ones in the Windows browser, will not allow me to close using the soap request. I can make the Windows server allow me to shutdown through a soap request, local and remote browser, and how can I fix a problem with Linux.

UPDATE 4 Just fixed the problem with Windows, I needed to add an IIS user account as an administrator so that she could perform shutdown, so now I just have a problem with Linux.

+4
source share
3 answers

I decided, but all your suggestions helped especially @janisz and @David Harris.

To make Windows work, I had to add an IIS user (IUSR) to the administrator group on Windows Server 2008.

For linux, I needed to run the command as sudoer, as @janisz said. To do this, I need to run the visudo command to add editing to the sudoers file, after which I added the following to the sudoers file.

 User_Alias APACHE = wwwrun Cmnd_Alias SHUTDOWN = /sbin/shutdown APACHE APACHE ALL = (ALL) NOPASSWD: SHUTDOWN 

Then reboot the server.

Running the shutdown command on Windows was nothing like the previous one, but on Linux, because @David Harris pointed to the /sbin/sudo and the arguments /sbin/shutdown -h now

Thank you for your help.

+3
source

The mono user probably does not have privileges to turn off the computer, so the following options are possible:

  • Add a monopoly group to the power group (or any other group that can turn off the machine) useradd -G power monouser

  • Add monouser to sudoers file with permission to run shutdown command without password. After changing the code to call sudo before shutting down. Run visudo and type monouser ALL=NOPASSWD: /sbin/shutdown

  • Change the permissions of the files that monouser can execute.

EDIT I just realized that shutdown not protected, but in normal user $ PATH there is no entry for /sbin/ , so it may add the full path to completion. Therefore, change process.StartInfo.FileName = "shutdown"; on process.StartInfo.FileName = "/sbin/shutdown";

+2
source

I would replace

 process.StartInfo.FileName = "shutdown"; process.StartInfo.Arguments = "-h now"; 

with

 process.StartInfo.FileName = "/usr/bin/sudo"; process.StartInfo.Arguments = "/sbin/shutdown -h now"; 

and have an account that runs the shutdown script installed in the sudoers file with permission to run / sbin / shutdown without a password.

+2
source

All Articles