How to shut down a Linux server after running for 60 minutes

cronshutdown

I have a server that it is normally switched off for security reasons. When I want to work on it I switch it on, execute my tasks, and shut it down again. My tasks usually take no more than 15 minutes. I would like to implement a mechanism to shut it down automatically after 60 minutes.

I've researched how to do this with cron, but I don't think it's the proper way because cron doesn't take into account when the server was last turned on. I can only set periodic patterns, but they don't take that data into account.

How could I do this implementation?

Best Answer

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.

Related Question