0

Using CentOS 7 I want to shutdown a Windows machine before the Linux server powers down.

I have been tinkering around with net rpc / samba, but I can't get it to work.

Most question I found like mine are the other way around (using a Windows machine to shutdown a Linux machine

1 Answer 1

0

You can use net rpc to do this - the syntax is:

net rpc SHUTDOWN -f -I <IP Address of Windows PC> -U <windows-username>

If you are looking to script this you will need to have the windows credentials saved on the linux machine - this can range from saving in plain text to kerberos authentication.

Here is a very similar question with some add'l info on the credential side of things: https://serverfault.com/questions/539699/shutting-down-a-windows-workstation-remotely-from-linux

Update

You can create a simple systemd service that will be triggered during a power down (shutdown or reboot) event. There are two primary approaches to a shutdown / reboot triggered systemd service

  • Trigger on the service (your service) being told to shutdown. This indicates the system is likely coming down BUT it could also mean that the service itself was just told to stop; e.g. by a user. This is the approach used below.

  • Trigger on reboot, shutdown and kexec targets / events. I've found this to be less reliable and avoid them. On your centos 7 system you probably have a service (likely not enabled) installed with an example of this - /usr/lib/systemd/system/canberra-system-shutdown-reboot.service

Here's how to create the systemd service

  1. Create a file named (something like) winpcshutdown.service. It needs to end with the suffix .service, with these contents

file: winpcshutdown.service

[Unit]
Description=Issue Poweroff Command to Win PC

[Service]
ExecStop=/usr/local/sbin/shutdownScript.sh
Type=oneshot
RemainAfterExit=yes

[Install]   
WantedBy=multi-user.target
  1. Next,create the target of ExecStop; shutdownScript.sh, which we will move to that path. This script will issue the net command to the win PC. We will also check to make sure that we didn't get a 'stop' command because we were intentionally stopped for reasons unrelated to power cycling. EDIT: I changed the criteria here as it was inconsistent; systemctl is-system-running will return false if the system state is in transition, both in power down and (irrelevant here) power up.

file: shutdownScript.sh

#!/bin/bash
# Check if the service is still enabled; if so then the stop order is legit.
if ! /usr/bin/systemctl -q is-system-running ; then
    /usr/bin/net rpc SHUTDOWN -f -I <IP Address of Windows PC> -U <windows-username>
else
  #Do Nothing
fi
  1. Run the following commands to move the files into the correct locations and to start the systemd service

chmod 755 shutdownScript.sh
sudo cp shutdownScript.sh /usr/local/sbin/
chmod 644 winpcshutdown.service
sudo cp winpcshutdown.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable winpcshutdown.service
sudo systemctl start winpcshutdown.service

Finally, make sure the service is active by querying it with this command: systemctl status winpcshutdown.service

Common systemd mistakes -

  • Forgetting set execute permissions on the script being called by the service
    • Forgetting the shebang #!/bin/bash in the script; systemd won't execute it without it being defined in the call itself ( /usr/bin/bash script ) or in the header.
  • Not including full paths for every command - the systemd environment is sparse and you can't assume that an executable is on it's path.

The command journalctl can be used to review the logs / events with systemd and your service.

1
  • Sorry for the tardy response, but I have followed all the steps you described. And the Windows machine is not shutting down. (I had to run sudo chmod - to avoid a permission error) When I run the literal command net rpc I used in the shutdownScript.sh the shutdown is successful
    – Martijn
    Commented Dec 18, 2016 at 21:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .