Cron job: how to run a script that requires to open display?
You will need a valid DISPLAY and XAUTHORITY to use X-Programms in Cron!
To set DISPLAY is very easy, type in the Bash:
export DISPLAY=":0.0"
In order to get a valid XAUTHORITY you have to look for it.
Under Debian/Gnome/gdm3 they are saved in var/run/gdm3/*/database
I used the following script:
export DISPLAY=":0.0"
[ -z $USER ] && USER=$( who | awk '{ print $1 }' | sort | uniq >/tmp/test )
for I in /var/run/gdm3/*; do
AUTHUSER="`echo $I | awk -F '-' '{ print $3 }'`"
for J in $USER; do
[ "${AUTHUSER}" = "${J}" ] || continue
USER="$J"
export XAUTHORITY="${I}/database" && break
done
done
sudo -u ${USER} /Path/to/xProgramm
The Var $USER can be empty, than the script looks for a valid user, otherwise you can tell the script the var!
Cron job can't run bash script (that shells python script requiring xsession) that runs perfectly well from console
The solution to this was to call the dryscrape.start_xvfb()
method before starting the dryscrape session.
How can I run a cron job that depends on a desktop without a permanent remote desktop connection open?
If you don't have a display attached, you'll need to find a headless system that can run with a virtual DISPLAY
. Headless Chrome is probably the easiest for you to swap in, and has convenient python bindings [related question].
You might also want to look into running selenium, depending on the task you need to run.
Finally, you might be interested in running a VNC server; depending on the config of your server, you can configure it to stay alive while your client disconnects. usually this is useful for temporary network hiccups, but set to a high enough timeout value, you may get the effects of a persistent, virtual, non-transient display.
how to set cron to display gui application
You can do this by setting the DISPLAY
variable to :0
. For instance:
* * * * * export DISPLAY=:0; gedit
This crontab line will open the gui software gedit every minute.
Related Topics
How to Load All Modules in a Folder
Standalone Python Applications in Linux
Linux:Python:Clear Input Buffer Before Raw_Input()
Get File Creation Time with Python on Linux
Python Sigkill Catching Strategies
How to Deal with Linux/Python Dependencies
Trouble Connecting to Phantomjs Webdriver Using Python and Selenium
How to Downgrade My Version of Python from 3.7.5 to 3.6.5 on Ubuntu
How to Limit the Amount of Time a Function Can Run for (Add a Timeout)
How to Show Pil Image in Ipython Notebook
How to Prompt For User Input and Read Command-Line Arguments
Multiprocessing: Use Only the Physical Cores
Groupby Weighted Average and Sum in Pandas Dataframe
/Usr/Bin/Env: Python2: No Such File or Directory
How to Lock a Directory Between Python Processes in Linux
How to Find the Target File's Full(Absolute Path) of the Symbolic Link or Soft Link in Python