0

I am trying to start FireFox from php script using exec("firefox"). This works fine if i run php file from the terminal but does not work when run by the cron. User for cron & terminal is root. Please suggest some solutions.

vkj
  • 101
  • Could you say why exactly you want to open Firefox? If you simply want to execute a php script, adding it to cron tab as php -f /my/php/file.php is enough. Just need to have that php-cli installed. – GreggD May 21 '16 at 17:53
  • @TedM. my php file triggers a selenium jar file which then uses firefox for the automation. I tried starting Firefox from selenium but facing the same issue as with PHP. – vkj May 21 '16 at 18:13
  • The issue might be related to root itself... To open Firefox you need to have desktop environment running and root does not normally have that. I would try forcing it to open FF on your normal user's desktop, I've seen some ways to open apps in such way, but mostly using ssh (might work with cron as well). Not sure if it's a good idea though... – GreggD May 21 '16 at 18:17
  • Investigate this: http://askubuntu.com/a/47658 – GreggD May 21 '16 at 18:31

2 Answers2

0

You can try using watch. watch -10 <YOUR COMMAND> This will try executing your command every 10 second in terminal.

0

Create a script (and chmod +x it):

#!/bin/bash
export DISPLAY=:0
firefox

Run crontab -e and add at the bottom:

* * * * * /path/to/my/script

..and it'll open Firefox every minute on your user's desktop.


Figured you might also want a feature to automatically close it after some time, instead of incrementing opened windows or tabs. I would suggest creating separate FF profile just for cron. Run firefox -P and create a new profile there. Name it... let's say "cron". Then use this script:

#!/bin/bash
export DISPLAY=:0
firefox -P cron &
sleep 30 # set here any amount of seconds you need
pkill -f "/usr/lib/firefox/firefox -P cron"

Works like charm in my environment.

Oh, you will have to update your everyday links to FF to load your normal profile, for example like this: firefox -P default.

GreggD
  • 1,114