perl script intended to reactivate computer so that cron jobs run

I have crontab jobs that take place during the night, but would rather not leave my machine (a Dell Precision T1500, running openSUSE 11.2 64-bit) constantly active. The need is to bump the machine into activity just before a scheduled crontab job.

The earlier history of my enquiry can be found (if needbe) under the 64-bit forum, under title: “scripts in crontab not executed if computer (Dell desktop, openSUSE 11.2) inactive.” In short, with others’ help I have been directed to a perl script (originating on a Ubuntu forum) that becomes active when the machine goes inactive (I think that’s the intent – makes sense anyway) and executes an rtcwake command designed to spur the computer into action at the designated time(s). The script, which is given below, is not working – or to be more specific, when I added this script to the Gnome session manager then rebooted, I got an empty screen right after login. I did CTRL ALT F1 and I received the following message: “prepare_preload warning: Too many forks, PIDs got reused, we’re confused at 41315 splash 4309 fork 4310”.

I am not familiar with perl, and I’m stuck at the line containing the test if(m/^\s+boolean true/){ … what does this mean? Any suggestion as to where I might be going wrong? Thanks, jdw

#!/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");
}
}

Hi
Have a read of this article;
Wake your Linux up from sleep for a cron job

j1d1w1 wrote:
> I am not familiar with perl, and I’m stuck at the line containing the
> test if(m/^\s+boolean true/){ … what does this mean?

It means that the code following the if (system etc) will be executed if
and only if a line read from the filehandle IN starts with one or more
whitespace characters followed by ‘boolean true’.

You haven’t shown where IN is opened and you haven’t shown anywhere that
$cmd is used. If what you’ve shown us is a complete program, it does
nothing.

Thanks Malcolm and djh-novell. Regarding the web reference, the catch is that my machine can go to sleep at any time (my choice), so I need something more than is explained there. The rtcwake is what I’m using, but I need to issue those commands whenever the machine drops off…

djh-novell, yes, sorry, when cutting out comments in my file I accidentally chopped one line (the open) from the script as I had shortened it for the forum… should be:


#!/usr/bin/perl
my $cmd = "dbus-monitor --session \"type='signal',interface='org.gnome.ScreenSaver', member='SessionIdleChanged'\"";
open (IN, "$cmd |");
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");
}
}

Hi
You need to run the dbus-monitor --session command and check the string your looking for is the same in the script.

Thanks Malcolm… response to “dbus-monitor --session” is:

signal sender=org.freedesktop.DBus -> dest=:1.1671 serial=2 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameAcquired
string “:1.1671”
method call sender=:1.1671 -> dest=org.freedesktop.DBus serial=3 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch
string “type=‘method_call’”
method call sender=:1.1671 -> dest=org.freedesktop.DBus serial=4 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch
string “type=‘method_return’”
method call sender=:1.1671 -> dest=org.freedesktop.DBus serial=5 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch
string “type=‘error’”

Referring back to the script shown earlier, I guess I change to

interface=‘org.freedesktop.DBus’

but ought I also to make the substitution: member=‘AddMatch’ ?

Thx, John.

Hi
Might be the case, can you manually create the screensaver or sleep to see what’s output?

Not sure what that means Malcolm, or how to proceed… thx, john

PS. I do know how to force the machine into sleep using rtcwake – but not sure where I should look for this “output”

On Thu, 2011-03-31 at 15:36 +0000, j1d1w1 wrote:
> PS. I do know how to force the machine into sleep using rtcwake – but
> not sure where I should look for this “output”
>
>
Hi
So open a terminal and run the command, then put it to sleep, then wake
it up. There should be output in the terminal showing the sleep and wake
output.


Cheers Malcolm °¿° (Linux Counter #276890)
SUSE Linux Enterprise Desktop 11 (x86_64) Kernel 2.6.32.29-0.3-default
up 15:09, 2 users, load average: 0.00, 0.06, 0.04
GPU GeForce 8600 GTS Silent - Driver Version: 260.19.26

Thanks malcolm – I did a “sudo /usr/sbin/rtcwake -u -m mem -s 20” and the response after 20 sec of sleep was:

rtcwake: wakeup from “mem” using /dev/rtc0 at Thu Mar 31 11:11:55 2011

Hi
And what about the output from going to sleep?

So, what is the typical scenario for your system going into standby? What I think needs to be done, is monitor the dbus output and carry on as normal, then review the dbus monitor output so you can select a ‘trigger’ for the script to activate.

The session monitor does not provide any information… I can have the machine slip into sleep for a few seconds and emerge, but nothing is logged in dbus-monitor --session. Anyhow I’ll leave the dbus-monitor active and watch out for anything that might pertain. Thanks again, John.

On Thu, 2011-03-31 at 18:36 +0000, j1d1w1 wrote:
> The session monitor does not provide any information… I can have the
> machine slip into sleep for a few seconds and emerge, but nothing is
> logged in dbus-monitor --session. Anyhow I’ll leave the dbus-monitor
> active and watch out for anything that might pertain. Thanks again,
> John.
>
>
Hi
Check down in /var/log as well there maybe something to monitor in one
of the log files.


Cheers Malcolm °¿° (Linux Counter #276890)
SUSE Linux Enterprise Desktop 11 (x86_64) Kernel 2.6.32.29-0.3-default
up 17:42, 2 users, load average: 0.03, 0.09, 0.03
GPU GeForce 8600 GTS Silent - Driver Version: 260.19.26

Thanks Malcolm – had a quick look at files in /var/log without anything jumping out as helpful. dbus-monitor --session --profile has these entries that are I think related to the screensaver coming on or off… John.

sig 1301604573 386969 25 /org/gnome/ScreenSaver org.gnome.ScreenSaver ActiveChanged
sig 1301604573 387320 84 /org/gnome/SessionManager/Presence org.gnome.SessionManager.Presence StatusChanged

Hello Malcolm, I have some info that might suggest a “trigger” to activate my perl script. I went to a departmental seminar at about 11:55 MDT, and my machine went to sleep some time later (apparently at 12:35:19, which probably makes sense: my settings are that display goes inactive after 10 minutes, and the computer “goes to sleep when inactive for 30 minutes”). I returned at about 13:00 MDT and when I issued a dbus-monitor --session I got some clues. The monitor registered events at Unix time 1301682919 (12:35:19) then nothing until 1301684370 (12:59:30) which coincides nicely with when I returned from the seminar. Do you see anything here that suggests how I might amend my perl script? Thanks, John.

The messages from dbus-monitor --session

mc 1301682919 688346 61 :1.22 / org.gnome.ScreenSaver Throttle
mr 1301682919 688654 50 61 :1.22
mc 1301682919 689033 62 :1.22 / org.gnome.ScreenSaver Lock
mc 1301682919 689057 63 :1.22 / org.gnome.ScreenSaver GetActive
mr 1301682919 689317 51 63 :1.22
mc 1301684370 621915 64 :1.22 / org.gnome.ScreenSaver SimulateUserActivity
mc 1301684370 621934 65 :1.22 / org.gnome.ScreenSaver UnThrottle
mr 1301684370 630237 52 65 :1.22
mc 1301684370 653781 66 :1.22 /org/freedesktop/Notifications org.freedesktop.Notifications Notify
mr 1301684370 714976 16 66 :1.22
mc 1301684370 717780 67 :1.22 / org.gnome.ScreenSaver UnThrottle
mr 1301684370 717943 53 67 :1.22
sig 1301684370 797621 90 /org/freedesktop/DBus org.freedesktop.DBus NameOwnerChanged
mc 1301684370 797641 1 :1.313 /org/freedesktop/DBus org.freedesktop.DBus Hello
mc 1301684370 797654 2 :1.313 /org/gnome/GConf org.gnome.GConf GetIOR
mr 1301684370 797801 193 2 :1.313
sig 1301684370 806301 91 /org/freedesktop/DBus org.freedesktop.DBus NameOwnerChanged
mc 1301684370 806323 1 :1.314 /org/freedesktop/DBus org.freedesktop.DBus Hello
mc 1301684370 807047 2 :1.314 /org/gnome/GConf org.gnome.GConf GetIOR
mr 1301684370 807061 194 2 :1.314
sig 1301684370 810480 92 /org/freedesktop/DBus org.freedesktop.DBus NameOwnerChanged

and then 7 seconds later (after I had entered my password?)

sig 1301684377 467383 93 /org/freedesktop/DBus org.freedesktop.DBus NameOwnerChanged

Hi
OK, lets assume the following would create the trigger;


my $cmd = "dbus-monitor --session \"type='signal',interface='org.gnome.ScreenSaver', member='GetActive'\"";

Then you would really need to look at a form of cancel, so normal activities don’t set it running, if you don’t have a cancel it will start multiple instances, else add a check by creating a PID file, and see if it exists and act accordingly.

Hi Malcolm,

Not sure what you mean by having a cancel – is that a further instruction in the perl script? And this would be, multiple instances of the rtcwake command? If so, that might not matter (unless it was zillions of them)? Thanks, John.

Hi
Yes a further check to only have one instance of rtcwake running, it’s just a thought. See how it goes, then can adjust if necessary.

Thanks Malcolm: I made that change in my script (my $cmd = “dbus-monitor --session “type=‘signal’,interface=‘org.gnome.ScreenSaver’, member=‘GetActive’””;)… as earlier, when I restart the machine with this as a start-up job as soon as I have logged in I get the default blank green screen and nothing else… so I did a CRTL ALT F1 and renamed the script. John.