Crontab Monitor On-Off setup
I decided to make use of the display i have mounted with my VMserver. I have set up a photo screen saver to rotate images, However i did not want it to run all day and all night i wanted it to have a schedule of when to come on and off. Let me start by pointing out some hiccup's i found in doing this.
originally i was going to use xset dpms force off | on but what i realised is when i would turn it back on the screen would still stay black until the mouse was moved not what i wanted
So i decided to use vbtool dpms off. this tool is nice when i turn the monitor off i can still RDC into the box and the monitor will stay off this was perfect. I did notice that the screen saver was still going off and using resources even though the display is not on we will turn this off as well.
Another thing i found is the i did not have the variable DISPLAY=:0.0 in my /etc/enviroment file which was producing the error: xset unable to open display""
Ok lets start by creating a batch file /home/username/cronscripts/monitorcontrol.sh
This script will have 2 switches.
- On will turn the Screensaver on echo screensaver on then turn the monitor on and echo monitor on then echo done
- Off will turn the Screensaver off then echo screensaver off then turn the monitor off and echo monitor off then echo done
#!/bin/bash export DISPLAY=:0.0 if [ $# -eq 0 ]; then echo usage: $(basename $0) "on|off|status" exit 1 fi if [ $1 = "off" ]; then echo -en "Turning Screensaver off..." /usr/bin/xset s off echo -en "Turning monitor off..." /usr/sbin/vbetool dpms off echo -en "done. Check:" /usr/bin/xset -q|grep "Monitor is" elif [ $1 = "on" ]; then echo -en "Turning Screensaver on..." /usr/bin/xset s on echo -en "Turning monitor on..." /usr/sbin/vbetool dpms on echo -en "done. Check:" /usr/bin/xset -q|grep "Monitor is" elif [ $1 = "status" ]; then /usr/sbin/xset -q|sed -ne 's/^[ ]*Monitor is //p' else echo usage: $(basename $0) "on|off|status" fi
Ok awesome now that we have the config in place and in order lets move on to setting up a cron Some basic Crontab syntax and commands for you
Commands
crontab -eEdit your crontab file, or create one if it doesn’t already exist. crontab -lDisplay your crontab file. crontab -rRemove your crontab file. crontab -vDisplay the last time you edited your crontab file. (This option is only available on a few systems.)
Syntax
* *** *command to be executed - -- -- | || || | || |+----- day of week (0 - 6) (Sunday=0) | || +------- month (1 - 12) | |+--------- day of month (1 - 31) | +----------- hour (0 - 23) +------------- min (0 - 59)
Ok here is my cron jobs i will explain after
55 23 * * * sh /home/thomas/cronscripts/monitorcontrol.sh off /home/thomas/cronscripts/log 2>&1
55 08 * * * sh /home/thomas/cronscripts/monitorcontrol.sh on /home/thomas/cronscripts/log 2>&1
The fist task will run at 11:55pm it will run the config we created with the option Off and will output any errors to a log file The second task will run at 8:55am it will run the config we created with the option On and will output any errors to a log file
to break down further the sh command will execute the script. Note: we do need to give the full absolute path to our config file net it will create a log file called log in the same directory as the script we made.
We have one more thing to check on my machine i was having an issue because i was missing a variable in th /etc/environment. You should check and make sure that variable is in place (I have called it in the script but it's still good to have in place) their error was: xset unable to open display""
In terminal type:
sudo nano /etc/environment
check to see if you have a line with the following:
DISPLAY=:0.0
If not add it to the file and save.
awesome you are good to go now. Please let me know what you think and if you have any questions feel free to ask...
Credit: Sparda at hak5 lent a hand and pointed out the need for absolute paths.
Thanks Sparda