LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to test if the script is started with an & (detached from console) (https://www.linuxquestions.org/questions/linux-software-2/how-to-test-if-the-script-is-started-with-an-and-detached-from-console-681936/)

frater 11-08-2008 03:44 AM

How to test if the script is started with an & (detached from console)
 
To preserve resources I have introduced a "sleep 120" in case my linuxbox hasn't been running for a while.
It is intended to be run from /etc/init.d

Of course I will have to put an ampersand at the end. Otherwise the system will wait idle.

Code:

uptime | grep -q " min" && sleep 120
I want to improve that line by testing if the script was really started with an & at the end.

If one knows how to test if the script is "detached" from the script that started it, it would do as well...

unSpawn 11-08-2008 04:21 AM

First of all if you sleep for that period, why not use a cronjob instead? Second, starting the "main" script gives you access to the PID, which children will have as their PPID. Finally you could use something like 'pgrep -f uptime >/dev/null 2>&1|| echo "no it's not running"'.

jlinkels 11-08-2008 07:03 AM

120 seconds is the break even point: shorter intervals are better implemented using sleep, larger using cron. The advantage of cron here is that you don't have to worry whether your script is backgrounded correctly. I also think that the check interval might be a bit longer as wel, so another point in favor of using cron.

However... if you do this for the sake of fun and see if you can do what you want to achieve just because it is nice to do. You can also check with pidof yourscriptname.

If you look into the regular startup scripts in /etc/init.d, you might find other examples simple and advanced to check if a script is already running.

jlinkels

frater 11-09-2008 03:12 PM

I don't think you know what I'm trying to achieve. This is of course my own fault....

I installed twonky (a uPnP server) on my satellite receiver and it comes with a startup-script. I want to preserve this script as much as possible.

In my rcS I've added the line:
Code:

[ -e /usr/local/TwonkyVision/twonkymedia.sh ] && /usr/local/TwonkyVision/twonkymedia.sh start &
Because my satellite receiver needs all the resources it can get during startup, I don't want twonky to slow it down.
Adding this line in the start segment of twonkymedia.sh I can achieve this goal:
Code:

uptime | grep -q " min" && sleep 120
If the script is started by hand and without the &, I don't want it to introduce this 2 minute pause.

It's not really a problem, but I was presented with the problem and still would like to know the solution....


All times are GMT -5. The time now is 06:45 AM.