12

I need to open a browser and access my server with the ip address 1.2.3.4. First I ping my server and if the ping did not fail I launch the home page in a browser. For this I have written a bash file as follows:

# add ip / hostname separated by white space
myHost=1.2.3.4

# no ping request
COUNT=1

count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
# 100% failed
echo "Host : $myHost is down (ping failed) at $(date)"
else
    firefox $myHost
fi

But I am getting an error message Error: no display specified. Firefox is not launching. What am I doing wrong.

This file is getting called every 5 minutes using a cronjob. The cronjob seems to be working fine.

1
  • This question was asked and solved (Feb 2014) long before the "duplicate question" was asked (Aug 2014). So I don't know why this is marked duplicate and why people are answering to this even now. Should I close this as solved ? How do I do that?
    – Pre
    Dec 6, 2016 at 4:23

1 Answer 1

7

You have to specify the display.

Add this to your script before running firefox

 export DISPLAY=:0

your script would be like:

#add ip / hostname separated by white space
myHost=1.2.3.4
export DISPLAY=:0
# no ping request
COUNT=1

count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
# 100% failed
echo "Host : $myHost is down (ping failed) at $(date)"
else
    firefox $myHost
fi
5
  • I tried adding export DISPLAY=:0, but now it is showing an error : 'No protocol specified. Error: cannot open display: :0'. What am I missing?
    – Pre
    Feb 6, 2014 at 7:17
  • reboot and then try again
    – Maythux
    Feb 6, 2014 at 7:20
  • Yes, rebooted my PC itself. Still the same error.
    – Pre
    Feb 6, 2014 at 7:28
  • 1
    "export DISPLAY=:0" and then I did a "xhost +" and now its working fine. I also did a "/usr/bin/firefox $myHost". Now its working fine. Thank you
    – Pre
    Feb 10, 2014 at 7:38
  • 1
    Just to further clarify for anyone else that comes along: You need to run 'xhost +' once from a terminal in ubuntu, and then you can launch GUIs (firefox) remotely (ssh,vboxmanage guestcontrol, etc)
    – apple16
    Jun 25, 2014 at 0:49

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