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 If one knows how to test if the script is "detached" from the script that started it, it would do as well... |
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"'.
|
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 |
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 & Adding this line in the start segment of twonkymedia.sh I can achieve this goal: Code:
uptime | grep -q " min" && sleep 120 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. |