systemd가 깨어난 후 사용자 스크립트를 실행하는 방법은 무엇입니까?

systemd가 깨어난 후 사용자 스크립트를 실행하는 방법은 무엇입니까?

내 Debian Jessie 데스크톱 상자가 절전 모드에서 깨어날 때(새로운 Shiny systemd를 통해) 내 마우스 설정은 기본값으로 돌아가고 내 사용자 정의 설정은 재설정됩니다.

xinput set-prop 12 'Device Accel Constant Deceleration' 2.5

로그인하면 실행됩니다.

깨우기 시 임의의 사용자 스크립트를 실행하는 방법은 무엇입니까?(사용자가 X 세션의 소유자라고 가정)

내가 아는 한, 다음은 내가 설정에 적용한 유일한 사용자 정의입니다. systemd(예, 임의의 사용자에게는 작동하지 않기 때문에 이것이 완전히 잘못된 것이라는 것을 알고 있지만 아직 수행 방법을 찾지 못했습니다. ...이것은 다소 관련이 있습니다)

또한 현재 X 스크린을 사용하고 있는 사용자로서 깨어나기 전에 임의의 사용자 스크립트를 어떻게 실행합니까?

cat /etc/systemd/system/i3lock.service

#systemctl enable i3lock.service

[Unit]
Description=i3lock
Before=sleep.target

[Service]
User=fommil
Type=forking
Environment=DISPLAY=:0
ExecStart=/usr/bin/i3lock -c 000000

[Install]
WantedBy=sleep.target

답변1

이 답변은 다음을 기반으로 합니다.askubuntu.com/a/661747/394818(또한논평작성자: @sun-bear),askubuntu.com/q/616272/394818그리고superuser.com/a/1269158/585953.

시스템 서비스 사용:

파일 만들기 /etc/systemd/system/my_user_script.service:

[Unit]
Description=Run my_user_script
After=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target


[Service]
ExecStart=/path/to/my_user_script
#User=my_user_name
#Environment=DISPLAY=:0

[Install]
WantedBy=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target

서비스가 특정 유형의 절전 모드에서 깨어난 후에만 실행될 수 있는 경우 일시 중지/최대 절전 모드/하이브리드를 제거합니다. 특정 사용자가 서비스를 실행해야 하는 경우 및 User=행 의 주석 처리를 제거 Environment=하고 관련 사용자 이름을 바꿉니다.

다음 명령을 사용하여 서비스 파일을 설치합니다.

sudo systemctl enable my_user_script

사용자 서비스를 사용하면 작동하지 않습니다.

User=다음을 사용하여 하드코딩된 사용자 이름을 ~/.config/systemd/user/my_user_script.service설정 하지 않으려면

systemctl --user enable my_user_script

그러나 이것은 작동하지 않습니다. @grawity에서 더 자세히 설명합니다.unix.stackexchange.com/a/174837/163108왜 이런 일이 일어나는가:

sleep.target시스템 서비스에만 해당됩니다. 그 이유는 sleep.target자고 있는 동안 자동으로 활성화되는 마법의 대상이 아니기 때문이다. 이건 그냥 평범한 목표야풋옵션시스템은 절전 모드로 전환됩니다. 따라서 "사용자" 인스턴스에는 동등한 인스턴스가 없습니다. (안타깝게도 "사용자" 인스턴스는 현재 시스템 전체 서비스에 의존할 수 없습니다.)

답변2

파일을 만들었습니다 /lib/systemd/system-sleep/(예를 들어 이라고 부를 수 있음 wakeup).

#!/bin/sh
case $1 in
  post)
    /bin/runme
  ;;
esac

그런 다음 실행 권한 비트를 설정했습니다.

# chmod +x /usr/lib/systemd/system-sleep/wakeup

일어났을 때 호출됨 post.

자세한 내용은 다음을 확인하세요.여기.

답변3

시스템 단위 파일을 만들 수도 있습니다.

# /etc/sysstemd/system/awake.service
# ExecStart is executed on wake up from 'sleep' (suspend or hibernate).
[Unit]
WantedBy=sleep.target
After=systemd-suspend.service systemd-hybrid-sleep.service systemd-hibernate.service

[Service]
Type=simple
ExecStart=/bin/bash -c 'echo awake from sleep' >> /tmp/awake.notes'

일반적으로 이는 systemd 서비스를 다시 시작하는 데 사용됩니다.

ExecStart=/bin/systemctl restart fancontrol

답변4

이전의 모든 솔루션은 실제로 출시할 수 있는 방법을 제공하지 않았습니다.사용자각성 후 유닛. 환경 변수를 그곳으로 가져왔으므로 사용자 단위를 사용하고 싶을 수도 있습니다(예: 를 사용하여 dbus-update-activation-environment --all --systemd).

나는 이 문제에 대해 사용자 서비스를 시작하는 시스템 서비스를 생성하는 것과 관련된 추악한 해결책을 찾을 수 있었습니다.

시스템 서비스 생성:/etc/systemd/system/[email protected]

[Unit]
Description=Run user unit of %i after wakeup.
After=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target

[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl --user --machine=%i@ start --wait afterwakeup.service

[Install]
WantedBy=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target

에서 영감을 받다https://unix.stackexchange.com/a/713646/454143.

그리고 사용자 단위 ~/.config/systemd/user/afterwakeup.service:

[Unit]
Description=Runs after wakeup from suspend etc.

[Service]
ExecStart=<do anything you want here, dont forget to import your environment>
Type=oneshot

이제 를 실행하세요 . 예를 들어 .xinitrc( ) systemctl enable afterwakeup@<youruser>.service에서 사용자 환경을 가져오는 것을 잊지 마세요 .dbus-update-activation-environment --all --systemd

관련 정보