Autorun-script needs shutdown-handler?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
DarkgrayLord

Autorun-script needs shutdown-handler?

Post by DarkgrayLord »

I wrote a script so activate a separate set of wallpapers for each workspace.

Code: Select all

#!/bin/bash
#---- HOW TO -- read me to get this started
# change the values in USERSETTINGS so that
# WALLPAPER_ROOT is a directory with subdirectories with images only
# and WALLPAPER_SETS names those subdirectories you want to associate with your work spaces 
# DELAY can be set optional for different timeouts
# run the script manually - if each workspace rotates its associated images
# set is as a start scrip (look for startup in mint menu)
# log of and on tho get it working

#---- USERSETTINGS ---- user defined settings to be changed
#full path to images directory; do NOT use ~ here... 
WALLPAPER_ROOT="/home/hans/Wallpaper/"
#subsets of wallpapers per workspace
WALLPAPER_SETS=("wood" "mountain" "sea")
#circle every xxx in seconds (initial 300 = 5 minutes
DELAY=300

#---- functions to be modified only if you want to change the overall behaviour

#set the wallpaper based on current desktop and timestamp
setWallpaper() {
  pics=()
  echo scanning "$CURRENT_WALLPAPER_SET"
  # collect pictures in current wallpaper set
  for file in "$CURRENT_WALLPAPER_SET"/*; do
    pics=("${pics[@]}" "$file")
  done
  total=${#pics[@]}
  # pictures must not be empty
  if [ "$total" != 0 ]; then    
    bySeconds=$(bc <<< $(date "+%s/$DELAY"%"$total"))
    wallpaper=${pics["$bySeconds"]}
    gsettings set org.gnome.desktop.background picture-uri file:///"$wallpaper"
  fi
}

# loop forever for time based image rotation
loopImages() {
  while [[ 1 -eq 1 ]]; do
    setWallpaper
    sleep "$DELAY"
  done
}

# set initial values
init() {
  #set initial wallpaper folder
  export CURRENT_WALLPAPER_SET=$WALLPAPER_ROOT${WALLPAPER_SETS[0]}
  # set the desktop names
  WALLPAPER_SETS_STR=$(printf "'%s', " ${WALLPAPER_SETS[*]})
  WALLPAPER_SETS_STR="[$(echo ${WALLPAPER_SETS_STR} | rev | cut -c 2- | rev)]"
  gsettings set org.cinnamon.desktop.wm.preferences workspace-names "${WALLPAPER_SETS_STR}"
  gsettings set org.cinnamon number-workspaces ${#WALLPAPER_SETS[@]}
}

#---- MAIN ---------------------------------------------------------------------

init
# onChange:
xprop -root -spy _NET_CURRENT_DESKTOP | while read -r; do
  if [[ "$!" != "" ]]; then kill $!; fi # stop the timed loop
  # get the desktop number:
  CURR_DESKTOP_NUM="${REPLY: -1}"
  CURRENT_WALLPAPER_SET=$WALLPAPER_ROOT${WALLPAPER_SETS[CURR_DESKTOP_NUM]}
    loopImages& # restart the timed loop
done

As you can see, it loops forever in reading the current desktop - that part was copied from various scripts I found for workspace scripting. But there is another "infinite" loop to rotate through the images restarted every time the workspace changes.
The script works fine for me, I just have the impression, that sometimes shutting down taks some extra time and I am not sure if the inner loop (loopImages) is responsible for this.

So one question is - what would be a good way to determine if this script delays shuidown due a respectful waiting of the system for the process to stop

The other question is (asuming that the scrip is delaying the shutdown) how to detect a requesting shutdown signal and speed up the stop (probably hanging around in "sleep")
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
catweazel
Level 19
Level 19
Posts: 9763
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: Autorun-script needs shutdown-handler?

Post by catweazel »

DarkgrayLord wrote: Sat Feb 02, 2019 5:52 pm The other question is (asuming that the scrip is delaying the shutdown) how to detect a requesting shutdown signal and speed up the stop (probably hanging around in "sleep")
viewtopic.php?f=247&t=278535&p=1532781#p1532451
"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
gm10

Re: Autorun-script needs shutdown-handler?

Post by gm10 »

DarkgrayLord wrote: Sat Feb 02, 2019 5:52 pm (asuming that the scrip is delaying the shutdown)
Your script contains no code to delay shutdown. It will simply receive a signal to terminate and do just that.
Locked

Return to “Scripts & Bash”