定制电源按钮的操作(Ubuntu 10.04)

我想自定义电源按钮的操作。http://blog.metalight.dk/2010/07/ubuntu-lucid-custom-power-button-event/上的教程有时有效,但并非总是如此(关机过程的次数有时会不同)。
我需要以下解决方案:
  • 按下按钮1次执行通常的操作系统关机
  • 按下按钮3次执行操作系统重启
您会如何编写代码? 文件:/etc/acpi/powerbtn.sh
#!/bin/sh
# /etc/acpi/powerbtn.sh
# Initiates a shutdown when the power putton has been
# pressed.

# you need to double 'click' the power button to shutdown
( if ! [ $(pidof -x powerbtn.sh | wc -w) -eq 3 ]; then
    sleep .4
    exit
  else
    poweroff
  fi
) &

楼主,你还在寻找答案吗?如果是的话,你可能需要重新发布你的问题,因为这个帖子已经被标记为关闭。祝好! - Ringtail
教程的链接已失效。您应该指向以下链接:https://blog.metalight.dk/2010/07/ubuntu-1004-custom-power-button-event.html。 - LucaM
1个回答

进程的数量不应该有所不同。在计算pid时,你必须记住要包括脚本本身和创建的子进程。
我创建了以下代码,应该符合你的要求。根据你希望多个按钮按下的速度,你应该调整超时时间。
#!/bin/sh
# /etc/acpi/powerbtn.sh
# Initiates a shutdown when the power putton has been
# pressed.

timeout=0.8
pid_count=$(pidof -x powerbtn.sh | wc -w)

( if [ $pid_count -eq 4 ]; then
        sleep $timeout
        /etc/acpi/sleep.sh
    else
        sleep $timeout
        pid_count_now=$(pidof -x powerbtn.sh | wc -w)
        if [ $pid_count_now -eq 2 ] && [ $pid_count -eq 2 ]; then
            poweroff
        fi
        exit
    fi
) &