Implement shutdown-if-not-in-use.
This commit is contained in:
		
							parent
							
								
									f1c67852da
								
							
						
					
					
						commit
						9f6fa677c4
					
				
					 5 changed files with 84 additions and 0 deletions
				
			
		
							
								
								
									
										47
									
								
								roles/lmn_kde/files/pwroff
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										47
									
								
								roles/lmn_kde/files/pwroff
									
										
									
									
									
										Executable file
									
								
							|  | @ -0,0 +1,47 @@ | ||||||
|  | #!/bin/bash | ||||||
|  | # | ||||||
|  | # logout idle users and shutdown machine | ||||||
|  | # | ||||||
|  | set -eu | ||||||
|  | 
 | ||||||
|  | action="systemctl poweroff" | ||||||
|  | uptime=$(cat /proc/uptime | cut -f1 -d.) | ||||||
|  | maxidle=3600 | ||||||
|  | 
 | ||||||
|  | u=($(loginctl list-users --no-legend  | sort -hr | head -1)) | ||||||
|  | una=${u[1]} | ||||||
|  | uid=${u[0]} | ||||||
|  | 
 | ||||||
|  | talk2dbus() { | ||||||
|  |     local display=":$(ls /tmp/.X11-unix/* | sed 's#/tmp/.X11-unix/X##' | head -n 1)" | ||||||
|  |     sudo -u $una DISPLAY=$display \ | ||||||
|  | 	 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$uid/bus "$@" 2>/dev/null | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | ######## | ||||||
|  | 
 | ||||||
|  | ## shutdown if nobody is loged in: | ||||||
|  | if [[ -z "$una" ]] ; then | ||||||
|  |   exec $action | ||||||
|  | fi | ||||||
|  | 
 | ||||||
|  | # FIXME: find idle time independent of running screensaver | ||||||
|  | if ! t=$(talk2dbus qdbus org.kde.screensaver /ScreenSaver GetActiveTime) ; then | ||||||
|  |     echo "No graphical logins found." | ||||||
|  | else | ||||||
|  |     idle=$(( t / 1000 )) | ||||||
|  |     if [[ $idle -gt $maxidle ]] ; then | ||||||
|  |         talk2dbus notify-send -i system-shutdown -u critical -a 'Important System Information' \ | ||||||
|  | 	          'Please log out, the system will shut down soon!' \ | ||||||
|  | 	          'There has been no activity for too long.' | ||||||
|  |         ## shutdown: | ||||||
|  |         #talk2dbus qdbus org.kde.ksmserver /KSMServer logout 1 2 0 | ||||||
|  |         ## logout: | ||||||
|  |         talk2dbus qdbus org.kde.ksmserver /KSMServer logout 1 0 0 | ||||||
|  |         echo "Log-out user $una after being idle for $idle seconds." | ||||||
|  |     else | ||||||
|  |         echo "The user $una has been idle for $idle seconds." | ||||||
|  |     fi | ||||||
|  | fi | ||||||
|  | 
 | ||||||
|  | #w -s | grep tty | sed "s/[[:space:]]\+/ /g" | cut -f4 -d ' ' | ||||||
							
								
								
									
										6
									
								
								roles/lmn_kde/files/pwroff.service
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								roles/lmn_kde/files/pwroff.service
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,6 @@ | ||||||
|  | [Unit] | ||||||
|  | Description=Run pwroff script | ||||||
|  | 
 | ||||||
|  | [Service] | ||||||
|  | Type=simple | ||||||
|  | ExecStart=/usr/local/sbin/pwroff | ||||||
							
								
								
									
										9
									
								
								roles/lmn_kde/files/pwroff.timer
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								roles/lmn_kde/files/pwroff.timer
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,9 @@ | ||||||
|  | [Unit] | ||||||
|  | Description=Run pwroff script every 10 min after 60 min uptime | ||||||
|  | 
 | ||||||
|  | [Timer] | ||||||
|  | OnBootSec=60min | ||||||
|  | OnUnitActiveSec=10min | ||||||
|  | 
 | ||||||
|  | [Install] | ||||||
|  | WantedBy=timers.target | ||||||
|  | @ -7,3 +7,8 @@ | ||||||
|     name: tmp.mount |     name: tmp.mount | ||||||
|     enabled: yes |     enabled: yes | ||||||
|   listen: enable tmp.mount |   listen: enable tmp.mount | ||||||
|  | 
 | ||||||
|  | - name: enable pwroff.timer | ||||||
|  |   systemd: | ||||||
|  |     name: pwroff.timer | ||||||
|  |     enabled: true | ||||||
|  |  | ||||||
|  | @ -137,6 +137,23 @@ | ||||||
|       AllowSuspendThenHibernate=no |       AllowSuspendThenHibernate=no | ||||||
|       AllowHybridSleep=no |       AllowHybridSleep=no | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|  | - Name: Copy pwroff script | ||||||
|  |   template: | ||||||
|  |     src: pwroff | ||||||
|  |     dest: /usr/local/sbin/ | ||||||
|  |     mode: 0755 | ||||||
|  | 
 | ||||||
|  | - name: Provide service and timer for pwroff script | ||||||
|  |   copy: | ||||||
|  |     src: "{{ item }}" | ||||||
|  |     dest: "/etc/systemd/system/{{ item }}" | ||||||
|  |     mode: 0644 | ||||||
|  |   with_items: | ||||||
|  |     - pwroff.service | ||||||
|  |     - pwroff.timer | ||||||
|  |   notify: enable pwroff.timer | ||||||
|  | 
 | ||||||
| ################# general settings ################## | ################# general settings ################## | ||||||
| - name: Protect grub menu entries | - name: Protect grub menu entries | ||||||
|   blockinfile: |   blockinfile: | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 Andreas B. Mundt
						Andreas B. Mundt