Bash script to shutdown a computer if certain criteria are met:-
- None of the listed clients are active.
- None of the listed processes are active.
- None of the listed Sony TVs are active.
- No users are logged-on.
- No torrents are active, such as downloading or seeding.
- No TV tuner is active, such as recording or streaming.
Download sleep.sh
into /usr/local/bin
so that it can be run from anywhere:-
sudo wget https://raw.githubusercontent.com/madaboutbrighton/sleep/master/sleep.sh -O /usr/local/bin/sleep.sh
sudo chmod a=rx /usr/local/bin/sleep.sh
Download call-sleep
into /etc/cron.hourly
so that it can be run automatically every hour:-
sudo wget https://raw.githubusercontent.com/madaboutbrighton/sleep/master/call-sleep -O /etc/cron.hourly/call-sleep
sudo chmod u=rwx,og-rwx /etc/cron.hourly/call-sleep
You can then customise call-sleep
based upon your own options:
sudo nano /etc/cron.hourly/call-sleep
sleep.sh
This would shutdown a computer if no users are logged-on, and no cp
, mv
, rsync
or scp
processes are running.
sleep.sh --clients "192.168.1.12 192.168.1.34"
This would shutdown a computer if 192.168.1.12
and 192.168.1.34
are not reachable on the network, no users are logged-on, and no cp
, mv
, rsync
or scp
processes are running.
sleep.sh --torrent-level "active_downloads" --torrent-type "transmission" --torrent-password "my_torrent_password"
This would shutdown a computer if transmission
is not actively downloading anything, no users are logged-on, and no cp
, mv
, rsync
or scp
processes are running.
sleep.sh --sony-tvs "192.168.1.67 192.168.1.89" --sony-tv-auth-psk "my_sony_psk"
This would shutdown a computer if the Sony TVs 192.168.1.67
and 192.168.1.89
are not active, no users are logged-on, and no cp
, mv
, rsync
or scp
processes are running.
A shutdown will only occur if the specified clients are not reachable on the network. A space-separated list of IP addresses.
Perform a trial run with no shutdown being implemented. Verbose output will be shown printed to the terminal.
A shutdown will only occur if the specified processes are not running. A space-separated list. Default value is cp mv rsync scp
.
Location of wake-up alarm. If a TV tuner is detected, and a future recording date has been found, an alarm will be set to the specified RTC folder. Default value is /sys/class/rtc/rtc0
.
Minimum time in seconds needed to shutdown the computer properly. Default value is 300
.
Minimum time in seconds needed to start-up the computer properly. Default value is 180
.
The password to be used when using the Sony TV API.
A shutdown will only occur if the specified Sony TVs are not active. A space-separated list of IP addresses.
The type of the torrent client. Currently only supports transmission
.
A shutdown will only occur if their are no torrents in the specified range. Currently only supports active
and active_downloads
.
active
- includes torrents being downloaded and seeded.active_downloads
- only includes torrents being downloaded.
The user name for the specified torrent client.
The password for the specified torrent client.
The type of the TV tuner. Currently only supports tvheadend
.
The user name for the specified TV tuner.
The password for the specified TV tuner.
Maximum time in hours not to wake-up for updating EPG. Default value is 48
.
Increase the amount of information printed to the terminal.
You can call sleep.sh
directly from the command-line. However, it is highly likely that you would want to call sleep.sh
on a regular basis, so that a shutdown occurs if the specified criteria are met. So another script is required that contains a call to sleep.sh
. This script is named call-sleep
. By placing it into one of the cron directories it can be automatically run on a regular basis. Within this script, you can add your own specific options, such as client IP addresses to be checked.
The options within call-sleep
could contain IP addresses and passwords. For security reasons it makes sense to restrict access to this script.