I use this script to restart Firefox overnight (to apply the package manager and addon updates):
#!/bin/bash killall -s SIGTERM firefox; sleep 15 firefox -P "user" & firefox -P "default settings" &
crontab (runs at 3 AM):
0 3 * * * /usr/local/bin/firefox.sh
When executed manually the script works as expected: closes the Firefox processes and starts two profiles in their own windows.
When cron runs the script, Firefox is consistently only closed.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
cron jobs run in a completely separate environment, isolated from your usual GUI desktop or terminal environment.
firefox expects to be run as a child process of your desktop environment or, at the very least to have a valid DISPLAY variable set.
It is sometimes possible to get cron jobs to start or interact with GUI programs. Try adding export DISPLAY=:0.0 as the second line of your script. If :0.0 doesn’t work, run a terminal in your desktop and run echo $DISPLAY to get the correct value.
If this still doesn’t work, you may also need to set XAUTHORITY=$HOME/.Xauthority or maybe use xauth to enable access.
Note that any program started from cron (including firefox) will inherit cron’s fairly minimalist environment. Variables like PATH, LOGNAME and/or USER may be different to what you expect, and many variables won’t be set at all. e.g. the LC_* locale variables may not be set (depending on distro – e.g. cron in Debian reads /etc/environment and /etc/default/locale. I don’t know if that’s also the case on Fedora or not). If that program needs specific environment variables set to certain values, you’ll need to set them in your crontab file, or export them in your script too. Or just source your usual shell startup files from the script.
Firefox, Chromium, and other web browsers may need http_proxy, https_proxy and other proxy-related variables set.
FYI, this is roughly how running GUI programs over ssh -X works. The -X option enables X11 forwarding. It sets up a tunnel to proxy X protocol over the ssh connection, and sets the DISPLAY variable to point to that tunnel.
I use this to, for example, run xsane on my server (hostname “ganesh”, which has a HP3030 printer/scanner attached) but have the windows display on my workstation’s monitor – i.e. ssh -X ganesh xsane.
If I were to run ssh -X ganesh 'echo $DISPLAY' (needs to be single-quoted or escaped so that my local shell doesn’t interpolate the variable), I’d see something like:
$ ssh -X ganesh 'echo $DISPLAY' ganesh:11.0
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0