简体   繁体   中英

Linux crontab: make terminal appear with warning

I have a crontab set to shutdown my computer at a certain time every night. 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. 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.

  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.)

  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 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 . The downside is that, depending on how you spawn the terminal, it requires X to be running, and will spawn a possibly annoying popup.

That being said, I believe this question is better suited for SuperUser or Unix & 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. For example:

  1. on terminal A, sudo tail -f /dev/tty50
  2. on terminal 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).

Since there is no console opened on your desktop (= no TTY), there's no one to see the message from wall . So in order to read is as per suggestion 1, you'd need to keep your own TTY somewhere. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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