How do I shut down a Linux server after running for 60 minutes?

There are a several options.

  • Provide time directly to shutdown -P:

    shutdown -P +60
    

    Note that shutdown man page also points out:

    If the time argument is used, 5 minutes before the system goes down the /run/nologin file is created to ensure that further logins shall not be allowed.

  • Use at command.

  • Create a systemd unit file or init script which runs shutdown -P 60 at startup.

  • Use cron's @reboot to run command after boot.

    Add to (root) crontab:

    @reboot shutdown -P +60
    

For the last two methods, you could also use sleep 3600 && shutdown -P now instead of using time argument to shutdown to delay the shutdown for 60 minutes. This way logins are possible to the last moment before shutdown is issued.


This looks like an XY problem.

My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.

If you shut down after 60 minutes, you run the risk that you may be running a particularly complicated problem and just need to have more time. Many of the previous solutions would not make it easy to delay the shutdown.

If the task is not an interactive task, but instead are a scripted task that is automatically triggered from another machine, @sdkks gave a great solution for that; you really should just task the machine to run poweroff as soon as the script and all its tasks finishes.

However, if your task is an interactive task, I'd suggest to do idle detection instead.

If you do your task in a GUI (X11) you can detect the idle GUI sessions using the approach described here: Run a command when system is idle and when is active again

If you do the task through a terminal, you can detect logged in users using the who command. You can set up a cronjob that shuts the machine down if who returns an empty result. Note that this will be quite a conservative approach; it will not shut down the system if you left the console connected, but idle.

If you want to be a bit more aggressive and disconnect idle terminal sessions as well, you can combine the previous approach with automatically disconnecting idle SSH sessions ClientAliveInterval and ClientAliveCountMax. Another approach for this if you don't have SSH, but have a local terminal session, is to use the terminal idle time as returned by the w command.


If you execute your tasks as the same user every time, you can simply add the shutdown command, optionally with the option -P, to your profile. The number stands for the amount of minutes the shutdown command is delayed. Make sure your user has the ability to execute the shutdown command via sudo without a password.

echo "sudo shutdown -P +60" >> ~/.profile

Added:

After reading most of the other answers I love the option to add "@reboot /<path to script/command>" in (root) cron. Answer given by sebasth as his fourth option. You may either add the shutdown command or write a small script that executes the shutdown and execute that via cron. WoJ also has a great solution if you use systemd.

Tags:

Shutdown

Cron