ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
can protect my eye for every hour to shutdown my pc when to set in crontab,but sometimes i have an important work to do ,i have to work continuously.
How to solve the problem then?
Unclear, you claim that you wish to force a shutdown once per hour to alleviate eyestrain. Therefore why allow an option to cancel it? However I do understand that people ultimately will wish to have that option.
Run the cron job to shutdown once per hour, but how about sleep for 3300 and then use wall(1) to send a message to all terminals stating that the system will be shut down in 5 minutes and alert them to cancel, or restart the cron job if they wish to delay the process. Then sleep another 300 and finally shutdown if the cron wasn't restarted.
Wouldn't a "typing break" applet accomplish the same thing? Many Linux distros include one by default, or you can do a web search for "linux typing break" (without the quotes) for some alternatives.
another approach is, instead of it reading from terminal (which is not available in the cron), let it check for the existence of a certain file in, say, /tmp.
It's not safe at all; but then again... there are flaws already pointed out.
Code:
sleep 3600
sync
if [[ ! -e /tmp/noshutdown ]]
then
wall "Shutting down in a few seconds, please save your work."
shutdown -h 10
else
wall "Not shutting down, the file /tmp/noshutdown exists."
fi
Myself, I would find such a script highly annoying... but that's me I suppose.
You could also, instead of shutting down the system, force the screen to turn off / on, when using X:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.