다시 시작/종료하기 전에 경고를 받으려면 어떻게 해야 합니까?

다시 시작/종료하기 전에 경고를 받으려면 어떻게 해야 합니까?

방금 Ubuntu를 다시 시작했지만 가상 머신이 백그라운드에 열려 있다는 사실을 잊어버렸습니다(작업 표시줄에 최소화된 아이콘이 있음). 다음과 같은 방식으로 Ubuntu를 구성하는 것이 가능합니까?사용자 정의애플리케이션이 실행 중이면 다시 시작/종료하기 전에 경고를 표시하시겠습니까? 16.04 버전을 사용하고 있습니다.

답변1

소개

다음 스크립트는 모든 또는 사용자 정의 응용 프로그램의 존재 여부를 모니터링하고 해당 존재가 발견되면 그래픽 대화 상자를 통해 시스템이 종료되는 것을 방지합니다(명령줄을 통한 종료는 시스템에 의해 적용되는 작업이므로 영향을 받지 않습니다). 자신이 무엇을 하고 있는지 알고 있는 관리자).

3가지 옵션이 있습니다:

-a열려 있는 모든 응용 프로그램을 모니터링합니다.

-c그래픽으로 앱 선택

-s명령줄에서 앱에 대한 .desktop 파일 지정

-h구문과 옵션 목록을 인쇄합니다.

-c옵션은 창을 클릭하고 모니터링하려는 단일 세션에만 적합합니다. 및 옵션 -a-s시스템 로그인 시 실행될 자동 시작 항목으로 추가하는 것이 적합합니다. -s옵션은 전체 경로 또는 일부와 함께 사용할 수 있습니다. 예를 들어 /usr/share/applications/firefox.desktop또는 둘 중 하나 firefox.desktop를 동일하게 사용할 수 있습니다.

스크립트 소스

스크립트 소스는 여기 또는 내에서 사용할 수 있습니다.GitHub. 사용자는 전체 저장소를 복제하거나 다음을 사용하여 스크립트를 얻을 수 있습니다.

wget https://raw.githubusercontent.com/SergKolo/sergrep/master/safe_shutdown.sh && chmod +x safe_shutdown.sh

스크립트 자체만 가져오는 명령입니다.

#!/usr/bin/env bash
#
###########################################################
# Author: Serg Kolo , contact: [email protected] 
# Date: May 14th , 2016 
# Purpose: Ensure that user closes all or specific
#          running windows and exits without any work
#          lost
# Written for: http://askubuntu.com/q/771227/295286
# Tested on: Ubuntu 14.04 LTS
###########################################################
# Copyright: Serg Kolo , 2016
#    
#     Permission to use, copy, modify, and distribute this software is hereby granted
#     without fee, provided that  the copyright notice above and this permission statement
#     appear in all copies.
#
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
#     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#     DEALINGS IN THE SOFTWARE.

ARGV0="$0"
ARGC=$#

_notify_user()
{
 # Close the shutdown dialog and display
 # graphical popup which will ask user's shutdown 
 # confirmation. If user clicks OK , we shutdown.
 # If cancel - no action.
 qdbus com.canonical.Unity \
       /com/canonical/Unity/Session \
       com.canonical.Unity.Session.CancelAction

 if zenity --question --title='WARNING!' \
      --text="You have running apps. Shutdown anyway ?" \
      2> /dev/null
 then
      qdbus com.canonical.Unity  \
           /com/canonical/Unity/Session \
           com.canonical.Unity.Session.Shutdown
 fi
}

_get_running_apps()
{
  # Gets list of .desktop files for each
  # running app
  qdbus org.ayatana.bamf \
       /org/ayatana/bamf/matcher \
       org.ayatana.bamf.matcher.RunningApplicationsDesktopFiles 

}

_check_any_running()
{
   # Among the running apps there's always one
   # .desktop file, which is compiz.desktop. 
   # We want to know if there's anything besides that
   if [ $( _get_running_apps | wc -l ) -gt 1  ]; 
   then
         _notify_user
   fi
}

_check_specific_running()
{
  # Get list of running apps and see if
  # the .desktop file we got is on the list
  if _get_running_apps | grep -q "$1"
  then
       _notify_user
  fi
}

_select_app()
{
  # xwininfo provides nice interface which allows selecting
  #  a window. The rest is just simple parsing and passing 
  # around the XID of the app.
  notify-send 'Select a window you would like to monitor '
  XID=$(xwininfo -int | awk '/xwininfo: Window id/{print $4}')
  APP=$(qdbus org.ayatana.bamf \
       /org/ayatana/bamf/matcher \
       org.ayatana.bamf.matcher.ApplicationForXid  $XID )
  qdbus org.ayatana.bamf \
        "$APP" org.ayatana.bamf.application.DesktopFile
}


_print_usage()
{
 cat <<EOF
 safe_shutdown.sh [-a | -c |-s DESKTOP_FILE | -h  ]

 Options:
 -a Monitor any open applications.
 -c Graphically select an app
 -s specify .desktop file for app on command line
 -h print this text

  Copyright Serg Kolo , 2016
EOF
}

parse_args()
{
 if [ $ARGC -eq 0 ] ; then
   printf "%s: No option specified\n Usage:\n" ${ARGV0##*/} 
   _print_usage 
   exit 1
 fi

 local OPTIND opt
 while getopts "acs:" opt
 do
   case ${opt} in
      a) FUNCTION="_check_any_running"
        break
        ;;
      c)
        DESK_FILE=$(_select_app  )
        FUNCTION=" _check_specific_running $DESK_FILE   "
        break
        ;;

      s) DESK_FILE=${OPTARG}
         FUNCTION=" _check_specific_running $DESK_FILE   "
         break
        ;;
      h) _print_usage
        exit 0
        ;;
     \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    esac
  done
  shift $((OPTIND-1))

}


main()
{
 # Basic idea is to let user chose what to do
 # then monitor dbus for appropriate signal
 # Once the RebootRequested signal is received 
 # then perform appropriate checks ( for a specific
 # or all apps ). 
 local FUNCTION
 parse_args  "$@"
 dbus-monitor --profile \
      "interface='com.canonical.Unity.Session',type=signal" |
 while read -r line;
 do
  case "$line" in
       *RebootRequested*)  $FUNCTION ;;
  esac
 done
}

main "$@"

관련 정보