Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

Any help is appreciated.

Thanks, Pre

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

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
share|improve this answer
    
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 at 7:17
    
reboot and then try again –  Ask Feb 6 at 7:20
    
Yes, rebooted my PC itself. Still the same error. –  Pre Feb 6 at 7:28
    
"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 at 7:38
    
you are welcome friend –  Ask Feb 10 at 7:46
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.