dmwsky
dmwsky

Reputation: 11

Script run from crontab can't use xvkbd or xdotool

I'd like to refresh firefox automatically after 2 hour using simple bash script. I've got:

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
xvkbd -window Firefox -text "\Cr";
exit 0

and I'd like to run it using crontab -e. I've added task but nothing happens. Everything is without any problem when I run this script with terminal. I also tried xdotool in my simply script. I'm not interested in firefox add-ons like "reload every" or "Tab auto reload", becouse every restart of firefox loses setting of add-ons. Any answer or ideas will be highly appreciated. Thank you.

Upvotes: 0

Views: 837

Answers (2)

dmwsky
dmwsky

Reputation: 11

There was something wierd with xvkbd package. Finally I found another solution. I installed MozRepl add-on for Firefox. It creates file [email protected] in .mozilla directory. I looked through defaults/preferences/mozrepl.js and I found pref("extensions.mozrepl.autoStart", false); "false" I change to "true". This is the way that Firefox runs add-on automatically even if I close the browser. I also wrote simple expect script:

#!/usr/bin/expect -f
set timeout 10
spawn nc localhost 4040
expect {
"repl>"                {send "BrowserReload(), repl.quit()\r"; exp_continue}
"lost connection"      {puts "ERROR: lost connection"}
"No route to host"     {puts "ERROR: no route to host"}
timeout                {puts "ERROR: timeout"}
}

Also I created a cron task:

00 */2 * * * /root/script.exp

Upvotes: 1

4ae1e1
4ae1e1

Reputation: 7634

crontab isn't a shell script. You should read more about the format of crontab by running man 5 crontab. If that seems too daunting for you, you should search for the myriad cron tutorials on Google. For instance, when you search for "Vixie cron tutorial", the first result is Newbie: Intro to cron, which upon brief inspection is pretty helpful to get you started.

For your particular use case, put the following in your crontab. (Either pasting it into the text editor that is opened by crontab -e, or saving it to a file and then do crontab FILENAME. I prefer the latter approach. You can view the contents of your current crontab by doing crontab -l. Read more about the crontab command by running man 1 crontab.)

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

00 */2 * * * xvkbd -window Firefox -text "\Cr"

This way the crond daemon should run the command xvkbd -window Firefox -text "\Cr" every two hours, at 0:00 am, 2:00 am, 4:00 am, etc. If you'd rather do it on 1:00 am, 3:00 am, etc., replace the last line by

00 1-23/2 * * * xvkbd -window Firefox -text "\Cr"

The first 00 is the minutes, so you can also replace that by, say, 30:

30 */2 * * * xvkbd -window Firefox -text "\Cr"

Then the command is run on 0:30 am, 2:30 am, etc.

As always, read the man page (man 5 crontab) or tutorial for more information. SO is not for complete tutorials.

Upvotes: 0

Related Questions