You are not logged in.

#1 2018-07-12 08:03:52

brianb
Member
From: Montreal, QC
Registered: 2012-02-17
Posts: 81
Website

pacman hook: execute two commands instead of one

(Go to Question if you don't feel like reading the background.)

Background

I have two scripts, checkup-official and checkup-aur, which check for the number of official and number of AUR packages that can be upgraded (e.g. 5 and 2), and then redirect those two numbers to /tmp/checkup-official and /tmp/checkup-aur, respectively. I have a systemd timer that runs these commands every 2 hours. My status bar then polls the two files in /tmp every second and prints the two numbers, like so: 5(2).

For a while, I used to run the following command to upgrade everything (I'm using aurutils at the moment):

aursync -uc && pacman -Syu && checkup-official && checkup-aur

The last two commands here would reset the two counter numbers in /tmp, so that my status bar would go back to 0(0).

Then I discovered pacman hooks. I thought that I could turn the "checkup-official && checkup-aur" part of my usual command into a pacman hook, so I created /etc/pacman.d/hooks/checkup.hook:

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

[Action]
Description = Resetting counter for official and AUR packages...
When = PostTransaction
Exec = /home/brian/repos/scripts/checkup-official && /home/brian/repos/scripts/checkup-aur

Problem

The hook only actually runs checkup-official; it seems to ignore everything starting from &&. I also tried changing && to ; and got an error:

:: Running post-transaction hooks...
(1/4) Resetting counter for official and AUR packages...
call to execv failed (No such file or directory)
error: command failed to execute correctly

I also tried including two Exec commands, but then pacman ends up discarding one of them (and telling me so) and using only the other.

Question

How can I make Exec in a pacman hook execute two commands?

Notes

As I mentioned, I have a single systemd service/timer that runs both commands, and I do this by having two ExecStart lines, one for each script. So, pacman hooks seem to differ from systemd services insofar as you can have multiple ExecStart's in a service file, but only one Exec in a hook.

I could of course create two different hooks, one for each script (and maybe that's even better, from a semantic/modular viewpoint), or I could write a very short third script, checkup, that runs checkup-official && checkup-aur, and then Exec that -- but I'm curious to know if there's a more direct way to execute two commands.

Last edited by brianb (2018-07-12 08:10:43)

Offline

#2 2018-07-12 08:33:00

ooo
Member
Registered: 2013-04-10
Posts: 1,607

Re: pacman hook: execute two commands instead of one

Did you try

Exec = /bin/sh -c 'checkup-official && checkup-aur'

Offline

#3 2018-07-12 12:32:38

amish
Member
Registered: 2014-05-10
Posts: 367

Re: pacman hook: execute two commands instead of one

I had almost created a "feature request" for the exact same subject today, but then withheld the request.

I knew about "sh -c" but I wanted systemd.unit type behaviour.

i.e. Repeatable "Exec" lines

Honor AbortOnFail but ignore the failure if "Exec" command begins with a "-" minus sign.

Last edited by amish (2018-07-12 12:33:14)


Forum signature: I discuss. I put my thoughts strongly. But I definitely respect all developers and time they put in.

Offline

#4 2018-07-12 13:16:49

Steef435
Member
Registered: 2013-08-29
Posts: 535
Website

Re: pacman hook: execute two commands instead of one

sh -c "abortonfail && (dontabort || true)" ?

Last edited by Steef435 (2018-07-12 13:17:12)

Offline

#5 2018-07-12 14:21:19

amish
Member
Registered: 2014-05-10
Posts: 367

Re: pacman hook: execute two commands instead of one

Yes I did it a bit differently:

sh -ce "abortonfail; dontabort || true"

But this creates an additional shell. You need to know shell scripting as well as its not easy to read.

Exec=abortonfail
Exec=-dontabort

is much easy to understand especially when you have 3-4 commands to run that too with some parameters.

Ofcourse opinions differ from person to person.

Last edited by amish (2018-07-12 14:35:27)


Forum signature: I discuss. I put my thoughts strongly. But I definitely respect all developers and time they put in.

Offline

#6 2018-07-12 14:30:14

eschwartz
Trusted User/Bug Wrangler
Registered: 2014-08-08
Posts: 2,552

Re: pacman hook: execute two commands instead of one

brianb wrote:
[Trigger]
Operation = Upgrade
Type = Package
Target = *

[Action]
Description = Resetting counter for official and AUR packages...
When = PostTransaction
Exec = /home/brian/repos/scripts/checkup-official && /home/brian/repos/scripts/checkup-aur

Problem

The hook only actually runs checkup-official; it seems to ignore everything starting from &&. I also tried changing && to ; and got an error:

:: Running post-transaction hooks...
(1/4) Resetting counter for official and AUR packages...
call to execv failed (No such file or directory)
error: command failed to execute correctly

I also tried including two Exec commands, but then pacman ends up discarding one of them (and telling me so) and using only the other.

Question

How can I make Exec in a pacman hook execute two commands?

As you've observed, the Exec line does not run in a bash shell, so you cannot use bash syntax.

The alpm-hooks(5) manpage has this to say:

"Command to run. Command arguments are split on whitespace. Values containing whitespace should be enclosed in quotes."

This is the complete entirety of supported syntax.

amish wrote:

I had almost created a "feature request" for the exact same subject today, but then withheld the request.

I knew about "sh -c" but I wanted systemd.unit type behaviour.

i.e. Repeatable "Exec" lines

Honor AbortOnFail but ignore the failure if "Exec" command begins with a "-" minus sign.

Given we explicitly log that it's repeated, tell you not to do it, and then verbosely ignore it?

I don't think systemd.unit type behavior is a target feature.


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#7 2018-07-12 14:40:02

amish
Member
Registered: 2014-05-10
Posts: 367

Re: pacman hook: execute two commands instead of one

Eschwartz wrote:

Given we explicitly log that it's repeated, tell you not to do it, and then verbosely ignore it?

Thats why its called a "feature request". Allow to do something thats not an existing behavior.

Eschwartz wrote:

I don't think systemd.unit type behavior is a target feature.

Yes thats why I eventually didnt file feature request. smile


Forum signature: I discuss. I put my thoughts strongly. But I definitely respect all developers and time they put in.

Offline

#8 2018-07-12 16:56:45

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 20,466
Website

Re: pacman hook: execute two commands instead of one

Ok, if I can state what should be the blindingly obvious here, rather than feature requests to change pacman hooks, or bloated ugly shell syntax, there is a much much easier solution: these are user-created scripts.  Why use two in the first place, just put all the work in one script, and specify the path to that one script in the Exec line.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#9 2018-07-12 17:07:34

eschwartz
Trusted User/Bug Wrangler
Registered: 2014-08-08
Posts: 2,552

Re: pacman hook: execute two commands instead of one

Trilby wrote:

Ok, if I can state what should be the blindingly obvious here, rather than feature requests to change pacman hooks, or bloated ugly shell syntax, there is a much much easier solution: these are user-created scripts.  Why use two in the first place, just put all the work in one script, and specify the path to that one script in the Exec line.

Well, as the OP said in the first post...

I could of course create two different hooks, one for each script (and maybe that's even better, from a semantic/modular viewpoint), or I could write a very short third script, checkup, that runs checkup-official && checkup-aur, and then Exec that -- but I'm curious to know if there's a more direct way to execute two commands.

But depending on how simple the two commands are, shell syntax for /bin/sh -c '...' might not be bloated and ugly at all...

$ grep /bin/sh /usr/share/libalpm/hooks/*
/usr/share/libalpm/hooks/ghc-register.hook:Exec = /bin/sh -c 'while read -r f; do /bin/sh "/$f" >>/tmp/haskell-register.log 2>&1 ; done'
/usr/share/libalpm/hooks/ghc-unregister.hook:Exec = /bin/sh -c 'while read -r f; do /bin/sh "/$f" >>/tmp/haskell-register.log 2>&1 ; done'
/usr/share/libalpm/hooks/gvfsd.hook:Exec = /bin/sh -c 'killall -q -s USR1 gvfsd || true'
/usr/share/libalpm/hooks/texinfo-install.hook:Exec = /bin/sh -c 'while read -r f; do install-info "$f" /usr/share/info/dir 2> /dev/null; done'
/usr/share/libalpm/hooks/texinfo-remove.hook:Exec = /bin/sh -c 'while read -r f; do install-info --delete "$f" /usr/share/info/dir 2> /dev/null; done'

We use both in the repos. It depends on the situation.


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#10 2018-07-12 17:14:31

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 20,466
Website

Re: pacman hook: execute two commands instead of one

It is bloated and ugly when it serves no purpose.  A shell to run a while loop (as in most of your examples) serves a purpose.  But a shell just to launch a couple other subshells should never be needed.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#11 2018-07-12 17:19:11

eschwartz
Trusted User/Bug Wrangler
Registered: 2014-08-08
Posts: 2,552

Re: pacman hook: execute two commands instead of one

Hmm, fair point especially as if the script truly needs to be able to report only one, it could do so via a parameter instead of a second script. I lost track of the fact that these are already ad-hoc custom scripts...

Last edited by eschwartz (2018-07-12 17:19:55)


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#12 2018-07-12 20:08:12

brianb
Member
From: Montreal, QC
Registered: 2012-02-17
Posts: 81
Website

Re: pacman hook: execute two commands instead of one

Thanks for the replies, everyone: /bin/sh -c '...' is precisely what I was looking for. (And I should have remembered it, too, since I use precisely that for complex keybindings in my WM configurations.)

And yes, while it's true that in this specific case, I could have and perhaps should have written a single script, the question was really more of a general curiosity.

Last edited by brianb (2018-07-12 20:09:53)

Offline

Board footer

Powered by FluxBB