Linux shutdown after hour

Automatic shutdown at specified times?

I’m aware I can use (sudo?) shutdown -h hh:mm in a terminal window to shut down a system, however the user that is logged in is a kiosk user, which is locked down in such a way that terminal (or any window other than firefox, for that matter) cannot be opened. My question is:
Is there a way I can do this without having a terminal window opened? I’ve read about cron, but can’t quite work it out.
How can I change the shutoff time depending on what day it is?

No, sorry, that was just what was used in the example I read. Usually at 11:30, but I was wondering if there is a way to specify different times for different days?

@vembutech That question is for shutting down after a certain time period has passed — not shutting down at a certain time — however although it would be easy to adopt this to my needs, I still can’t run additional windows other than firefox. Thank you for your reply though! It seems much simpler than using cron, and I will most likely use it for other uses 🙂

3 Answers 3

Cron will work very well for this.

You need first to find the complete path to the shutdown command:

a@ubuntu:~$ which shutdown /usr/sbin/shutdown 

Knowing the path to the shutdown command, you can add the below line (with tweaks) to the end of /etc/crontab :

30 23 * * * root /usr/sbin/shutdown -h now 

At 23:30 (11:30 PM), the kiosk will shut down. No matter what user is logged in, the shutdown command runs as root.

(If you don’t want to use the global crontab, log in as root and use crontab -e . Use the same above syntax without the root ).

MM : Minute, 0-59
HH : 24-hour hour
DD : Day of month
OO : Month
WW : Day of Week (Sunday is 0, Monday is 1)
command : Self-explanatory

from crontab(5): The format of the system crontab, /etc/crontab includes a who column which does not exist in user crontabs. In the system crontab, cron runs the command as the user specified in this column. In a user crontab, all commands run as the user who created the crontab.

@MrAxlee -s doesn’t seem to be «proper syntax.» -h means to halt or shutdown the system (system’s choice — shutdown preferred)

Читайте также:  Битые сектора диск linux

A cronjob seems to be the best way because you can specify different times for different days. On Gnome based systems you can just install GNOME Shedule Tasks by using

sudo apt-get install gnome-schedule 

and then configure the cronjob using the GUI.

otherwise you would have to use sudo crontab -e and then add the following lines

30 11 * * 1-5 /sbin/shutdown -h now 30 10 * * 0,6 /sbin/shutdown -h now 

this would shutdown the PC at 11:30 from Monday to Friday and on 10:30 on Saturday and Sunday. The structure is very simple:

minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command 

For more information about this you could also just check out CronHowto

I had to specifically use /sbin/shutdown instead of shutdown because my root cron PATH was different than in the terminal I tried my script.

We can shutdown automatically at specified time by simply running command sudo poweroff in crontab .

If you want to shutdown the system at 6:30 pm everyday. Type in terminal:

@mrm There are alternative methods, there could be many different solutions to particular issue and the answers so. Isn’t it answer useful or not? Please I request you not to downvote to answer or question if you’re not clear about it. Read question properly and see if the answer apply to that specific question or not. If you still have any dilemma you are free to ask on Ask Ubuntu Meta or here and then make a decision.

In one crontab line, you’ve got two issues: poweroff skips shutdown scripts, which may be important to, say, safely shutdown your database or whatever, and you’ve got a sudo in a root crontab which is unneeded. You also didn’t address how to reference different days of the week as the question asked. So no, this isn’t an alternative method. As is, it’s two mistakes and an omission. The two other answers made a year prior to yours are correct and actually work.

@mrm Well, I’m using this script for last three years and I didn’t see any issue. And yes this alternative and also correct useful method. Que is Automatic shutdown at specified times. There is a command to shutdown and for auto specified time, we can use crontab as per our requirement. It would be better for you if you ask a different question regarding your issue.

Читайте также:  Linux show all opened ports

Источник

How to shut down Ubuntu after 2 hours of being idle?

Running: Xubuntu 14.04 32-bit. I have read several outdated, not working, working different that I want answers here and elsewhere. I had a script that resulted in shutting my Laptop down at boot time after I set it to autostart. I also tried it install complexshutdown despite that tool having died in 2011 and has a bug on Lauchpad that his exact option is not available. It not even installed on 14.04 here. I am looking for a simple straightforward script that is actually tested and working that I can put into the autostart that is doing nothing but checking for activity (mouse/keyboard) and shutting down my laptop if there was none for 2 hours. Thanks.

5 Answers 5

This is probably the best solution. No need for screensaver tweaking and running.

Install sudo apt-get install xprintidle

Put this script into autostart:

#!/bin/bash idletime=$((1000*60*60*2)) # 2 hours in milliseconds while true; do idle=`xprintidle` echo $idle if (( $idle > $idletime )); then #sudo shutdown -P now dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 "org.freedesktop.login1.Manager.PowerOff" boolean:true fi sleep 1 done 

The comments and @Jobin ‘s answer did lead me to investigate myself again and I actually found a unfinished script with xprinttime but without any loop in the script. Probably not finished this because my linux/bash knowlege was not good at the time. I also did put the script I had in rc.local or something like that triggered the shutdown on boot. Thanks to @Jobin for the reminder how to add startup apps in XFCE, I already knew this but . and credits for the dbus thing, never saw that, better then shutdown since it not requires root.

Источник

How can I automatically shutdown the system after a specific time?

and just replace 60 with whatever number of minutes you want to take.

shutdown +m 60 doesn’t work for me. gives me an error message and says that there’s an invalid time value.

I thought it was odd. it shouldn’t be «+m 60», it should be «+60» (or maybe more like «-h +60». The bottom of this page seems to know how it works: linux.org/lessons/beginner/l5/lesson5a.html.

alt text

  • You can use gshutdown
  • After installation it can be found under Applications → Accessories → GShutdown
  • Also have a look at this method.

i didn’t down vote. But Gshutdown doesn’t work smoothly here. When i choose shutdown it insteads log the user out?

Читайте также:  Linux mint рабочие места

Because the topic abt shutting down PC after certain period of inactivity is redirected to this topic, I will explain this issue here.

I spent lots of time to solve this problem, so I find it useful to share it, to make the same issue simple for others. I hv tried different programmes but they haven’t work for me so I found using short script with cronjob the best solution.

I will copy it below and then explain improvements to make it work:

Install xprintidle. This tool gives the idle time of a user.

sudo apt-get install xprintidle 

Make a script autoshutdown.sh which checks for the idle time and instructs the computer to shutdown if idle for 30 minutes.

idle=$(xprintidle) if [ $idle -gt 1800000 ]; then shutdown -h now fi 

Make a cronjob for this that checks from time to time if the system has been idle for too long and if it has been idle for a longer than 30 minutes it will shutdown. Note that the cronjob has to be made for the root user.

This script needs some improvements to work, like:

idle=`env DISPLAY=:0 su OUR_USER -c xprintidle 2>&1` 

OUR_USER is the user we refer to for checking idle time (not root user)

DISPLAY=:0 is correct for one desktop display (run env command to read DISPLAY in your situation)

if script is run by OUR_USER , line above can be reduced:

idle=`env DISPLAY=:0 xprintidle 2>&1` 

if script is run by OUR_USER , shutdown command should be preceded by sudo

My script was run from cron by line in cron file:

*/5 * * * * /home/OUR_USER/autoshutdown.sh 

If script is not run by root we should remember to add the line:

ALL ALL=(ALL) NOPASSWD: /sbin/shutdown 

in sudoers file, so shutdown command won’t need a password to be executed.

I tried such cronjobs on 2 similar distro Lubuntu 12.04.4 RC LXLE 32-bit ( http://www.lxle.net/ )

In one system it works only using root cronjob set in file:

CAVEAT

Another problem is that xprintidle in my system has given sometimes random for me values and sometimes logically incremental. The final result — my system has been usually shutdown after 20 mins maybe, if I set the max idle value to 30 mins. I think the culprit is xscreensaver which doesn’t work as is set by entered parameters.

Источник

Оцените статью
Adblock
detector