Home  >  Q&A  >  body text

linux - Ubuntu自启动脚本实现定时关机的问题?

想象一下给年幼的孩子配了一台电脑,出于对孩子身体健康的着想,打算给孩子装上Ubuntu系统,并让系统实现以下功能:孩子玩电脑超过50分钟时,电脑自动关机;只有当孩子休息20分钟后才能成功开机。

问题:将以下代码写入shutdown_on_time.sh,再新建一个olddate.log(在该文件里写入数字1)文件,shutdown_on_time.sh与olddate.log所处的文件夹位置为/home/lic/shell_test/
在Ubuntu系统的文件/etc/rc.local(系统启动后,会自动执行rc.local里的程序)里加入
bash /home/lic/shell_test/shutdown_on_time.sh
电脑开机后,并没有实现该功能,请问有什么解决方案吗?

注:shutdown_on_time.sh的权限设置:
chmod 777 shutdown_on_time.sh

#!/bin/bash

#shutdown after 50 munite when open the system
standardskip=20 #规定休息时间必须超过20分钟

datenow=$(date +%Y%m%d%H%M) #读取当前时间

dateold=$(awk 'NR==1{print $1}' /home/lic/shell_test/olddate.log) #读取上一次记录的时间

skip=$(expr $datenow - $dateold)

if [ $skip -gt $standardskip ]
then
sleep 100 
notify-send "主人,你已经休息了20分钟了,可以使用电脑了"
sleep 3
notify-send "主人,你已经休息了20分钟了,可以使用电脑了"
sleep 3
notify-send "主人,你已经休息了20分钟了,可以使用电脑了"
sleep 3000
echo $datenow > /home/lic/shell_test/olddate.log
notify-send ["计算机将会在2分钟后关机"] "请及时保存已修改的文件"
sleep 3
notify-send ["计算机将会在3分钟后关机"] "请及时保存已修改的文件"
sleep 3
notify-send ["计算机将会在3分钟后关机"] "请及时保存已修改的文件"
sleep 120
shutdown now
else
sleep 100 
notify-send "主人,还不到20分钟,你应该再休息会"
notify-send ["计算机将会在3分钟后关机"] "请及时保存已修改的文件"
sleep 3
notify-send ["计算机将会在3分钟后关机"] "请及时保存已修改的文件"
sleep 3
notify-send ["计算机将会在3分钟后关机"] "请及时保存已修改的文件"
sleep 300
shutdown now
fi

exit 0
大家讲道理大家讲道理2715 days ago753

reply all(1)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 17:07:43

    "This function is not implemented" What exactly is not implemented? Has the notification been implemented? Or does it just not shut down automatically?
    Already installed Ubuntu, are you afraid that your children will play web games and steam?

    The shutdown command requires sudo permissions. sudo visudoAdd shutdown to password-free, and add sudo in front of it when calling.

    In addition, my general approach to self-starting programs is
    1 Use Ubuntu's "Startup Applications" management, this is simple.
    2 Write systemd or sys v service script

    reply
    0
  • Cancelreply