You are not logged in.

#1 2011-08-23 15:18:05

nTia89
Banned
From: varese, italy
Registered: 2008-12-22
Posts: 1,230

[solved] script control daemon

hi to everybody,

i want to create a script that start a daemon, then launch an application and finally when the application close, stopping the daemon

anybody can help me ?

thanks

Last edited by nTia89 (2011-08-25 19:47:49)


+pc: custom | AMD Opteron 175 | nForce4 Ultra | 2GB ram DDR400 | nVidia 9800GT 1GB | ArchLinux x86_64 w/ openbox
+laptop: Apple | MacBook (2,1) | 2GB ram | Mac OS X 10.4 -> DIED
+ultrabook: Dell | XPS 13 (9343) | 8GB ram | 256GB ssd | FullHD display | Windows 8.1 64bit ArchLinux x86_64 w/ Gnome

Offline

#2 2011-08-23 15:33:12

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: [solved] script control daemon

#!/bin/sh

sudo rc.d start <daemon>
<application>
sudo rc.d stop <daemon>

?

Offline

#3 2011-08-23 15:34:53

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [solved] script control daemon

Try something like

if pgrep app >/dev/null; then
  sleep 60
else
   sudo rc.d stop daemon 
fi

to check if the app is running every minute.

Last edited by karol (2011-08-23 15:36:20)

Offline

#4 2011-08-23 17:40:13

hbekel
Member
Registered: 2008-10-04
Posts: 311

Re: [solved] script control daemon

Better use $! and wait:

# start daemon...

# start the app in background, get its pid and wait for it to terminate:
app & pid=$!
wait $pid

# stop daemon...

Offline

#5 2011-08-23 18:00:57

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: [solved] script control daemon

hbekel wrote:

Better use $! and wait:

# start daemon...

# start the app in background, get its pid and wait for it to terminate:
app & pid=$!
wait $pid

# stop daemon...

Aka,

app # start the app in the foreground

?

I think karol was thinking about the case when the app forks into the background or something and you need to wait for it to close.  But usually there's a --no-fork option or something for that case.

Offline

#6 2011-08-23 21:00:01

nTia89
Banned
From: varese, italy
Registered: 2008-12-22
Posts: 1,230

Re: [solved] script control daemon

thanks very much hbekel, this is what i need:

http://pastebin.com/SwXr1kgJ

now i have to refine it...... for example, don't start the application until the daemon is started, any idea ?


+pc: custom | AMD Opteron 175 | nForce4 Ultra | 2GB ram DDR400 | nVidia 9800GT 1GB | ArchLinux x86_64 w/ openbox
+laptop: Apple | MacBook (2,1) | 2GB ram | Mac OS X 10.4 -> DIED
+ultrabook: Dell | XPS 13 (9343) | 8GB ram | 256GB ssd | FullHD display | Windows 8.1 64bit ArchLinux x86_64 w/ Gnome

Offline

#7 2011-08-23 21:21:05

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: [solved] script control daemon

But seriously, why not this?

#!/bin/bash

gksudo /etc/rc.d/tor start
chromium --proxy-server="socks://localhost:9050"
gksudo /etc/rc.d/tor stop

Offline

#8 2011-08-23 21:33:12

nTia89
Banned
From: varese, italy
Registered: 2008-12-22
Posts: 1,230

Re: [solved] script control daemon

what change between my (hbekel) script and your ?

Last edited by nTia89 (2011-08-23 21:34:54)


+pc: custom | AMD Opteron 175 | nForce4 Ultra | 2GB ram DDR400 | nVidia 9800GT 1GB | ArchLinux x86_64 w/ openbox
+laptop: Apple | MacBook (2,1) | 2GB ram | Mac OS X 10.4 -> DIED
+ultrabook: Dell | XPS 13 (9343) | 8GB ram | 256GB ssd | FullHD display | Windows 8.1 64bit ArchLinux x86_64 w/ Gnome

Offline

#9 2011-08-24 10:02:41

hbekel
Member
Registered: 2008-10-04
Posts: 311

Re: [solved] script control daemon

tavianator wrote:

But seriously, why not this?

#!/bin/bash

gksudo /etc/rc.d/tor start
chromium --proxy-server="socks://localhost:9050"
gksudo /etc/rc.d/tor stop

Yes, of course. Don't know what I was thinking, maybe Karol send me on the wrong track smile For the above purpose this is all that's needed. Sorry for causing confusion.

@nTia89: the method I described is useful only in the case where you want to start some app and do some more stuff right away before finally
waiting for the app to terminate. The way I use it in my example is pretty pointless, as it has the exact same effect as simply starting the app in the foreground.

Offline

#10 2011-08-24 12:07:57

nTia89
Banned
From: varese, italy
Registered: 2008-12-22
Posts: 1,230

Re: [solved] script control daemon

ok,

now i want to refine my script.

how can i check if the daemon is already running ?


+pc: custom | AMD Opteron 175 | nForce4 Ultra | 2GB ram DDR400 | nVidia 9800GT 1GB | ArchLinux x86_64 w/ openbox
+laptop: Apple | MacBook (2,1) | 2GB ram | Mac OS X 10.4 -> DIED
+ultrabook: Dell | XPS 13 (9343) | 8GB ram | 256GB ssd | FullHD display | Windows 8.1 64bit ArchLinux x86_64 w/ Gnome

Offline

#11 2011-08-24 12:11:22

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [solved] script control daemon

nTia89 wrote:

how can i check if the daemon is already running ?

if pgrep daemon_name >/dev/null; then
  do_this
else
  do_that
fi

works for most apps,

rc.d list started | grep daemon_name

may be better for daemons.

Last edited by karol (2011-08-24 12:12:29)

Offline

#12 2011-08-25 19:47:38

nTia89
Banned
From: varese, italy
Registered: 2008-12-22
Posts: 1,230

Re: [solved] script control daemon

thanks very much, i think i can put solved
despite i used another if costruct...... because with your it can't works.....

but, this is the result:   http://pastebin.com/YZqQvGTJ

with this script i finally can use tor as i need, without the firefox buggy extension !!!!

Last edited by nTia89 (2011-08-25 19:49:01)


+pc: custom | AMD Opteron 175 | nForce4 Ultra | 2GB ram DDR400 | nVidia 9800GT 1GB | ArchLinux x86_64 w/ openbox
+laptop: Apple | MacBook (2,1) | 2GB ram | Mac OS X 10.4 -> DIED
+ultrabook: Dell | XPS 13 (9343) | 8GB ram | 256GB ssd | FullHD display | Windows 8.1 64bit ArchLinux x86_64 w/ Gnome

Offline

Board footer

Powered by FluxBB