Создать блокировку с помощью сокета / порта unix

Это не решение imagemagic, но

sips -r 90 *.JPG

поворачивает все изображения, оканчивающиеся на .JPG, на 90 градусов. Это хороший лайнер.

1
28.02.2018, 09:15
1 ответ

Zócalo unix abstracto(man 7 unix):

  • abstract: an abstract socket address is distinguished (from a pathname socket) by the fact that sun_path[0] is a null byte ('\0'). The socket's address in this namespace is given by the additional bytes in sun_path that are covered by the specified length of the address structure. (Null bytes in the name have no special significance.) The name has no connection with filesystem pathnames. [...]

Su principal interés aquí es que este socket no permanece cuando el proceso que lo creó muere. Afortunadamente socatproporciona un método ABSTRACT-LISTENespecíficamente para sockets abstractos (que evita tener que manejar '\0'en parámetros y shell ). Permitiendo así implementar este método python en shell:

#!/bin/sh

socat ABSTRACT-LISTEN:/myownapplock - >/dev/null &
socatpid=$!

sleep 2 # wait for socat to have executed and be listening or have failed
if pgrep -P $$ '^socat$'; then
    locked=yes
else
    locked=no
    echo >&2 'Lock failed.'
    exit 1
fi

my-proc

kill $socatpid # cleanup once my-proc is done

La ​​ejecución del mismo código por segunda vez fallará. Estoy seguro de que el sleep 2se puede mejorar, pero este código sigue siendo carrera -libre (siempre que socat tarde menos de 2 segundos en comenzar ). El socket abstracto se puede ver, por ejemplo, con netstat -xlp|fgrep @/myownapplock.

1
27.01.2020, 23:44

Теги

Похожие вопросы