1

I have a "media center" PC that sometimes I forget to shut down at night. If I remember, I'll usually remote into my "media center" PC and shut it down, but most nights I forget to do it and the PC runs all night (and throughout the next day too).

Is there a way to shutdown the PC via a dialog box (ie. "Do you want to shutdown?" [Yes] [No])? The reason I want a dialog box is because sometimes we are watching movies late at night and I don't want to arbitrarily shutdown.

I can do a cron job which runs a script at 1am but I can't figure out how to execute poweroff or shutdown as a standard user. Any help? I'm running Linux Mint 17.3 (Ubuntu 14.04).

2 Answers 2

3

Is there a way to shutdown the PC via a dialog box (ie. "Do you want to shutdown?" [Yes] [No])?

I believe the simplest way to do this is via xmessage. Something like this:

#!/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export DISPLAY=:0.0

xmessage -timeout 10 "Shutdown computer?" -buttons yes:0,no:1

case $? in
    0)  
    echo "Shutting down..."
    #systemctl poweroff
    ;;  
    1)  
    echo "Ok."
    ;;  
    *)  
    echo "Mystic error!"
    ;;  
esac

I have added the $PATH and $DISPLAY variables so that it can be used by cron (more on that here). And, of course, if you want to shutdown your computer you will have to uncomment that line, and you can of course remove the echo statements as well. I just threw this together quickly as a sample.

The gist of it: When a user presses yes the return code is 0 and the computer shuts down. If a user presses no the return code is 1, then do not shutdown. (Edit:) If no user presses any button after timeout seconds (10 in this case), xmessage will exit with 0 and the computer will shutdown.

When executed, the script produces this little dialog box:

https://s22.postimg.org/irl97w835/xmessage.png

I am sure that zenity is great, but for a simple task like this I think xmessage might be easiest.

2
  • Neat! Is there a way to add a timeout such that if no action is taken (ie. No user is using the PC) then shutdown occurs? This may require some actual development but luckily I'm a Python dev by trade :D. If nothing simple is available this should work in the mean time. Thanks
    – notorious
    Oct 2, 2016 at 16:51
  • @notorious Yes, that is easy to fix. See my edit above. Oct 2, 2016 at 17:11
1

Too many actual questions in one question. Ask separate questions next time. The problem you described should be solved by videoplayer option "prevent suspending" or "disable sscreensaver". VLC has this option somewhere in settings. However, people report that it sometimes does not work.
To show dialogs from bash script, use xdialog utility. However, it will not simply work if run from outside user session, like from crontab. You will need to disable X server security first.
To run poweroff as a standart user, run it through systemd
systemctl poweroff.

4
  • Thanks. I asked a single question everything else, including the VLC part, is background info and what I've tried to do.
    – notorious
    Oct 2, 2016 at 13:22
  • 1
    zenity is a better alternative than xdialog for such a thing (doesn't require you to give away the little security X provides). But I completely second this answer.
    – JohnW
    Oct 2, 2016 at 13:22
  • @notorious: in that case, you are basically telling us that you disabled everything remotely related to power management first then to tell us that you eventually want power management: this is the way you solved a first problem (unwanted shut-down while playing a movie), which is fine; but this may not be the only way to achieve the same result (minus the problems) which is what Barafu was saying.
    – JohnW
    Oct 2, 2016 at 13:23
  • Ah OK I see what you're saying. The VLC part is actually irrelevant because now that I think about it we've been using web based clients lately. I'll go ahead and remove it. Sorry for that. I'm checking up on zenity now
    – notorious
    Oct 2, 2016 at 13:35

You must log in to answer this question.

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