简体   繁体   English

Linux crontab:使终端出现警告

[英]Linux crontab: make terminal appear with warning

I have a crontab set to shutdown my computer at a certain time every night. 我将crontab设置为每晚某个时间关闭计算机。 It also displays a warning and allows the process to be cancelled. 它还显示警告,并允许取消该过程。

My issue is I can't get it to show the terminal with the warning if the terminal is not already open. 我的问题是,如果终端尚未打开,则无法显示警告终端。 I want the terminal to appear and display the warning and leave the possibility to cancel it. 我希望终端出现并显示警告,并保留取消它的可能性。

I'm including my current cron command. 我包括当前的cron命令。 What am I doing wrong? 我究竟做错了什么? Thanks. 谢谢。

0 3 * * * /sbin/shutdown -r 5 "Shutting down in five minutes..."

This is how wall / mesg , the broadcasting mechanism used by shutdown , works - it's tightly tied to terminals and doesn't know about graphic environment. 这是wall / mesgshutdown使用的广播机制)的工作方式-它与终端紧密相连,并且不了解图形环境。

  1. One alternative would be to create a script that spawns a popup if there are available messages. 一种替代方法是创建一个脚本,如果有可用消息,该脚本将生成一个弹出窗口。 This requires you to allocate TTY somewhere (check the details at the end of the answer.) 这要求您将TTY分配到某个位置(请在答案末尾检查详细信息。)

  2. Another, IMHO better and simpler, alternative would be to ditch shutdown messaging system altogether and spawn a terminal that does something like echo Shutting down in 5 minutes...; sleep 300; shutdown 另一个更好,更简单的恕我直言,替代方法是完全放弃shutdown消息传递系统,并生成一个终端,执行类似echo Shutting down in 5 minutes...; sleep 300; shutdown echo Shutting down in 5 minutes...; sleep 300; shutdown echo Shutting down in 5 minutes...; sleep 300; shutdown . echo Shutting down in 5 minutes...; sleep 300; shutdown The upside of this approach is that you get to cancel it with just Ctrl+C . 这种方法的好处是,您只需按Ctrl + C即可取消它。 The downside is that, depending on how you spawn the terminal, it requires X to be running, and will spawn a possibly annoying popup. 不利的一面是,取决于生成终端的方式,它需要X才能运行,并且会生成可能令人讨厌的弹出窗口。

That being said, I believe this question is better suited for SuperUser or Unix & Linux SE . 话虽如此,我相信这个问题更适合于SuperUserUnix&Linux SE


To be specific wall , used by shutdown just loops through all writable TTYs in the system (such as /dev/pts/1 ) and writes a string to them. 具体而言wall ,通过使用shutdown只是通过系统中的所有可写TTY中循环(如/dev/pts/1 )和一字符串写入它们。 For example: 例如:

  1. on terminal A, sudo tail -f /dev/tty50 在终端A上, sudo tail -f /dev/tty50
  2. on terminal B, sudo wall test 在终端B上, sudo wall test

will show the message test three times: one time in terminal B, one in terminal A and one in tail inside terminal A, that reads from TTY50 (in this example, we need sudo to broadcast the message to TTY we don't own). 将显示消息test三遍:一次在终端B中,一次在终端A中,另一次在终端A内部的tail ,从TTY50读取(在此示例中,我们需要sudo将消息广播到我们不拥有的TTY中) 。

Since there is no console opened on your desktop (= no TTY), there's no one to see the message from wall . 由于您的桌面上没有打开任何控制台(=没有TTY),因此没有人可以看到wall消息。 So in order to read is as per suggestion 1, you'd need to keep your own TTY somewhere. 因此,按照建议1进行阅读,您需要将自己的TTY保留在某处。 I believe the article Allocating Pseudo-Terminals explains how to do it in low-level way. 我相信“ 分配伪终端”一文解释了如何以低级方式进行操作。 Another way to implement it would be to monitor an already allocated TTY such as TTY50 with superuser privileges, although I don't recommend this. 实现它的另一种方法是使用超级用户权限监视已经分配的TTY,例如TTY50,尽管我不建议这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM