代码之家  ›  专栏  ›  技术社区  ›  MrWater

如何在Linux中在给定时间关闭Firefox?

  •  -1
  • MrWater  · 技术社区  · 7 年前

    现在的挑战是在一个特定的时间关闭火狐,保护一个人免受“网瘾”的伤害,并确保在晚上休息。

    在这种情况下,我的搭档要求在22点关闭firefox,因为她晚上熬夜,然后第二天累了。 当她在22点后碰巧想打开火狐时,在她打开火狐15分钟后自动关闭它。

    编辑

    我写这个问题的时候已经有了答案,在那里我创建了一个shell脚本,然后与linux文件集成。

    代码中有一个错误,如果时间在22:00之前,它仍然会增加15分钟。

    now=$(date +'%R')
      KILL_DATE=$(date -d "22:00 today" +'%R')
      if [ "$now" > "$KILL_DATE" ]; then
         KILL_DATE=$(date -d "$now today + 15 minutes" +'%R')
      fi
      exec echo "pkill -f firefox" | at $KILL_DATE
      exec $MOZ_PROGRAM "$@" 
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   MrWater    6 年前

    我找到的解决办法是:

    1. 在上复制Firefox文件 /usr/bin/ 到火狐白天
    2. 更新加载firefox的行,并使用“at”命令在指定时间执行后台终止任务。

    之所以选择这个,是因为她通常从“开始菜单”上的快捷键打开firefox。 这只是用opensuse测试的。

    具体来说,实现这一目标的准则是:

      now=$(date +'%R')
      kill_date=$(date -d "22:00 today" +'%R')
      if [ $now -gt $kill_date ]; then
         kill_date=$(date -d "$now today + 15 minutes" +'%R')
      fi
      exec echo "pkill -f firefox" | at $kill_date
      exec $MOZ_PROGRAM "$@" 
    

    可以在新文件的最后几行找到。

    /usr/bin/firefox_白天

    #!/bin/sh
    #
    # ***** BEGIN LICENSE BLOCK *****
    # Version: MPL 1.1/GPL 2.0/LGPL 2.1
    #
    # The contents of this file are subject to the Mozilla Public License Version
    # 1.1 (the "License"); you may not use this file except in compliance with
    # the License. You may obtain a copy of the License at
    # http://www.mozilla.org/MPL/
    #
    # Software distributed under the License is distributed on an "AS IS" basis,
    # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    # for the specific language governing rights and limitations under the
    # License.
    #
    # The Original Code is mozilla.org Code.
    #
    # The Initial Developer of the Original Code is
    # Netscape Communications Corporation.
    # Portions created by the Initial Developer are Copyright (C) 1998
    # the Initial Developer. All Rights Reserved.
    #
    # Contributor(s):
    #    Wolfgang Rosenauer <wolfgang.rosenauer@suse.de>
    #                       <wr@rosenauer.org>
    #
    # Alternatively, the contents of this file may be used under the terms of
    # either the GNU General Public License Version 2 or later (the "GPL"), or
    # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    # in which case the provisions of the GPL or the LGPL are applicable instead
    # of those above. If you wish to allow use of your version of this file only
    # under the terms of either the GPL or the LGPL, and not to allow others to
    # use your version of this file under the terms of the MPL, indicate your
    # decision by deleting the provisions above and replace them with the notice
    # and other provisions required by the GPL or the LGPL. If you do not delete
    # the provisions above, a recipient may use your version of this file under
    # the terms of any one of the MPL, the GPL or the LGPL.
    #
    # ***** END LICENSE BLOCK *****
    
    ##
    ## Usage:
    ##
    ## $ mozilla [args]
    ##
    ## This script is meant to run a mozilla program from the mozilla
    ## rpm installation.
    ##
    ## The script will setup all the environment voodoo needed to make
    ## mozilla work.
    
    cmdname=`basename $0`
    
    ##
    ## Variables
    ##
    MOZ_DIST_BIN="/usr"
    MOZ_DIST_LIB="/usr/lib64/firefox"
    MOZ_APPNAME="firefox"
    MOZ_PROGRAM="$MOZ_DIST_LIB/$MOZ_APPNAME"
    
    MOZ_APP_LAUNCHER="$MOZ_DIST_LIB/$MOZ_APPNAME.sh"
    if [ "$0" = "$MOZ_APP_LAUNCHER" ]; then
      [ -h "/usr/bin/$MOZ_APPNAME" ] && \
       _link=$(readlink -f "/usr/bin/$MOZ_APPNAME")
      if [ "$_link" = "$MOZ_APP_LAUNCHER" ]; then
        export MOZ_APP_LAUNCHER="/usr/bin/$MOZ_APPNAME"
      fi
    else
      export MOZ_APP_LAUNCHER="/usr/bin/$MOZ_APPNAME"
    fi
    
    mozilla_lib=`file $MOZ_PROGRAM`
    LIB=lib
    echo $mozilla_lib | grep -q -E 'ELF.64-bit.*(x86-64|S/390|PowerPC)' && LIB=lib64
    
    BROWSER_PLUGIN_DIR=/usr/$LIB/browser-plugins
    if [ ! -d $BROWSER_PLUGIN_DIR ]; then
      BROWSER_PLUGIN_DIR=/opt/netscape/plugins
    fi
    
    MOZILLA_FIVE_HOME="$MOZ_DIST_LIB"
    export MOZILLA_FIVE_HOME
    LD_LIBRARY_PATH=$MOZ_DIST_LIB${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
    export LD_LIBRARY_PATH
    
    # needed for SUN Java under Xorg >= 7.2
    export LIBXCB_ALLOW_SLOPPY_LOCK=1
    
    ##
    if [ -z "$MOZ_PLUGIN_PATH" ]; then
      export MOZ_PLUGIN_PATH=$BROWSER_PLUGIN_DIR
    else
      # make sure that BROWSER_PLUGIN_DIR is in MOZ_PLUGIN_PATH
      echo "$MOZ_PLUGIN_PATH" | grep "$BROWSER_PLUGIN_DIR" 2>&1 >/dev/null
      _retval=$?
      if [ ${_retval} -ne 0 ]; then
        export MOZ_PLUGIN_PATH=$MOZ_PLUGIN_PATH:$BROWSER_PLUGIN_DIR
      fi
    fi
    
    # disable Gnome crash dialog (doesn't make sense anyway)
    export GNOME_DISABLE_CRASH_DIALOG=1
    
    moz_debug=0
    script_args=""
    pass_arg_count=0
    while [ $# -gt $pass_arg_count ]
    do
      case "$1" in
        -d | --debugger)
          moz_debugger=$2;
          if [ "${moz_debugger}" != "" ]; then
            shift 2
            moz_debug=1
          else
            echo "-d requires an argument"
            exit 1
          fi
          ;;
        *)
          # Move the unrecognized argument to the end of the list.
          arg="$1"
          shift
          set -- "$@" "$arg"
          pass_arg_count=`expr $pass_arg_count + 1`
          ;;
      esac
    done
    
    if [ $moz_debug -eq 1 ]; then
      tmpfile=`mktemp /tmp/mozargs.XXXXXX` || { echo "Cannot create temporary file" >&2; exit 1; }
      trap " [ -f \"$tmpfile\" ] && /bin/rm -f -- \"$tmpfile\"" 0 1 2 3 13 15
      echo "set args ${1+"$@"}" > $tmpfile
      echo "run" >> $tmpfile
      echo "$moz_debugger $MOZ_PROGRAM -x $tmpfile"
      exec $moz_debugger "$MOZ_PROGRAM" -x $tmpfile
    else
      now=$(date +'%R')
      kill_date=$(date -d "22:00 today" +'%R')
      if [ $now -gt $kill_date ]; then
         kill_date=$(date -d "$now today + 15 minutes" +'%R')
      fi
      exec echo "pkill -f firefox" | at $kill_date
      exec $MOZ_PROGRAM "$@"
    fi