Search code examples
bashubuntu-14.04croncron-task

Bash script executes fine on its own, but not with cron


I type

crontab -e

my crontab looks like

*/1 * * * * /home/sara/Desktop/kioskscripts/reloadpage.sh >> /home/sara/Desktop/kioskscripts/logfile.log

logfile is created in /kioskscripts but remains empty.

reloadpage.sh looks like this

#!/bin/bash
sleep 5
/usr/bin/xdotool key F5

sh reloadpage.sh

works as expected and simulates f5 being pressed 5 seconds after execution.


Solution

  • The program executed by cron does not have an active window, so you would need to explicitly specify which window you want the keystroke to be sent to using the --window option.

    You can get the window id of your currently active window with xdotool getactivewindow and then use that number in an xdotool command. Or you can use xdotool search with various options to find the window you want to direct the keystroke to. Read man xdotool for the various search options. (You can do that in a single command: xdotool search --name Foo key F5 will send F5 to a window with FOO in its name.)

    But that will only work if the indicated window accepts the events, and many windows don't.