我正在尝试为自己制作一个生产力套件。我的第一个目标是从 0900 到 1600 阻止 Facebook、Gmail 和 Stackexchange。
到目前为止,我已经编辑了我的/etc/hosts并添加了 0.0.0.0 www.facebook.com 和类似的 gmail 和 stackexchange。
但是我对如何在我的脚本中包含阻塞持续时间感到有些困惑。
我认为是有 2 个不同的文件(hosts_allow、hosts_block),然后cp hosts_allow hosts或cp hosts_block hosts取决于时间,但这需要放入无限循环或我不确定是解决问题的最佳方法。
有什么线索吗?
War*_*ung 22
使用cron.
crontab -e 以 root 身份说——或者sudo crontab -e如果你已经sudo设置——并将以下内容放入文本编辑器中出现的文件中:
0 9 * * * cp /etc/hosts_worktime /etc/hosts
0 16 * * * cp /etc/hosts_playtime /etc/hosts
Run Code Online (Sandbox Code Playgroud)
这表示在一个月的每一天的第 9 和第 16 小时的第 0 分钟,/etc/hosts使用给定的 shell 命令覆盖。
您实际上可能想要更复杂的东西:
0 9 * * 1-5 cp /etc/hosts_worktime /etc/hosts
0 16 * * 1-5 cp /etc/hosts_playtime /etc/hosts
Run Code Online (Sandbox Code Playgroud)
一个变化——排1-5在第五位——表示工作和娱乐时间之间的变化只发生在周一到周五。
说man 5 crontab要获得有关您可以在crontab文件中执行的所有操作的完整说明。
顺便说一句,我更改了hosts上面文件的名称,因为hosts_allow太接近hosts.allow,TCP Wrappers 使用。