Friday, April 26, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 1725  / 1 Year ago, mon, november 14, 2022, 7:11:28

I wish to write a bash script that executes on start up Ubuntu 13.04 and starts motion whenever the screen is locked. Being a novice at bash scripting, I wasn't able to understand the various help sources available on the web and thereby write a script on my own. Anyways, I know that the following command is used to check if the system is locked:



gnome-screensaver-command -q | grep "is active"


But, if I use this command, I will have to periodically (say every 5-10 seconds) check the lock status. Is there a better alternative? Could someone provide me with a skeleton of the script?



So I could write the following script which starts the webcam once I lock the screen but does not stop it once sign back in. Any suggestions?



#!/bin/bash

while :
do
sleep 2
if (gnome-screensaver-command -q | grep "is active");
then
motion 2> ~/.motion/log
elif (gnome-screensaver-command -q | grep "is inactive");
then
/etc/init.d/motion stop 1> /dev/null
fi
done

More From » bash

 Answers
0

I don't think that you can find a simple better alternative at gnome-screensaver-command -q command, but I found a solution to make your script to function as probably you expect:



#!/bin/bash

is_active=0

while :
do
sleep 2
if (gnome-screensaver-command -q | grep "is active");
then
if [ "$is_active" -eq "0" ];
then
is_active=1
motion 2> ~/.motion/log &
fi
elif (gnome-screensaver-command -q | grep "is inactive");
then
if [ "$is_active" -eq "1" ];
then
is_active=0
/etc/init.d/motion stop 1> /dev/null
fi
fi
done


Some explanation:




  • motion 2> ~/.motion/log command followed by & will start the motion process to run in terminal; without &, when the execution of the script reach to that line, this will remain hanged/blocked there.

  • you don't need to run at every 2 seconds motion 2> ~/.motion/log & or /etc/init.d/motion stop 1> /dev/null command, but only when the state of the screensaver is changing; for this reason other changes from the script.


[#30007] Tuesday, November 15, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nuehan

Total Points: 253
Total Questions: 109
Total Answers: 120

Location: Singapore
Member since Sat, Jul 25, 2020
4 Years ago
;