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.

muru
  • 197,895
  • 55
  • 485
  • 740
Pre
  • 123
  • 1
  • 1
  • 5
  • 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 06 '16 at 04:23

1 Answers1

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
Maythux
  • 84,289
  • 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 06 '14 at 07:17
  • reboot and then try again – Maythux Feb 06 '14 at 07:20
  • Yes, rebooted my PC itself. Still the same error. – Pre Feb 06 '14 at 07: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 '14 at 07: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 '14 at 00:49