4

I'm trying to execute a bash command on screen lock/unlock.

Following tutorials & StackExchange questions, I came up with the following code:

#!/bin/bash while true; do #added to try to solve the issue, but alas it did not dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" | while read sign; do case "$sign" in *"boolean false"*) echo "Screen unlocked";; *"boolean true"*) echo "Screen locked";; esac done done 

I launch the program with the following command:

nohup myprogram.sh & 

Everything works well at start, but after a while (several hours), there is no more echoed output when screen is locked/unlocked.

Checking the output of ps aux | grep mycommand, I have the following result at start:

user <pid1> 0.0 0.0 <number> <number> pts/2 S 13:01 0.00 /bin/bash myprogram.sh user <pid2> 0.0 0.0 <number> <number> pts/2 S 13:01 0.00 /bin/bash myprogram.sh 

After it breaks and does not emit messages anymore, then the ps output only shows one line.

I'm using CentOS 6.5, with Gnome 2.28 (and I unfortunately cannot upgrade to any newer version).


Would you have any insight about what could be happening and/or how to investigate further?


Edit: corrected the while true; then syntax error

3
  • Not having run your code -- but personally I'd introduce a small sleep inside the loop. Commented May 3, 2019 at 10:25
  • 1
    Btw., the outer loop should also be while true; do etc. Commented May 3, 2019 at 10:30
  • Add a line *) echo "`date` Unknown";; to the case statement, and run the command as nohup myprogram.sh </dev/null > $HOME/myprogram.out 2>&1 &. Commented May 3, 2019 at 10:51

2 Answers 2

1

The following script works for me on Linux Mint Cinnamon. Note 'cinnamon' instead of 'gnome'; if you're unsure as to what to use, run echo $DESKTOP_SESSION, which should provide you with the name to use instead of cinnamon; for me:

me@localhost ~] echo $DESKTOP_SESSION cinnamon 

Here's the script:

#!/bin/bash while true; do #added to try to solve the issue, but alas it did not dbus-monitor --session "type='signal',interface='org.cinnamon.ScreenSaver'" | while read sign; do case "$sign" in *[Ff]alse*) echo "Screen unlocked";; *[Tt]rue*) echo "Screen locked";; *) echo "`date` Unknown";; esac sleep 0.250 done done 

Run like so:

nohup ./myprogram </dev/null >| $HOME/myprogram.out 2>&1 & 
Sign up to request clarification or add additional context in comments.

Comments

1
#!/bin/bash while true; do dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" | while read -r sign; do case "$sign" in *"boolean false"*) echo "Screen unlocked";; *"boolean true"*) echo "Screen locked";; esac done done 

i change while true; then to while true; do and add option -r to while read sign; do

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.