Ubuntu: run php file from cron and start browser through exec on ubuntu [duplicate]



Question:

This question already has an answer here:

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.


Solution:1

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


Solution:2

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »