You are not logged in.

#1 2011-02-10 07:58:36

yms
Member
From: Switzerland
Registered: 2009-06-05
Posts: 64

[SOLVED] Automatically run a browser when network is up

Hi,

I have been trying to use netcfg's POST_UP variable to run a graphical browser (midori) automatically once the network is up and running using this script (called thanks to the POST_UP variable in my network's configuration)

#!/bin/sh

if [ "$(ps aux | grep "$USER" | grep midori | grep -v grep | grep -v defunct)" = "" ]
then
    midori > /dev/null 2>&1 &
fi

Yes, the condition in the if statement is very hackish (can not currently think of a better way to do this) but it works when I am logged in (graphically) and run netcfg manually.

The only problem is that I have configured my rc.conf to automatically connect to my network at start-up.

NETWORKS=(home)
...
DAEMONS=(... @net-profiles ...)

As the daemon is backgrounded, the boot process carries on and I am logged in X long before the POST_UP operation is done. Still, when running netcfg like that, midori does not start and I get the following error:

Midori - Cannot open display:

Although I can think of ideas why it is so, I have no idea how to make this work. Any insights ?

Thank you in advance,

yms

Last edited by yms (2011-02-10 22:06:08)


Sorry for any English mistakes, I am not a native speaker.

Offline

#2 2011-02-10 09:32:57

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: [SOLVED] Automatically run a browser when network is up

netcfg is the wrong place to run a browser from, because it will run as root - you never want that, even if you think you do.

You need something in your .shellrc or .xinitrc that checks for a net connection and runs the browser if it finds one.

Offline

#3 2011-02-10 09:59:21

yms
Member
From: Switzerland
Registered: 2009-06-05
Posts: 64

Re: [SOLVED] Automatically run a browser when network is up

Hi,

Thank you for your reply, but I took care of that by using su:

su - yms -c "/home/yms/scripts/net_autostart.sh"

So when netcfg runs that line and net_autostart.sh runs midori, everything is nice and safe. (Isn't it ?)

I considered checking for a connection elsewhere and running my browser at that time. The thing is I really need the browser to run immediately after the connection is up (whenever that may be) and I think that checking for a connection every couple of seconds (or minutes) is particularly ugly.

This is why I really would like my setup (with netcfg and everything) to work, unless it really is unsafe even when using su.


Sorry for any English mistakes, I am not a native speaker.

Offline

#4 2011-02-10 10:26:36

jOaNbE
Member
Registered: 2011-01-20
Posts: 20

Re: [SOLVED] Automatically run a browser when network is up

yms wrote:

As the daemon is backgrounded, the boot process carries on and I am logged in X long before the POST_UP operation is done. Still, when running netcfg like that, midori does not start and I get the following error:

The error you are getting is in contradiction to your statement. Also, just to be clear, just because a network interface is configured and "up"  does not automatically mean you have been assigned an IP address. Why you are trying to do it this way puzzles me.

For fun: your if statement can be truncated to "if [ -z $(pidof midori) ]".

Last edited by jOaNbE (2011-02-10 10:30:38)

Offline

#5 2011-02-10 10:30:03

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,217
Website

Re: [SOLVED] Automatically run a browser when network is up

If you want to avoid the problem with "Cannot open display" when you boot, then this should stop that:

#!/bin/sh

[[ -z "$DISPLAY" ]] && exit 0

if [ "$(ps aux | grep "$USER" | grep midori | grep -v grep | grep -v defunct)" = "" ]
then
    midori > /dev/null 2>&1 &
fi

I think the only other thing you could do is change the POST_UP and POST_DOWN scripts to maintain a state file (eg, echo 1 > /var/run/network.state for up, 0 for down) then check this file in your .xinitrc file:

if grep -q 1 /var/run/network.state ; then midori > /dev/null 2>&1 & ; fi

Offline

#6 2011-02-10 10:32:21

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,217
Website

Re: [SOLVED] Automatically run a browser when network is up

jOaNbE wrote:

For fun: your if statement can be truncated to "if [ -z $(pidof midori) ]".

Not quite -- this doesn't take into account the UID of the user running the process. That may or may not be relevant, but appears to be relevant to the OP.

EDIT: @OP If you don't mind installing another package, the Nagios Plugins has a good script to check running processes, filtered by user etc. I use it to monitor mailman:

/usr/share/nagios/libexec/check_procs --command=python2.4 -a /home/mailman/bin/qrunner -u mailman -c8: -w6:

Last edited by fukawi2 (2011-02-10 10:34:31)

Offline

#7 2011-02-10 11:23:26

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: [SOLVED] Automatically run a browser when network is up

yms wrote:

I really need the browser to run immediately after the connection is up

I'm curious now - why do you need that?

Offline

#8 2011-02-10 11:37:59

yms
Member
From: Switzerland
Registered: 2009-06-05
Posts: 64

Re: [SOLVED] Automatically run a browser when network is up

jOaNbE wrote:

The error you are getting is in contradiction to your statement.

Indeed. This is why I am posting. The thing is netcfg is executed and backgrounded and while I am getting a connection, I am logged in and X starts. A few seconds later, the connection is established and when the POST_UP operation is done, there should be no problem with the display ... except there is.

@fukawi2:
1) Concerning your solution using:

[[ -z "$DISPLAY" ]] && exit 0

This is not what I am looking for. This would exit without the error (which in my situation does not matter) but would not run midori either which is what I need.

2) I've thought about using a state file. It seemed like I could do without, but it is an acceptable solution. I am going to try that if everything else fails.

Lastly, about checking if an instance of midori is already running. Yes taking into account the UID does matter in my case. I am definitely going to check the Nagios Plugins out.


Sorry for any English mistakes, I am not a native speaker.

Offline

#9 2011-02-10 11:44:38

yms
Member
From: Switzerland
Registered: 2009-06-05
Posts: 64

Re: [SOLVED] Automatically run a browser when network is up

tomk wrote:

I'm curious now - why do you need that?

Nothing special, I have just been asked to automate redundant operations as much as possible including autorunning programs. And the reason I can't run the browser anyways is that it won't load the default tabs properly (as the connection is down).


Sorry for any English mistakes, I am not a native speaker.

Offline

#10 2011-02-10 11:51:46

jOaNbE
Member
Registered: 2011-01-20
Posts: 20

Re: [SOLVED] Automatically run a browser when network is up

fukawi2 wrote:

Not quite -- this doesn't take into account the UID of the user running the process. That may or may not be relevant, but appears to be relevant to the OP.

My answer was predicated on the assumption that the OP is attempting this on his/her laptop thus negating the need to check if other users are running midori. This seems reasonable since you would not be trying this weirdness on anything other than a laptop.

I maintain that this is strange approach to solving a non-critical issue. Just launch the browser when you are sure you have a valid IP address.

Offline

#11 2011-02-10 13:26:10

jOaNbE
Member
Registered: 2011-01-20
Posts: 20

Re: [SOLVED] Automatically run a browser when network is up

yms wrote:
jOaNbE wrote:

The error you are getting is in contradiction to your statement.

Indeed. This is why I am posting. The thing is netcfg is executed and backgrounded and while I am getting a connection, I am logged in and X starts. A few seconds later, the connection is established and when the POST_UP operation is done, there should be no problem with the display ... except there is.
.

Actually I'm wrong: My statement "The error you are getting is in contradiction to your statement." was intendend as "No, midori is called before an xserver instance started by $USER" which is false. The error is caused by the fact that midori is started by root, who doesn't have a running xserver instance.

I'm also curious as to why UID matters to you (besides avoiding running web browsers as root).

yms wrote:

... I have just been asked to automate redundant operations as much as possible including autorunning programs. And the reason I can't run the browser anyways is that it won't load the default tabs properly (as the connection is down).

Are you doing this in a multi user environment or your personal single user laptop? Who asked you to do this? What do you mean by the last sentence in the quote above?

Offline

#12 2011-02-10 20:26:16

yms
Member
From: Switzerland
Registered: 2009-06-05
Posts: 64

Re: [SOLVED] Automatically run a browser when network is up

I don't understand why you are asking all those questions. This is not about whether it is smart or not and critical or not to do that but how to do that.
Anyways, thank you for your contributions.


Sorry for any English mistakes, I am not a native speaker.

Offline

#13 2011-02-10 20:48:07

Knute
Member
From: Minot, ND
Registered: 2009-03-17
Posts: 604

Re: [SOLVED] Automatically run a browser when network is up

If you want to do this in a manner that meets all your conditions, you could simply have a state file for your net connection, and one for your X session, then in your script, use a while statement to keep checking until both files come back with a 1.


Knute

Offline

#14 2011-02-10 21:55:05

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,217
Website

Re: [SOLVED] Automatically run a browser when network is up

yms wrote:

@fukawi2:
1) Concerning your solution using:

[[ -z "$DISPLAY" ]] && exit 0

This is not what I am looking for. This would exit without the error (which in my situation does not matter) but would not run midori either which is what I need.

Well you could make it exit with an error, but you can NOT start midori during boot since there is no X session for it to use as the display.

Offline

#15 2011-02-10 22:05:29

yms
Member
From: Switzerland
Registered: 2009-06-05
Posts: 64

Re: [SOLVED] Automatically run a browser when network is up

fukawi2 wrote:

Well you could make it exit with an error, but you can NOT start midori during boot since there is no X session for it to use as the display.

Well that settles it then. I will have to go with the status file and check its existence once X started.
Thank you all very much for your contributions. I'm marking this as solved:

Use status files and do the checks after X starts.


Sorry for any English mistakes, I am not a native speaker.

Offline

Board footer

Powered by FluxBB