0

It seems that when my screen is locked for some period of time, my S.gpg-agent.ssh disappears, and so in order to continue using my key I have to re-initialise it.

Obviously, this is a relatively frequent occurrence, so I've written a function for my shell to kill gpg-agent, restart it, and reset the appropriate environment variables.


This may be a bit of an 'X-Y problem', X being above this line, but I think Y below is more generally useful to know anyway.


How can I automatically run a command when an extant file no longer exists?

The best I've come up with is:

nohup echo "$file" | entr $command &

at login. But entr runs a command when files change, not just deletion, so it's not clear to me how that will behave with a socket.

OJFord
  • 10,522
  • 8
  • 64
  • 98
  • Why not use [`cron`](https://en.wikipedia.org/wiki/Cron) daemon? – Yasen Sep 02 '19 at 13:11
  • @Yasen because I'm not waiting for a time, I'm waiting for a file to disappear? And I don't want to poll it - I'll either be polling way too frequently, or I'll try to use it between polls and have to fix it manually anyway. – OJFord Sep 02 '19 at 13:15

2 Answers2

0

According to your comment, cron daemon does not fit.

Watch socket file deletion

Try auditd

# auditctl -w /var/run/<your_socket_file> -p wa 
$ tail -f /var/log/audit/audit.log | grep 'nametype=DELETE'

Howto run a script if event occurred

If you want to run a script on socketile deletion, you can use while loop, e.g.:

tail -Fn0 /var/log/audit/audit.log | grep 'name=<your_socket_file>' | grep 'nametype=DELETE' \
while IFS= read -r line; do
  # your script here
done

thx to Tom Klino and his answer

Yasen
  • 4,241
  • 1
  • 16
  • 25
0

You don't mention the OS you're using, but if it's linux, you can use inotifywait from the inotify-tools package:

#!/bin/sh

while inotifywait -qq -e delete_self /path/to/S.gpg-agent.ssh; do
    echo "Socket was deleted!"
    # Recreate it.
done
Shawn
  • 47,241
  • 3
  • 26
  • 60
  • 1
    Thanks, I had dismissed it because I hoped for something cross-platform (Linux & macOS) but looking into it now seems `fswatch` is similar and available on both: https://stackoverflow.com/questions/1515730/is-there-a-command-like-watch-or-inotifywait-on-the-mac – OJFord Sep 03 '19 at 08:16