I want to run some servers and whatnot on my machine but I don't want it to interfere with what I'm doing... Gaming, research, writing a paper, etc. My thought was to have a script run when the system idles as in when the screen is locked or no users are logged in. The stuff I would be running during idle would be things like a plex server, bitcoin miner or bittorrent. I can start and stop all of these things with something like "service plexd start/stop". Any ideas on how I can initiate these commands when the system goes into and comes out of idle?
Asked
Active
Viewed 1,373 times
1
-
Just brainstorming.. Is cron only time based or can it be activated by other variables? – leszakk Aug 04 '14 at 03:44
-
possible duplicate of [How to run a command or script at screen lock/unlock?](http://askubuntu.com/questions/429716/how-to-run-a-command-or-script-at-screen-lock-unlock) – Barafu Albino Aug 04 '14 at 06:25
-
@leszakk cron is nearly only time based(excepted @reboot), you should use `upstart`. – LittleByBlue Aug 04 '14 at 09:20
-
see [upstart](http://upstart.ubuntu.com/) – LittleByBlue Aug 04 '14 at 09:25
-
Isnt upstart being replaced by systemd? – leszakk Aug 04 '14 at 14:21
-
@Barafu Albino - that thread is only half of what I want to do. I tried it thinking that it might work as a temporary solution but `echo $X` does not return anything for me – leszakk Aug 04 '14 at 18:43
-
seems to be that the only way to tell if the screen is locked is to run something like `gnome-screensaver-command --query | grep -oh "\w*active\w*"` – leszakk Aug 04 '14 at 19:20
-
this is interesting...http://askubuntu.com/questions/505681/unity-how-to-detect-if-the-screen-is-locked – leszakk Aug 05 '14 at 00:10
-
Why not run it all the time and give a low nice value? – muru Aug 05 '14 at 03:34
-
@muru you could with the script ive made but i chose not to because i have bandwidth restrictions – leszakk Aug 05 '14 at 03:39
1 Answers
2
I've created a new script to run a command upon locking and unlocking the screen because the others on the web don't work on 14.04. I set it as a startup program and added it to my sudoers file so that it can manage services on its own.
#!/bin/bash
while sleep 30s ; do
state=$(gnome-screensaver-command --query | grep -o "\w*active\w*" >> /dev/null)
if [[ $state == active ]]
then
./start.sh
else
./stop.sh
fi
done
leszakk
- 552
- 2
- 4
- 20
-
3A couple of suggestions: if `while [ 0 -le 1 ]` is supposed to be an infinite loop, you can simply use `while true`. Better yet, in this case, `while sleep 30s` and skip the sleep before the `done`. – muru Aug 05 '14 at 03:43