32

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?

MrAxlee
  • 507
  • Do you want to shutdown in 02:00 AM? – αғsнιη Jan 02 '15 at 08:56
  • 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? – MrAxlee Jan 02 '15 at 08:58
  • 1
    You can refer the url for automatic shutdown. http://askubuntu.com/questions/19774/how-can-i-automatically-shutdown-the-system-after-a-specific-time – BDRSuite Jan 02 '15 at 09:20
  • 1
    @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 :) – MrAxlee Jan 02 '15 at 09:28
  • @MrAxlee... So, you want the system to shut down n hours after turning on? – Kaz Wolfe Jan 02 '15 at 09:31
  • @Whaaaaaat Nono, other way around :P The question vembutech linked is how to shut down n hours after turning on, which can be modified to suit my needs but I still cannot run additional windows (like terminal, or gShutdown which was linked to as an answer.

    Trying to decide which cron answer would be best to mark as a solution!

    – MrAxlee Jan 02 '15 at 09:34
  • you don't need additional tools if you want the system to shut down after some time. – Kaz Wolfe Jan 02 '15 at 09:36

3 Answers3

49

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


Cron Format:

MM HH DD OO WW command

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

Augusto
  • 457
Kaz Wolfe
  • 34,122
  • 21
  • 114
  • 172
  • @JacobVlijm When was that added? I don't see it in the official Cron docs. – Kaz Wolfe Jan 02 '15 at 09:31
  • 1
    I'll look it up... – Jacob Vlijm Jan 02 '15 at 09:37
  • 1
    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. – Jacob Vlijm Jan 02 '15 at 09:47
  • @Whaaaaaat Great answer, works perfectly, thanks!. What's the difference between -h and -s? – MrAxlee Jan 02 '15 at 12:28
  • 1
    @MrAxlee -s doesn't seem to be "proper syntax." -h means to halt or shutdown the system (system's choice - shutdown preferred) – Kaz Wolfe Jan 02 '15 at 20:42
  • if you're on mac you need to specify the full path /sbin/shutdown since the PATH for crontab on mac only contains /usr/bin:/bin – Picard Nov 14 '18 at 08:24
  • Why do you need root for this? I mean user can power off the system, use of user’s CEO tab should be enough, no? – 0andriy Jul 15 '19 at 07:10
11

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

d a i s y
  • 5,511
  • 3
    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. – Jaakko Jan 11 '17 at 12:51
  • @Jaakko That's still true with Ubuntu 20.04. – AaronD Nov 01 '20 at 19:42
6

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:

sudo crontab -e

edit

30 18 * * * poweroff
d a i s y
  • 5,511
  • @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. – d a i s y May 15 '18 at 03:38
  • 1
    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 May 22 '18 at 21:36
  • @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. – d a i s y May 23 '18 at 03:43
  • Straightforward solution. Has been running for 8 days, no issues. – Billal Begueradj Jun 16 '23 at 14:00