Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shutdown linux using c# and mono

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

The idea is that when the user presses a button on the web interface it sends a soap request which will shutdown the server.

This is working fine on Windows but on Linux its not working, I don't see any reason why not as when I run the shutdown command manually on the server it works fine.

Below is the code that I am 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 can't see any reason why the linux section won't perform the shutdown but the windows version works fine. What am I doing wrong.

Just to be clear I am trying to shutdown the entire server, not the application.

Thanks for any help you can provide

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

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

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

When I run the soap service to perform the 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: Conversation error ; TTY=unknown ; PWD=/ ; USER=root ; COMMAND=/sbin/shutdown -h now

I don't know where to go from here now, sorry if I'm asking a bit of a noob question, I'm fairly new to soap/linux/mono.

UPDATE 2 Have just found out something else, it works on Windows only if the browser is running on the local PC that is being shutdown. I've tried redirecting output from the command to see if it is returning an error and nothing is, so I can't find out why it is failing.

UPDATE 3 I've just found out something else, I have two windows PC, laptop and desktop both Windows 7 x64, from the desktop browser I can do the soap request to shutdown the laptop and visa versa, however, the problem I am having is with Windows Server and Linux, even local on the Windows server browser won't let me shut itself down via the soap request. Is there a way I can make it so Windows server will allow me to do the shutdown via the soap request, local and remote browser and how can I fix the problem with Linux.

UPDATE 4 Have just fixed the Windows problem, I needed to add the IIS user account as an administrator to allow it to perform the shutdown, so its just Linux I have a problem with now.

like image 482
Boardy Avatar asked Feb 20 '23 15:02

Boardy


2 Answers

I'd 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 the account that executes the shutdown script set up in the sudoers file with permission to execute /sbin/shutdown with no password.

like image 74
David Harris Avatar answered Feb 23 '23 17:02

David Harris


I've got it solved but all of your suggestions did help especially @janisz and @David Harris.

In order to get Windows to work I had to add the 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 command visudo in order to add edit the sudoers file, I then added the following into the sudoers file.

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

Then rebooted the server.

To execute the shutdown command on Windows wasn't any different as before but on Linux as @David Harris pointed running the command as /sbin/sudo and the arguements /sbin/shutdown -h now

Thanks for your help.

like image 33
Boardy Avatar answered Feb 23 '23 19:02

Boardy