0

I'm using i3wm and I want a script to run after I've unlocked my screen. I can get this to work when I manually lock my screen, e.g. using the steps outlined in this post.

But I can't get this to work after my screen is automatically unlocked, e.g. via xautolock. For example,

xautolock -time 5 -locker "blurlock -n && my_script.sh"

doesn't work – the screen locks after 5 minutes, but the script is not run after unlock. In fact, it doesn't seem to be run at all.

Is there some way to get this to work? Maybe using xss-lock or similar?

EDIT

Based on the suggestion by @aviro in the comments, I changed the script to read

#!/usr/bin/bash
echo "Hello" >> $HOME/temp.txt
blurlock -n
echo "Unlocked" >> $HOME/temp.txt

and then ran xautolock -time 1 -locker "my_script.sh &". The screen locked, and the file temp.txt contained both lines from the script. So it is possible to have a command run after unlocking when used in xautolock.

Part of what I want my script to do is to capture an image from a camera via ffmpeg. I changed my script to the following:

#!/usr/bin/bash
blurlock -n 
ffmpeg -f video4linux2 -s vga -i /dev/video-cam $HOME/Pictures/test.jpg 
notify-send -t 30000  'Unlocked'

This works when run manually -- the screen is locked, and after unlocking a notification pops up saying "Unlocked", and an image from the web-cam is written to the file as specified.

But when run via xautolock -locker "my_script.sh", after unlocking, there's no image capture or notification. Since I'm running xautolock from the terminal (and send it to the background with &), I get the following message

[1]  + 581665 suspended (tty output)  xautolock -time 1 -locker "my_script.sh"

Adding > /dev/null 2>&1 to the ffmpeg command doesn't help either.

nonreligious
  • 113
  • 6
  • Instead of using "blurlock -n && my_script.sh" (which I don't know if it would work and what is the exit status of blurlock, which would determine if the next script runs or not), just create a single script. Inside the script do something at the beginning (write something to file to see that it started running), the next line run blurlock, and then at the last line write something to a file at the end (you could also add the exit status of blurlock). Run this script manually first to see that it works. Then use it as the -locker and see if it also works when xautolock runs it. – aviro Feb 06 '24 at 13:40
  • Thanks @aviro; I've tried your test with a script that does echo "hello" >> file.txt; blurlock -n; echo "unlocked" >> file.txt as the -locker, and the file is written to appropriately. FYI I think this is the script for blurlock; it is based on i3lock. – nonreligious Feb 07 '24 at 09:21
  • Based on this, I tested a few more things. I removed the echo commands from the script, and put the command I wanted after blurlock -n; it's actually a line that uses ffmpeg to take a picture using a webcam. The script works fine when run manually, but when supplied to xautolock as the -locker option, nothing happens. I think it might have something to do with stdout=/=stderr messages, but even adding > /dev/null 2>&1 does not fix this. – nonreligious Feb 07 '24 at 09:21
  • Please [edit] your question and add the relevant details. My comments only intended to get your closer to the root cause. – aviro Feb 07 '24 at 09:52
  • Actually, your original question is solved: You can run a script after the lock. Now what happens inside it is entirely different question unrelated to this one. – aviro Feb 07 '24 at 09:53
  • For the last problem, just google this error. First result on google: https://stackoverflow.com/questions/24056102/why-do-i-get-suspended-tty-output-in-one-terminal-but-not-in-others – aviro Feb 07 '24 at 17:20
  • @aviro I had already found that link -- it's not that it's a problem (I don't intend to manually run xautolock from the terminal, it would be launched automatically on login by i3), but I thought it might give a hint as to where the xautolock problem originates: maybe there is some output of ffmpeg to stdout or stderr that breaks the unlock process. – nonreligious Feb 07 '24 at 17:37
  • Try to run my_script.sh & in your shell. I assume you'll see the same problems. Start by checking which of the 3 lines in the script are causing this to isolate the problem and simplify it. Comment out a line at the time until the problem doesn't appear. Then you'll know it's that specific command and you can be more focused. Then you can submit another new more simplified question that people are more likely to answer, such as: "If I run ffmpeg [...] & in the background, it fails with the message..." (depends if it's actually the ffmpeg or something else). – aviro Feb 07 '24 at 18:00

1 Answers1

0

After a lot of experimenting, I'm reasonably sure that using nohup before the ffmpeg command fixes the problem. That is, the above line in my_script.sh should be changed to:

nohup ffmpeg -f video4linux2 -s vga -i /dev/video-cam $HOME/Pictures/test.jpg > /dev/null 2>&1
nonreligious
  • 113
  • 6