Page 1 of 1

Cronjob fail

Posted: Mon Jan 05, 2015 3:00 pm
by Yiannis
Hi all and Happy New Year,

I am new to Raspberry and Linux so please don't mind silly questions or if this is not the right place to post. I have a Raspberry Pi B+ running Rasbian.

I am trying to create a project whereby when I switch on the Raspberry an .odp presentation starts in presentation mode running in loops (i.e. when it reaches the last slide it starts over). So far so good.

The problem is that I want it to stop after 8 hours....that is, terminate libreoffice using wmctrl -c and then halting the system sudo shutdown -h now.

So I first created a crontab job (i.e. crontab -e) and I added the line:

Code: Select all

*/2 * * * * wmctrl -c libreoffice
. This is every 2 mins for testing purposes. The above line works in terminal but not as a cronjob.
I then created a a script which contained the following lines

Code: Select all

#! bin/bash
wmctrl -c libreoffice
made it executable, placed it in home/pi and changed the crontab

Code: Select all

*/2 * * * * home/pi/killgracefully.sh
. This didn't work either.

I tried other solutions as found at http://stackoverflow.com/questions/1616 ... ind-wmctrl, http://stackoverflow.com/questions/2229 ... b-will-use and http://askubuntu.com/questions/29239/wh ... sh-profile.

Nothing seems to work. Any ideas/suggestions will be very much appreciated.
Extra info: the Pi will not be connected to the network and therefore I will never have correct time hence the every 8 hours cronjob.
Thanks
I also

Re: Cronjob fail

Posted: Mon Jan 05, 2015 3:59 pm
by DougieLawson
You need to use a full path starting with a slash in crontab.

So something.sh in your home directory would go in as

Code: Select all

*/2 * * * * /home/pi/something.sh
Or /usr/bin/foobarprogram would go in as

Code: Select all

*/30 9-19 * * * /usr/bin/foobarprogram -parameter1 --input_file=/home/pi/file.to.read.goes.here -etc

Re: Cronjob fail

Posted: Mon Jan 05, 2015 4:09 pm
by Yiannis
Many thanks for the response.
DougieLawson wrote:You need to use a full path starting with a slash in crontab.

So something.sh in your home directory would go in as

Code: Select all

*/2 * * * * /home/pi/something.sh
I may be mistaken but I think this is the same as the last bit of code I posted above (I forgot to add the backslash but it is in my cronjob)?

Re: Cronjob fail

Posted: Mon Jan 05, 2015 4:13 pm
by DougieLawson
Yiannis wrote:

Code: Select all

*/2 * * * * home/pi/killgracefully.sh
DougieLawson wrote:

Code: Select all

*/2 * * * * /home/pi/something.sh
Spot the difference !!!
Hint: there's a leading slash [/] in one of those examples that's missing in the other.

Re: Cronjob fail

Posted: Mon Jan 05, 2015 4:18 pm
by Yiannis
Seriously, apart from the backslash before home (home Vs /home) I cannot see any difference - and as I said earlier the backslash is included in my actual cronjob just missed it in my post.

Re: Cronjob fail

Posted: Mon Jan 05, 2015 5:36 pm
by DougieLawson
That's not a backslash [\] it's a (forward) slash [/].

Do you get an email from cron when it fails? Do you get anything logged to /var/log/syslog when it fails?

Re: Cronjob fail

Posted: Mon Jan 05, 2015 6:35 pm
by Yiannis
Hi,

Apologies for the silly mistake...yes I meant "/".

I am afraid I don't know where to go to read these mails.
I have left the unit back at work...tomorrow morning I will post the contents of /var/log/syslog.

Again many thanks for the support.

Re: Cronjob fail

Posted: Mon Jan 05, 2015 7:20 pm
by DirkS

Code: Select all

#! bin/bash
wmctrl -c libreoffice
Shebang is also missing forward slash

Code: Select all

#!/bin/bash
wmctrl -c libreoffice

Re: Cronjob fail

Posted: Tue Jan 06, 2015 9:36 am
by Yiannis
Hi DirkS,

Again I apologise for the omission of forward slash before "bin" but it is included in my original script (since the Pi is not connected to the net I had to write manually my little scripts here hence the little omissions which I know do not help to the discussion).

The contents of the syslog are as follows:

Code: Select all

Dec 26 00:18:01 raspberrypi /USR/SBIN/CRON[2274]: (pi) CMD (/home/pi/killgracefully.sh)
Dec 26 00:18:01 raspberrypi /USR/SBIN/CRON[2273]: (CRON) info (No MTA installed, discarding output)
Dec 26 00:20:01 raspberrypi /USR/SBIN/CRON[2278]: (pi) CMD (/home/pi/killgracefully.sh)
Dec 26 00:20:01 raspberrypi /USR/SBIN/CRON[2277]: (CRON) info (No MTA installed, discarding output)
Dec 26 00:20:01 raspberrypi /USR/SBIN/CRON[2278]: (pi) CMD (/home/pi/killgracefully.sh)
Dec 26 00:20:01 raspberrypi /USR/SBIN/CRON[2277]: (CRON) info (No MTA installed, discarding output
Any idea?
Thanks

Re: Cronjob fail

Posted: Tue Jan 06, 2015 9:39 am
by DougieLawson
sudo apt-get install postfix

That gets you a mail transfer agent (MTA) and you'll get email from cron with details of what's run or what's gone wrong. Any echo or printf in your shell script comes back as a line in the resulting email.

Re: Cronjob fail

Posted: Tue Jan 06, 2015 9:48 am
by Yiannis
Ok, I will need to take the Pi back home tonight to install it and I will get back to you tomorrow with updates.

Many thanks!

Re: Cronjob fail

Posted: Tue Jan 06, 2015 9:59 am
by mike_p
If you are going to shutdown after terminating libreoffice, why bother terminate libreoffice individually. Why not just use the cronjob to shutdown?

As far as I can tell your use of wmctrl in a cronjob is the problem. The cron environment doesn't know about the x window displays. If you definitely want to end libreoffice before calling shutdown you could also try using pkill instead of wmctrl in the cronjob. pkill doesn't need to know about the windows and can take the process name as a parameter.

Re: Cronjob fail

Posted: Tue Jan 06, 2015 10:43 am
by Yiannis
Hi mike_p,

Thanks for your reply.
mike_p wrote:If you are going to shutdown after terminating libreoffice, why bother terminate libreoffice individually. Why not just use the cronjob to shutdown?
I want to go in the morning, switch Pi on, the presentation starts and after 8 hours the presentation is terminated gracefully and the Pi shuts down. If the presentation is just killed, then the next morning I have to "recover" the presentation which means that a mouse/keyboard should be involved. This cannot be done because the monitor will be on a wall, only connected to the Pi and power supply...nothing else.
mike_p wrote:As far as I can tell your use of wmctrl in a cronjob is the problem. The cron environment doesn't know about the x window displays.
I think you are right on that but as per first link in my original post I included

Code: Select all

DISPLAY=:0 &&  /home/pi/wmctrl -c
both in the script and/or in the cronjob but it didn't work.
mike_p wrote:If you definitely want to end libreoffice before calling shutdown you could also try using pkill instead of wmctrl in the cronjob. pkill doesn't need to know about the windows and can take the process name as a parameter.
I think it will have the same effect as kill namely I will have to recover the presentation next time I boot Pi up.

Re: Cronjob fail

Posted: Tue Jan 06, 2015 6:29 pm
by Yiannis
I think I might have found the problem.

As I said when Pi starts an .odp presentation starts in full/presentation mode. This is achieved via a .desktop file.
The reason why the

Code: Select all

*/2 * * * *  export DISPLAY=:0 && /home/pi/killgracefully.sh
does not work is because when the killgracefully.sh kicks in (every 2 mins) the .odp is in presentation mode and wmctrl seems to have some issues with it. Indeed, when libreoffice is just "windowed" the cronjob and the script work as expected.

So it's an wmctrl issue and I will dig around but if in the meantime you have any suggestions do please let me know.
Thanks

Re: Cronjob fail

Posted: Tue Jan 06, 2015 7:18 pm
by DougieLawson
Yiannis wrote:

Code: Select all

*/2 * * * *  export DISPLAY=:0 && /home/pi/killgracefully.sh
Does

Code: Select all

*/2 * * * * DISPLAY=:0 /home/pi/killgracefully.sh
work any better.

Re: Cronjob fail

Posted: Thu Jan 08, 2015 4:32 pm
by Yiannis
Hi all,

I think I have solved the problem. I post below the solution. It's perhaps not the cleanest one but at least it works for me and I am not going to touch it :) .
I used xdotool to simulate the "Escape" key to exit presentation mode. The script looks like this:

Code: Select all

#!/bin/sh
export DISPLAY=:0
WIN=xdotool getwindowfocus
xdotool windowacitvate $WIN
xdotool windowfocus $WIN
xdotool key "Escape"
The line I added in my crontab:

Code: Select all

*/5 * * * * export DISPLAY=:0 && /home/pi/escape.sh
There is no need to use wmctrl anymore as the "Escape" press exits libreoffice altogether.
Again many thanks to all of you for your valuable input.
P.S. Is there a "Solved" button/feature to mark this thread as solved?