* shutdown script
Posted on September 29th, 2011 by Alex. Filed under Linux.
If computers are not in use, I would like to shut dem down automatically to save power and also to extend the lifetime of the hardware (especially mechanical HDD). Many users forget or are too lazy to shut down their machines, when they e.g. leave their office. That is why I wrote this little script that can be executed as a cronjob as root.
It will not shut down the computer, if one of the following conditions are met:
- There exists is a file called
dontShutdownin/tmp - Users are logged in
- Any screen sockets exist.
- The load on the machine is more than 20% (of one core/CPU)
You can comment/decomment the if conditions in the script accordingly. The reason to include the last condition was to ensure, that the computer is shut down, if it is idle, since I have the habit of not terminating screen sessions. However, if you e.g. download a big file in a screen session, which does not require many CPU resources, then the computer is also shut down. Similar, if you decrease the load of the CPU to such a low level that e.g. service which come sporadically into life, cause a short necessity of CPU resources, the machine is never shut down. Adjust the parameters according to you needs. After that, ensure that the script is executed periodically by e.g. putting it into the cron of the root user (here every 30 minutes):
30 * * * * /root/shutDownIfUnused.sh > /dev/null 2>&1
License
The annoying legal part: Do whatever you want with the file.