Search code examples
bashterminalcronxterm

How to open xterm from script being runned by cron?


I've got cronjob running this script every 20 minutes but I cannot understand how to make xterm open with it..

#!/bin/bash

xterm -hold -e "echo Time to relax"
play -q /home/username/music/relax.mp3 trim 0 01:20
sleep 120
exit 0

and in crontab -e

*/20 * * * * /home/username/Desktop/alarm.sh

The script runs fine but I just can't get xterm to open, Am I doing anything wrong here? When I run the script via terminal, ex: sh /home/username/Desktop/alarm.sh the terminal opens just fine and the song plays as well..


Solution

  • When the script is not started from within a X session, which is the case for a cron script, you need to pass the DISPLAY environment variable:

    DISPLAY=:0 xterm -hold -e "echo Time to relax"
    

    If you are running a standard desktop system :0 should work as the value, otherwise you need to pass the correct value for your system.