AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 640845
Accepted
Madhav Nikam
Madhav Nikam
Asked: 2015-06-26 05:28:12 +0800 CST2015-06-26 05:28:12 +0800 CST 2015-06-26 05:28:12 +0800 CST

我可以使用什么图形实用程序来实现 Ubuntu 自动关机?

  • 772

在 Microsoft Windows 中,我使用switch off软件进行自动关机、休眠、重启等。我能在 Ubuntu 中找到类似的东西吗?

关掉

我阅读了以下链接。如何在特定时间后自动关闭系统?

shutdown
  • 3 3 个回答
  • 980 Views

3 个回答

  • Voted
  1. Best Answer
    Maythux
    2015-06-26T05:32:22+08:002015-06-26T05:32:22+08:00

    GShutdown是一个实用程序,允许您安排关闭或重新启动计算机。有了它,您可以在特定时间或倒计时后简单快速地选择关闭时间。

    安装 Gshutdown:

    sudo apt-get install gshutdown
    

    在此处输入图像描述

    资源。

    • 5
  2. VRR
    2015-12-18T07:21:27+08:002015-12-18T07:21:27+08:00

    由于某些未知原因,这类应用程序不会持续很长时间,并且在更新的 Ubuntu 迭代中它们会被破坏。对于 Ubuntu 15.10, Gshutdown不是开箱即用的,而且为 kwin 应用程序下载数百 MB 的依赖项不是很方便。至少不适合这种应用程序。

    这是我写的一个小脚本来解决这个问题,可以说这是一个更好的选择。为什么 :

    • 它是一个图形脚本,因此可以作为经典应用程序运行
    • 因为任何其他脚本功能都可以根据用户的喜好进行调整
    • 该代码可以在各种发行版中使用,并且不太可能很快改变
    • 脚本中描述了包含的各种选项。

    关闭对话框在行动: 在此处输入图像描述

    如何使用它

    只需要安装一个包来解决用户不活动功能,但是脚本将在没有它的情况下运行其他可用选项。要在终端中安装“xprintidle”类型:

    sudo apt-get install xprintidle
    

    打开文本编辑器,粘贴下面提供的代码并将文件保存Shutdown_dialog在您的主文件夹中。右键单击文件并选择allow executing file as program或通过终端:

    chmod +x ~/Shutdown_dialog
    

    编码:

    #!/bin/bash
    
    ###########<<INFO
    # This script is intended for use in various Linux distros. In order to use all available options install 'xprintidle' package. It is responsible for 'system idle' option,
    # but the script will run without it as well. To install package on Debian-based distros execute: `sudo apt-get install xprintidle` .
    # Other packages used in script: zenity, notifyOSD, xdotool, wmctrl, play.
    #
    # Below INFO section there are some predefined variables. Their values can easily be customized by user.
    #
    #   Some options available:
    #   
    # -Select an action - the only option that must be selected, others can omit. The hibernation action is not visible by default, but it can be enabled. Use this at your own risk,
    # it has not been tested. To enable hibernation uncomment two lines in `case var1` statement and add |Hibernate option in zenity dialogs:
    #  --list-values='Shutdown|Restart|Hibernate|Suspend|Logout|Lock screen'. Alternative actions are commented and use them in case your distro doesn't support default ones.
    #
    # -Time from now; on user inactivity; at specific time and date - only one of these can be used at the same time. The accuracy for latter two is +-1 min.
    #
    # -Run custom command before action executes - BE CAREFULL !!! with this one. After confirmation dialog timeout, there is no turning back.
    #
    # -When selected application exit - clicking on this option new dialog will be presented with available options. Select action and preferred options (if any) and press OK. 
    # Then select the application by clicking on it.
    #
    # -Default - first time this option is activated it will open new dialog with available options. Select preferred options. They will be saved in `default_conf_file` 
    # which is predefined as    `default_conf_file=~/.cache/Shutdown_dialog_conf_file.txt`  The script will then execute ! 
    # But on next run script will execute just by pressing `default` button.
    #############INFO
    
    #####Predefined variables intended for user customization
    default_conf_file=~/.cache/Shutdown_dialog_conf_file.txt
    default_sound_1=/usr/share/sounds/freedesktop/stereo/service-login.oga
    default_sound_2=/usr/share/sounds/freedesktop/stereo/dialog-warning.oga
    default_sound_3=/usr/share/sounds/freedesktop/stereo/message.oga
    defaul_time_to_show_confirmation_dialog=10
    
    
    #####Functions that correspond to selected options
    default_option () {
        if [ -f "$default_conf_file" ];then
            options=$(cat "$default_conf_file")
        else
    options=$(zenity --forms --title="Reappeared shutdown dialog" --text="\nYou are running DEFAULT option for the first time. Choose settings\n and press OK. The default options will be saved for further use.\n\n *Only one of the marked options can be used at the same time\n" --add-list="Select an action:" --list-values='Shutdown|Restart|Suspend|Logout|Lock screen' --add-entry="Time from now (min)*:" --add-entry="On user inactivity (min)*:" --add-entry="At specific time (hh:mm) and date:*" --add-combo="This has no purpose. It only replaces calendar" --combo-values= --forms-date-format=%Y-%m-%d --add-entry="Run command before action executes:" --add-entry="Show desktop notifications every (min):" --add-entry="Seconds to display confirmation dialog:" --add-combo="Play sound on confirmation dialog:" --combo-values="|default_sound_1|default_sound_2|default_sound_3")
            [ $? == 1 ] && exit 1
            echo "$options" > "$default_conf_file"
            rm_default_conf_file="$default_conf_file"
        fi  
    }
    
    
    
    select_app () {
    options=$(zenity --forms --title="Reappeared shutdown dialog" --text="\n You are running WHEN SELECTED APPLICATION EXIT option! \n\n Choose additional settings, press OK and then click on application\n to select. Use Alt+Tab to cycle through open applications.\n\n" --add-list="Select an action:" --list-values='Shutdown|Restart|Suspend|Logout|Lock screen' --add-combo="Unavailable option" --combo-values= --add-combo="Unavailable option" --combo-values= --add-combo="Unavailable option" --combo-values= --add-combo="Unavailable option" --combo-values= --add-entry="Run command before action executes:" --add-combo="Unavailable option" --combo-values= --add-entry="Seconds to display confirmation dialog:" --add-combo="Play sound on confirmation dialog:" --combo-values="|default_sound_1|default_sound_2|default_sound_3")
        [ $? == 1 ] && exit 1
        win_id=$(xdotool selectwindow)
        notify-send "Window selected:  $(xdotool getwindowname $win_id)"
        win_id=$(printf 0x0%x "$win_id")
        while :
        do
            [[ -z $(wmctrl -l | grep "$win_id") ]] && break || sleep 5
        done
    }
    
    
    
    user_inactivity () {
        which xprintidle
        if [ $? == 1 ];then
            zenity --warning --timeout=7 --ellipsize --text="\tPackage 'xprintidle' is not installed.\n\n Option ** \"ON USER INACTIVITY\" ** cannot be used!\n\n\t\t Exiting...."
            [ -f "$rm_default_conf_file" ] && rm "$default_conf_file"
            exit 1
        fi
        while [[ "$idle_time" -lt "$var3" ]]
        do
            idle_time=$(( $(xprintidle) / 60000 ))
            sleep 5
        done
    }
    
    
    
    notification () {
        x=1
        [ -z "$time" ] && return 1
        notify-send "System will $var1 in $time minutes!"
        while :
        do
            remaining=$(( $time - $x * $var7 ))
            sleep "$var7"m
            notify-send "System will $var1 in $remaining minutes!"
            x=$(( x + 1 ))
            [ "$remaining" -lt "$var7" ] && break
        done
        sleep "$remaining"m
    }
    
    
    #####Confirmation dialog shown before action is executed
    confirmation_dialog () {
        [ -n "$sound" ] && play "$sound" &
        $(sleep 0.9;wmctrl -r "Shutdown confirmation dialog" -b add,above) &        #comment this line if you don't want dialog to be above other windows
        zenity --question --title="Shutdown confirmation dialog" --text="System will $var1 in $var8 seconds! \n\nDo you wish to execute?" --ellipsize --timeout="$var8" --cancel-label=Cancel
        [ $? == 1 ] && exit 1
    
    }
    
    
    ###### Initial Shudown dialog window #######
    options=$(zenity --forms --title="Shutdown dialog" --text="\nAvailable options are documented in the header of the script\n\n *Only one of the marked options can be used at the same time\n" --add-list="Select an action:" --list-values='Shutdown|Restart|Suspend|Logout|Lock screen' --add-entry="Time from now (min)*:" --add-entry="On user inactivity (min)*:" --add-entry="At specific time (hh:mm) and date:*" --add-calendar="Select date for previous option" --forms-date-format=%Y-%m-%d --add-entry="Run command before action executes:" --add-entry="Show desktop notifications every (min):" --add-entry="Seconds to display confirmation dialog (s):" --add-combo="Play sound on confirmation dialog:" --combo-values="|default_sound_1|default_sound_2|default_sound_3" --extra-button="When selected application exit" --extra-button="Default")
    
    #####Validate selection and call functions
    if [ $? != 0 ];then
        if [ "$options" == "When selected application exit" ];then select_app
        elif [ "$options" == "Default" ];then default_option
        else exit 1
        fi
    fi
    
    #####Variables assignment
    old="$IFS"
    IFS="|"
    read -r var{1..9} <<< "$(echo "$options")"
    IFS="$old"
    
    #####Validate and define time variables
    if [ "$var2" != 0 ] && [ -z "$var3" ] && [ -z "$var4" ]; then 
        time="$var2"
    elif [ "$vr3" != 0 ] && [ -z "$var2" ] && [ -z "$var4" ]; then
        if [ ! -z "$var7" ];then
            zenity --warning --timeout=7 --ellipsize --text="\nThe *** \"ON USER INACTIVITY\" *** option cannot be used with desktop notifications!\n\n\t\t Exiting...."
            [ -f "$rm_default_conf_file" ] && rm "$default_conf_file"
            exit 1
        fi
        user_inactivity
    elif [ "$vr4" != 0 ] && [ -z "$var3" ] && [ -z "$var2" ]; then
        current=$(date +%s)
        target=$(date +%s -d "$var5 $var4")
        time=$(( ($target - $current) / 60 ))
    elif [ "$var2" == " " ] && [ "$var3" == " " ] && [ "$var4" == " " ]; then :
    else
        zenity --warning --timeout=5 --ellipsize --text="\t*Only one of the marked options can be used at the same time!\n\n\t\t Exiting...."
        [ -f "$rm_default_conf_file" ] && rm "$default_conf_file"
        exit 1  
    fi
    
    #####Validate selection and define action (replace it with commented one if it doesn't work)
    case $var1 in
        Shutdown,)
            action="poweroff";;                     #"systemctl poweroff"
        Restart,)
            action="reboot";;                       #"systemctl reboot"
    #   Hibernate,)
    #       action="systemctl hibernate";;
        Suspend,)
            action="systemctl suspend";;
        Logout,)
            action="gnome-session-quit --logout --no-prompt";;      #"pkill -u $USER"
        Lock?screen,)
            action="gnome-screensaver-command -l";;             #"xdotool key Ctrl+Alt+l"
    esac
    
    #####Validate other settings
    [ -n "$var7" ] && notification
    
    [ -z "$var8" ] && var8="$defaul_time_to_show_confirmation_dialog"
    
    [ "$var9" != 0 ] && case $var9 in
        default_sound_1)
            sound="$default_sound_1";;
        default_sound_2)
            sound="$default_sound_2";;  
        default_sound_3)
            sound="$default_sound_3";;
    esac    
    
    #####Execute
    [[ -n "$time" && -z "$var7" ]] && sleep "$time"m
    confirmation_dialog
    eval "$var6"
    eval "$action"
    
    exit 0
    

    或者,您可以创建桌面图标。打开文本编辑器,粘贴下面提供的代码并将文件保存Shutdown_dialog.desktop在桌面文件夹中。右键单击文件并选择allow executing file as program或通过终端:

    chmod +x ~/Desktop/Shutdown_dialog.desktop
    

    编码:

    #!/usr/bin/env xdg-open
    
    [Desktop Entry]
    Name=Shutdown dialog
    Comment=Shutdown dialog script
    Terminal=false
    Icon=/usr/share/icons/oxygen/64x64/actions/system-shutdown.png
    Exec="/home/username/Shutdown_dialog"
    Type=Application
    Categories=Application;Utility;
    Name[en_US]=Shutdown dialog
    

    只需确保该Exec行具有脚本的绝对路径。

    如果有任何不清楚的地方,请发表评论,我会在必要时进行编辑。

    • 0
  3. Stan Gabenov
    2016-01-08T06:56:21+08:002016-01-08T06:56:21+08:00

    我认为 GShutdown 是这些实用程序中最好的。但是要让它与 Ubuntu 15.10 一起工作,你需要做一些技巧。

    1. 安装并打开 gshutdown
    2. 编辑 -> 首选项
    3. 选择“自定义命令”
    4. 单击“关闭计算机”并粘贴此 sh -c "dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 "org.freedesktop.login1.Manager.PowerOff “布尔值:真”
    • 0

相关问题

  • 如何在 KVM 域上使用“virsh shutdown”关闭 Ubuntu 来宾?

  • 如何从命令行安全地关闭/重新启动/注销 KDE?

  • 如何使关机不需要管理员密码?

  • 关机前执行脚本

  • 按下电源按钮时如何关闭计算机?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve