I have various scripts intended to retrieve files from the web regularly during day and night. These scripts work fine, and are executed by my crontab table – but only provided the machine is not in sleep mode. I don’t want the computer in active mode 24/7 just so it can do a ten second job four times a day. Is there a solution to this other than to disable the sleep mode under Power Management? Thanks, John.
There is script in ubuntu forums that claimed to be executed when system goes into idle. You can find it in here: HOWTO: Run a Program or Windows Screen Saver when System is Idle - Ubuntu Forums
It seems like it can be modified for your needs. As a command, you what google01103 said. So this is what you will achive,
When your computer thinks its idle, script executed, telling rtcwake sleep until specified time. When specified time comes, your computer should get up and execute your cron job.
About specified time, you can write a simple bash file to calculate difference between now and lets say 6:00 am, so that your rtcwake will get your computer up and running exactly when you need it. After waking up, it will stay awake until next screensaver jumps in.
Thanks Google01103 and Yasar11732. I’ve made some progress with using rtcwake (and using date to supply it with the wanted time to awaken the machine). John.
j1d1w1 you should post your results and script(s) as it’s a bit obscure and others might at some point have an interest
Thanks for the suggestion google01103, I’ll need first to sort out how to do that (there is nothing too fancy about my crontab file though). Would I just literally copy code into this window?
I’ve made some progress following the url supplied by Yasar11732. I have figured out how to use rtcwake and use date to supply it with the needed time. I’ve written (or largely, copied) a perl script that is supposed to set up these commands so that each time the machine goes inactive the needed rtcwake commands are issued. I hoped I might be able to make this active by adding a line to /etc/profile.local, viz. perl /usr/local/sbin/jdw.pl . However when I source /etc/profile.local with that line inserted, I get a syntax error. I have no experience with perl, so that’s a place to start, but perhaps there is something more profoundly wrong. I can’t follow that link too literally as it applies to Ubuntu. Thx, jdw
Maybe you can share your scripts here, so that people with perl experience can help you. The thing you will get, at least, more eyes on your codes that migt be reduce the occuring chances of obvious mistakes.
Thanks yasar11732. Below I have copied in the perl script, more or less as copied. Its intent is to make sure the machine is active in time to respect instrcutions in my crontab, which I also copied in below. Thx, jdw
#!/usr/bin/perl
# Latest modification by JDW 21 Mar. 2011 (created 20 Mar. 2011)
# this script copied from [HOWTO: Run a Program or Windows Screen Saver when System is Idle - Ubuntu Forums](http://ubuntuforums.org/showthread.php?t=1029704)
# in context of need to find a way to force the Dell machine to execute
# wget instructions (emerging from sleep mode).
# need to be superuser to create/edit a file in this directory /usr/local/sbin
# The idea is as follows: when the computer goes into sleep mode this
# script is executed, and orders the machine to sleep until a time I specify
# These wake times need to be coordinated with the crontab file, and
# the machine should be woken a minute or so before the timing for the wget
#
my $cmd = "dbus-monitor --session \"type='signal',interface='org.gnome.ScreenSaver', member='SessionIdleChanged'\"";
open (IN, "$cmd |");
while (<IN>) {
if (m/^\s+boolean true/) {
#when Ubuntu idles these commands will run
system("/usr/sbin/rtcwake -m mem -t $(date --date=01:00:00 +%s")
system("/usr/sbin/rtcwake -m mem -t $(date --date=05:00:00 +%s")
system("/usr/sbin/rtcwake -m mem -t $(date --date=10:00:00 +%s")
system("/usr/sbin/rtcwake -m mem -t $(date --date=15:00:00 +%s")
#system("COMMAND"); #you can remove or add more commands by
# adding another system("COMMAND"); line
} elsif (m/^\s+boolean false/) {
#when Ubuntu returns from idle these commands will run, delete these two system lines if not wanted/needed
#system("COMMAND");
#system("COMMAND");
system("echo Issued a wake-up reminder to ensure scheduled crontab jobs run");
}
}
Here’s part of my crontab file…
# Latest change 20 Mar. 2011
# need to synchronize the wget execution times with my pearl script in /usr/local/sbin (must be su to edit it) which ensures machine is awake in time to see these. Thus I always execute the wget 5 minutes after the hour
# copy of the crontab file on Tornado which runs Red Hat Fedora
# copied 17 Mar. 2011 to transfer to Dell Linux machine
#14 10 * * * /usr/sbin/rtcwake -h > ~/junk
#MIN HR DY MNTH DYofWK CMD
05 01 * * * source ~/weather/downloadscripts/getcmc00Zcharts.sh
05 05 * * * source ~/weather/downloadscripts/getcmc06Zcharts.sh
05 10 * * * source ~/weather/downloadscripts/getcmc12Zcharts.sh
05 15 * * * source ~/weather/downloadscripts/getcmc18Zcharts.sh
And here is script getcmc06Zcharts.sh
# latest modification 17 Mar./2011
#
#jdw Mar 15,2005 ... retrieving weather analysis files ... sourced at 0900 LST daily
#
cd ~/weather/2011/cmc/sfc
wget http://www.weatheroffice.gc.ca/data/analysis/jac06_100.gif
/usr/bin/convert jac06_100.gif sfc_$(date +%Y%m%d)@06Z.png
rm jac06_100.gif
#
cd ~/weather/downloadscripts
system("/usr/sbin/rtcwake -m mem -t $(date --date=01:00:00 +%s")
Change all of these lines to
system("/usr/sbin/rtcwake -m mem -t $(date --date=01:00:00 +%s)")
You are forgetting to close the brackets you opened for date command.
Thanks yasar11732… done that (and added semi-colon at end of each of those lines). Now when I source /etc/profile.local (which contains the line perl -w /usr/local/sbin/jdw.pl ) there is no error message, but the terminal goes into some weird mode. If the crontab instructions are executed tonight I’ll know its working!
How did it go? Did it work?
No! I wondered if the mode the terminal entered (I don’t know how to describe it: no prompt, one could type, hit return, but nothing happened) signified that process was waiting. But in any case, the crontab jobs did not run.
If you are using gnome desktop, you can try to add it to session manager.
Go to system, personel, startup. Click, add and find your perl script. I think it will work.
I’ve done that yasar11732 and shall let you know if this works.
Ooops! I did a re-start. As soon as I log in, empty screen. This I suppose must be due to the execution of that script… recall when executed in a terminal it was starting a process that did nothing and never ended. I did CTRL ALT F1 and I see the following message: “prepare_preload warning: Too many forks, PIDs got reused, we’re confused at 41315 splash 4309 fork 4310”. As a temporary expedient I logged in, moved the perl script to another file, and did a halt.
Can anyone please explain for me this test in the perl script below (please see messages above for the origin of this query): if (m/^\s+boolean true/)
#!/usr/bin/perl
my $cmd = "dbus-monitor --session \"type='signal',interface='org.gnome.ScreenSaver', member='SessionIdleChanged'\"";
while (<IN>) {
if (m/^\s+boolean true/) {
system("/usr/sbin/rtcwake -m mem -t $(date --date=01:00:00 +%s)");
system("/usr/sbin/rtcwake -m mem -t $(date --date=05:00:00 +%s)");
system("/usr/sbin/rtcwake -m mem -t $(date --date=10:00:00 +%s)");
system("/usr/sbin/rtcwake -m mem -t $(date --date=15:00:00 +%s)");
} elsif (m/^\s+boolean false/) {
system("echo Issued a wake-up reminder to ensure scheduled crontab jobs run");
}
}