Creating useful Pacman hooks

The documentation I read on Gnome terminal said you had to configure a profile to execute commands on opening. Too bad the generic command doesn't work for all terminals, as that would make life way simpler.

I use a separate script for opening my yakuake terminal with four separate tabs preconfigured and running different commands when it launches.

I probably should have included more notes for use with other terminals, but truthfully I didn't know if anyone would even bother to test this out.

I find playing and learning this stuff all very much fun myself as well. You have a great attitude @sgs.

1 Like

I try this gnome-terminal --window-with-profile=tbg-hook -e command from second link, seems old or wrong.

[sgs@deepin hooks]$ gnome-terminal --window-with-profile=tbg-hook -e command
# _g_io_module_get_default: Found default implementation dconf (DConfSettingsBackend) for ‘gsettings-backend’
# watch_fast: "/org/gnome/terminal/legacy/profiles:/" (establishing: 0, active: 0)
# watch_fast: "/org/gnome/terminal/legacy/profiles:/:b1dcc9dd-5262-4d8d-a863-c897e6d979b9/" (establishing: 0, active: 0)
# Profile 'tbg-hook' specified but not found. Attempting to fall back to the default profile.
# Die Option »-e« ist veraltet und wird in einer späteren Version von GNOME-Terminal entfernt.
# Verwenden Sie »-- «, um die Optionen abzubrechen. Schreiben Sie die auszuführende Befehlszeile dahinter, die ausgeführt werden soll.
# watch_established: "/org/gnome/terminal/legacy/profiles:/" (establishing: 1)
# watch_established: "/org/gnome/terminal/legacy/profiles:/:b1dcc9dd-5262-4d8d-a863-c897e6d979b9/" (establishing: 1)
# _g_io_module_get_default: Found default implementation gvfs (GDaemonVfs) for ‘gio-vfs’
# Theme parsing error: gtk.css:3611:22: 'none' is not a valid color name
# watch_fast: "/org/gnome/terminal/legacy/" (establishing: 0, active: 0)
# unwatch_fast: "/org/gnome/terminal/legacy/" (active: 0, establishing: 1)
# watch_established: "/org/gnome/terminal/legacy/" (establishing: 0)
# unwatch_fast: "/org/gnome/terminal/legacy/profiles:/:b1dcc9dd-5262-4d8d-a863-c897e6d979b9/" (active: 1, establishing: 0)
# unwatch_fast: "/org/gnome/terminal/legacy/profiles:/" (active: 1, establishing: 0)

Simplest, write in the post about. Do not use gnome terminal :smiley:

Profile cannot be saved

1 Like

Gnome terminal should work. It just doesn't work with the -e parameter like most other terminals. Check the links I posted. One of them might have directions on how to configure gnome terminal.

1 Like

To be perfectly honest your original idea to use a bash alias is 10x simpler than writing a hook and 2 scripts, but it was all for a learning experience for me. I love a good challenge.

2 Likes

Yeah, sometimes I'm too lazy to make my life hard. :smiley: :joy:

1 Like

I have some manjaro on desktop, i use one pacman hook by manjaro for create good entries in my grub
run hook only when etc/lsb-release file is updated

/etc/pacman.d/hooks/lsb-release.hook

[Trigger]
Operation = Install
Operation = Upgrade
Type = File
Target = etc/lsb-release

[Action]
Description = lsb-release change description for grub
When = PostTransaction
Exec = /usr/bin/sed -i 's/^DISTRIB_DESCRIPTION.*/DISTRIB_DESCRIPTION="Manjaro Cinnamon"/' /etc/lsb-release
3 Likes

Thanks for posting @papajoke.

We're starting to get a nice little collection of hooks going here. I did a rewrite of my earlier timeshift hook combining auto cancellation with a time threshold, as well as with user input via the keyboard. I'll post it shortly as it seems to work quite well and is much nicer with the combined features.

2 Likes

we can also create "security" hooks

never remove a package, one example :

[Trigger]
Operation = Remove
Type = Package
Target = pacman
Target = bash

[Action]
Description = REQUIRED, no uninstall possible
When = PreTransaction
Exec = /usr/bin/false
AbortOnFail

Warning: we can be blocked if a package changes repository, it is temporarily removed ( for example : extra<->community), remove extension ".hook" to pass

3 Likes

Add packages descriptions

[Trigger]
Operation = Upgrade
Type = Package
Target = *

[Action]
Description = Descriptions upgrade
When = PostTransaction
Exec = /etc/pacman.d/hooks.bin/descriptions.sh 20
NeedsTargets

we can pass a parameter to script (here 20) : run this hook only if we have less than 20 packages

#!/bin/bash
# show packages description
#echo "user_agent: $HTTP_USER_AGENT" pacman or pamac
[[ "${HTTP_USER_AGENT:0:2}" != 'pa' ]] && exit 2
declare -r max="${1:-5}"
declare -a packages=($(cat -))

if ((${#packages[@]} <= $max)); then
	for package in "${packages[@]}"; do
	   LANG=C pacman -Qi "${package}" | awk -F: '/^Desc/ {print "'${package}': "$2}'
	done
fi

result:

...
:: Running post-transaction hooks...
(1/4) Descriptions upgrade
go:  Core compiler tools for the Go programming language
guile:  Portable, embeddable Scheme implementation written in C
(2/4) Arming ConditionNeedsUpdate...
...

it's very good for me, to learn more about packages

3 Likes

Very nice indeed. :+1:

Other for save in journald ... :crazy_face: save only removed :wink:

[Trigger]
Operation = Remove
Type = Package
Target = *

[Action]
Description = write on journald OPERATION=remove , use journalctl -t pacman for read
When = PostTransaction
Exec = /etc/pacman.d/hooks.bin/journald.sh remove
Depends = util-linux
NeedsTargets

we pass "remove" to parameter, so we can make
journalctl -t pacman OPERATION=remove but with other hook, we can also pass "update"/"install"

#!/bin/bash
# save in journald event in parameter
[[ "${HTTP_USER_AGENT:0:6}" != 'pa' ]] && exit 2
declare -r operation="$1"
declare -a packages=()

# journalctl -t pacman OPERATION=remove : script history in journald
function logd(){
   local msg=$1
   local niveau=${2:-6}
   printf "%s\n%s\n%s\n%s" \
          MESSAGE="${operation} : ${msg}"  \
          SYSLOG_IDENTIFIER="pacman" \
          PRIORITY="${niveau}" \
          OPERATION="${operation}" \
          | logger --journald
}

while read -r package; do
	packages=("$package" "${packages[@]}")
done
logd "${packages[*]}" 6  # 6= level in journald

for read my journal:

 journalctl -t pacman
-- Logs begin at Mon 2019-04-15 07:22:55 CEST, end at Wed 2019-07-10 09:01:52 CEST. --
avril 18 21:12:33 tower pacman[14383]: remove : numix-frost-themes gtk-engine-murrine
-- Reboot --
avril 22 23:54:06 tower pacman[3402]: remove : libmagick
-- Reboot --
avril 25 18:08:09 tower pacman[7047]: remove : pamac-qt-git
avril 25 18:44:05 tower pacman[22960]: remove : pamac-qt
-- Reboot --
avril 30 07:01:36 tower pacman[27806]: remove : python-selenium
4 Likes

only for demo/ideas purposes, we can make same action as pacman.conf "NoExtract"

[Trigger]
Operation = Install
Operation = Upgrade
Type = Package
Target = ki18n

[Action]
Description = Delete unnecessary locales
When = PostTransaction
Exec = /etc/pacman.d/hooks.bin/ki18n.sh
#!/bin/sh
# rm some files if it's not my language.
pacman -Qlq ki18n | grep "LC_MESSAGES" | grep -Ev "/de|/en" | grep -v "/$" | xargs truncate -s 0
pacman -Qlq ki18n | grep "LC_MESSAGES" | grep -Ev "/de|/en" | grep -v "/$"
# return to rm :
/usr/share/locale/af/LC_MESSAGES/ki18n5.mo
/usr/share/locale/ar/LC_MESSAGES/ki18n5.mo
/usr/share/locale/as/LC_MESSAGES/ki18n5.mo
/usr/share/locale/be/LC_MESSAGES/ki18n5.mo
/usr/share/locale/be@latin/LC_MESSAGES/ki18n5.mo
/usr/share/locale/bg/LC_MESSAGES/ki18n5.mo
/usr/share/locale/bn/LC_MESSAGES/ki18n5.mo
/usr/share/locale/bn_IN/LC_MESSAGES/ki18n5.mo
/usr/share/locale/br/LC_MESSAGES/ki18n5.mo
/usr/share/locale/bs/LC_MESSAGES/ki18n5.mo
/usr/share/locale/ca/LC_MESSAGES/ki18n5.mo
/usr/share/locale/ca@valencia/LC_MESSAGES/ki18n5.mo
...
3 Likes

Work great, thank you :slight_smile:

Too bad that timeshift doesn't also use DESCRIPTION, then I wouldn't have to use it manually :wink:

[sgs@deepin ~]$ cat /etc/lsb-release 
DISTRIB_ID=ManjaroLinux
DISTRIB_RELEASE=18.0.4
DISTRIB_CODENAME=Illyria
DISTRIB_DESCRIPTION="Manjaro Deepin"

Competition Question :wink:

Can I check when Deepin replaces the starter icon with a hook (Version update/upgrade)?

Script to restore own icons that Deepin changed
#!/bin/bash
# - 15.05.2018 -
Hilfe(){
  echo "${0##*/} ersetzt das starter icon"
  echo "Keine Syntax für ${0##*/}"
  exit
}
test "${1}" = "-h" -o "${1}" = "-help" -o "${1}" = "--help" && Hilfe

# Check if the current user is "root" otherwise restart the script with "sudo"...
[ "$(whoami)" != "root" ] && exec sudo -- "$0" "$@"

# Instruct the Script to Abort for any command error...
set -e

cd /usr/share/icons/deepin/places/256/

cp /home/sgs/.icons/deepin-launcher.svg /usr/share/icons/deepin/places/256/deepin-launcher.svg

cd /usr/share/icons/deepin/apps/48/

cp /home/sgs/.icons/catfish.svg /usr/share/icons/deepin/apps/48/catfish.svg

exit 0

this ?

[Trigger]
Operation = Install
Operation = Upgrade
Type = File
Target = usr/share/icons/deepin/apps/*/catfish.svg
# or Target = usr/share/icons/deepin/apps/48/catfish.svg
Target = usr/share/icons/deepin/places/*/deepin-launcher.svg

whe have for "Type" : File or Package
if File, dont write the first "/"

3 Likes

Ahh, Target does not mean target :), it means, look for change/Event.

Zusammenfassung

target or Target , or no matter

</OT> :wink:

run hook (by Exec) only if Target is in Operation (Upgrade, Install, Remove) :wink:

with When=, we evaluate hook before or after transaction (PreTransaction, PostTransaction)

2 Likes

Timeshift Pacman Hook:


This is a revised version of my earlier timeshift hook with improved options for backup cancellation.

This hook will trigger an automatic Timeshift backup prior to installing a system upgrade. It also contains several methods to cancel the backup. This version includes automatic backup cancellation if another backup was recently made within an adjustable time frame. It also incorporates a feature that allows the cancellation of any backup with a 10 second (adjustable) countdown window via the keyboard. While the countdown is active you can cancel any backup by pressing ENTER

You may receive a popup error message when a timeshift snapshot is cancelled. The error message is irrelevant as it is only notifying about the failure of the scheduled timeshift backup (which was intended).

Preparation for creating the hook and scripts:

Create a sub-directory for the pacman hook:

sudo mkdir /etc/pacman.d/hooks

Create a sub-directory for pacman hook related scripts:

sudo mkdir /etc/pacman.d/hooks.bin

Now that we have the directories created to locate the hook and scripts we can proceed with writing them.


Timeshift Pacman Hook:


With a root capable text editor create the timeshift pacman hook:

/etc/pacman.d/hooks/50-timeshift.hook
#/etc/pacman.d/hooks/50-timeshift.hook

[Trigger]
Operation = Upgrade
Type = Package
Target = *

[Action]
Description = pre-upgrade timeshift snapshot
When = PreTransaction
Exec = /bin/sh -c "sudo -Hiu user konsole -e /etc/pacman.d/hooks.bin/killshot.sh | /etc/pacman.d/hooks.bin/timeshift.sh"

It is sometimes difficult to use variables in pacman hooks, so there are portions of code that you will need to insert your own specific info.

Exec = /bin/sh -c "sudo -Hiu user konsole -e

In the above portion of the "Exec" line, replace "user " with your own username. Replace " konsole" with the name of the terminal emulator that you use on your system. Not all terminal emulators will launch properly using the "-e" (execute) parameter. The gnome terminal may be problematic to auto-execute with this script. Unlike many other terminal emulators gnome-terminal doesn't accept the "-e" parameter. If any knows the best method to auto-launch scripts with gnome-terminal please post the info.

This script must be used with a terminal emulator capable of accepting the "-e" (execute) parameter at launch. I do not know exactly which terminals are capable of this, but Yakuake and Konsole seem to handle this task well. You may need to experiment using different terminals to find the correct method of auto launching the script via a pacman hook.

This was a very difficult hook to get working properly with user keyboard intervention. I finally solved the problem by creating two separate scripts that require launching simultaneously via the hook. This method seems to work very well from my testing so far.


Timeshift Launch Script:


This script launches a timeshift backup automatically whenever an upgrade is initiated. The script also contains a method to automatically cancel any backup if another snapshot was taken recently. This function is to limit repeated snapshots within a short period of time. Repeated snapshots become a major annoyance when you run into an upgrade problem that you need to debug.

This script will cancel the snapshot if a prior snapshot was taken within the predetermined window of time you select. To change the time frame, replace "-mmin -60" (1hour) with another unit of time. If you would prefer the trigger to be set at two hours then simply change the time unit to "-mmin -120".

You will need to replace the path "/run/media/user/1TB/timeshift/snapshots-ondemand" in the script with your actual timeshift backup location. The backup is stored within the "/snapshots-ondemand" sub-directory inside the main timeshift directory.

The script also triggers a small GUI popup message when the backup is beginning.

Create the script to initiate (or automatically cancel) the timeshift snapshot:

/etc/pacman.d/hooks.bin/timeshift.sh
#!/bin/bash
#/etc/pacman.d/hooks.bin/timeshift.sh
find /run/media/user/1TB/timeshift/snapshots-ondemand -mmin -60 | grep $(date +%Y-%m-%d)
if [ $? -eq 0 ]; then
    echo "timeshift backup canceled, time threshold not met"
else
    sudo -Hiu user /bin/sh -c 'DISPLAY=:0 notify-send "Timeshift_Backup_Beginning"'
        echo " "
            echo "Initiating Pre-Update Timeshift Snapshot"
            echo " "
        echo "You have 10 seconds to cancel timeshift snapshot"
    /usr/bin/timeshift --create --comments "timeshift-pacman-hook-snapshot"
fi
exit
 

Save the script, then make the script executable:

sudo chmod +x /etc/pacman.d/hooks.bin/timeshift.sh

Killshot Script:


This script allows for manual cancellation of the timeshift snapshot via keyboard intervention. It provides a 10 second (adjustable) countdown window during which you may cancel the snapshot. While the countdown is active you can cancel any backup by pressing ENTER

The number of seconds that the countdown window is open can be adjusted in this line:

for (( i=9; i>0; i--)); do

If you would like to increase the time window for cancellation to 20 seconds then adjust it as follows:

for (( i=20; i>0; i--)); do

The script also triggers a small GUI popup message when the backup is aborted.

Create the script to cancel the timeshift snapshot via keyboard intervention:

/etc/pacman.d/hooks.bin/killshot.sh
#!/bin/bash
#/etc/pacman.d/hooks.bin/killshot.sh
  echo "press any key to cancel timeshift snapshot"
      sleep 1
for (( i=9; i>0; i--)); do
    printf "\n$i seconds left to cancel snapshot  ... hit any key "
    read -s -n 1 -t 1 key
if [ $? -eq 0 ]; then
    sudo kill -2 `ps -ef|grep -i timeshift| grep -v grep| awk '{print $2}'`
        echo " "
            echo "aborting timeshift snapshot" 
      echo " "
   sudo -Hiu user /bin/sh -c 'DISPLAY=:0 notify-send "Timeshift_snapshot_aborted"'
fi
done

Save the script, then make the script executable:

sudo chmod +x /etc/pacman.d/hooks.bin/killshot.sh

This is more complicated to write than other timeshift hooks, but far more versatile in my opinion.

4 Likes

This one need a special place but all scripts together are better, please could you link in post #1 to each hook with name orphans - timeshift - etc. , so new/user could find it quick?
or

manjaro-wiki-hooks

2 Likes

I was planning on getting around to that, but Rome wasn't built in a day you know. :smile:

I've been busy with these silly little scripting projects of mine. The service I just wrote to restart dropped connections will really help a lot of people with flaky wifi I think.

@sgs did you get the above hook working well for you (and on which DE and terminals)

1 Like

I'm so sorry, I'm afraid not. But I'm sure you know how to do it and that it has to work.
I am on unstable, but with ""-mmin -60" (1hour)" I will test next time.


Thread: table of contents
I'd do the preliminary work if I knew what was relevant. LOL :smiley: :wink: :slight_smile:

My knowledge is unfortunately very limited respectively I have done everything already once, but I just can't remember it anymore :smiley:

1 Like

Forum kindly sponsored by