2

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.

1
  • Hi @user598527 - I think it's probably a simple permissions thing - firefox is being launched as user cron, however the "UI" user is not that user, so firefox isn't able to open a window as the UI user who launched firefox originally.. Depending on the platform it might also be an X settings thing too.. You might see something in the cron log (if configured) or system.log...
    – Mr R
    Commented Apr 28, 2022 at 8:59

1 Answer 1

6

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
2
  • Adding export DISPLAY=:0.0 resolved the issue on Fedora 35, Wayland.
    – user598527
    Commented Apr 28, 2022 at 10:27
  • That's good to hear. BTW, I should have highlighted this in my answer, but the firefox started by cron will inherit cron's fairly bare-bones, minimalist environment, including $PATH. So if firefox needs anything from your usual env, you should either set those env vars in your script or just source your usual shell startup files (e.g. ~/.bash_profile, ~/.bashrc, etc) files from the script. This is only likely to be a problem if you execute other programs (e.g. pdf viewer, media player, etc) from firefox. See man cron and man 5 crontab for some details about cron's env.
    – cas
    Commented Apr 28, 2022 at 12:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .