skyhigh99
Posts: 4
Joined: Mon Jan 28, 2019 2:53 pm

Task not running with Cron

Tue Feb 05, 2019 4:57 pm

I've got a bit of a problem with automating a task with Cron... I'm trying to run a command to take a screenshot of a webpage and save it on the desktop, but it's not running through Cron. If I try to execute it manually, it's working fine.

The command I'm using is: wkhtmltoimage --height 1500 --width 350 google.com /home/pi/Desktop/print.png

I've installed the GUI for Cron and put the command in the box. If I check the log, I can see that the task has been started but it's not outputting a screenshot. I'm new to a lot of this, so am I missing something obvious as to why it's not working?

Thanks

Andyroo

Re: Task not running with Cron

Tue Feb 05, 2019 5:36 pm

Try

Code: Select all

wkhtmltoimage --height 1500 --width 350 google.com /home/pi/Desktop/print.png >> /var/log/error.log 2>&1
Then when it fails have a look in /var/log/error.log to see when the error was. Feel free to post here if stuck.

If I try it from the command line I get 'could not connect to the display' but then realised I have no GUI on this box :oops:

You also may do better with http://goolge.com or https://google.com to define the exact page as google.com is technically just an ip address where as http or https define a web page.

skyhigh99
Posts: 4
Joined: Mon Jan 28, 2019 2:53 pm

Re: Task not running with Cron

Tue Feb 05, 2019 9:50 pm

Thanks for that - I've tried it... The cron log shows that the task has been executed, but when I try to view the output log I just get the message

Code: Select all

bash: /var/log/error.log: No such file or directory
. It almost seems as if the task just isn't being executed? I have another scheduled task set up to print the screenshot which is working fine, it's just not generating the screenshot... It still works fine when I run the command myself though.

I'm not actually using google.com - that's just an example as the actual page is one on my own webserver, but it makes no difference whatever address you use.

Andyroo

Re: Task not running with Cron

Tue Feb 05, 2019 9:58 pm

Is it finding the executable - maybe putting the path to wkhtmltoimage may help?

You could wrap the command in a simple bash script and try that?

Does wkhtmltopdf work?

You could try --log-level info to see if it even executes?

User avatar
thagrol
Posts: 3178
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Task not running with Cron

Wed Feb 06, 2019 1:58 pm

skyhigh99 wrote:
Tue Feb 05, 2019 4:57 pm
I've got a bit of a problem with automating a task with Cron... I'm trying to run a command to take a screenshot of a webpage and save it on the desktop, but it's not running through Cron. If I try to execute it manually, it's working fine.

The command I'm using is: wkhtmltoimage --height 1500 --width 350 google.com /home/pi/Desktop/print.png

I've installed the GUI for Cron and put the command in the box. If I check the log, I can see that the task has been started but it's not outputting a screenshot. I'm new to a lot of this, so am I missing something obvious as to why it's not working?

Thanks
  1. "If I try to execute it manually, it's working fine."
    • Exactly how are you running it manually?
    • Are you running it in a terminal window on a logged in gui?
    • Via ssh?
    • Via vnc?
    Tasks started by cron have a very limited environment and don't have access to a GUI.
  2. Does wkhtmltoimage require a desktop/GUI?
  3. Did you include the full path to wkhtmltoimage in your crontab entry?
  4. When cron runs your job, is the desktop running with a user logged in?
If wkhtmltoimage requires a desktop, and one is available when the cron task runs try wrapping your command in a script and calling the script from cron. For example:

Code: Select all

#!/bin/bash
DISPLAY=:0
/path/to/wkhtmltoimage --height 1500 --width 350 google.com /home/pi/Desktop/print.png >> /var/log/error.log 2>&1
Replace /path/to with the correct path.

The above may still fail so check the log file.

While Andyroo has suggested using /var/log/error.log, you may find that unless your cron job is running as root it won't be able to write there. Use something like /tmp/wkhtmltoimage.log instead. Generic names like error.log aren't very usefull as they don't connect back to what generated the log.
Arguing with strangers on the internet since 1993.

Return to “Troubleshooting”