LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-08-2008, 03:44 AM   #1
frater
Member
 
Registered: Jul 2008
Posts: 121

Rep: Reputation: 23
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...
 
Old 11-08-2008, 04:21 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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"'.
 
Old 11-08-2008, 07:03 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 11-09-2008, 03:12 PM   #4
frater
Member
 
Registered: Jul 2008
Posts: 121

Original Poster
Rep: Reputation: 23
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....

Last edited by frater; 11-09-2008 at 03:14 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
echo test >/dev/console freezes cambie Linux - General 0 02-20-2007 12:41 PM
I have started working on a tutorial script kedens Linux - General 6 10-15-2006 04:48 PM
How to boot RO && Single User Mode to test hdparm ??? skouket Slackware 5 06-21-2005 07:29 AM
[Req]: How to config WebSvr & DNS Svr from console by Bash shell script CiF Linux - Networking 2 03-22-2005 12:31 AM
How Do I Have a script started at boot time? Chijtska Linux - General 12 03-18-2002 05:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration