LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-06-2005, 06:56 AM   #1
abdul_zu
Member
 
Registered: Feb 2005
Posts: 71

Rep: Reputation: 15
daemon Shell Script


Hi all,

How i can run my shell script in daemon?
 
Old 10-06-2005, 07:23 AM   #2
Config
Member
 
Registered: Jan 2001
Location: Switzerland
Distribution: Gentoo
Posts: 376

Rep: Reputation: 30
I would start your script from a virtual console (ctrl-f1 up to f6) and run it with:

./myscript &

This way, the script starts in background mode. May be, you want to check out start-stop-daemon - a tool that should be installed on most distros.

Hope this helps
 
Old 10-06-2005, 04:07 PM   #3
abdul_zu
Member
 
Registered: Feb 2005
Posts: 71

Original Poster
Rep: Reputation: 15
hi Config,

Nice to ready your reply, but really it is very hard for me to understand. I am new in linux OS, if you can give me little easy explain, i will be able to find the logic.
 
Old 10-07-2005, 03:59 AM   #4
Config
Member
 
Registered: Jan 2001
Location: Switzerland
Distribution: Gentoo
Posts: 376

Rep: Reputation: 30
I'm not posting this from a linux-machine, so I can't give you all the details you want, but still:

You say, that you have your own shell-script... usually, a shell script starts, does some job and exits. If that's the case, do the following

someone@localhost: $ cd some/dir/where/your/script/is
someone@localhost:/somedir $ ./yourscript &
[ new job id blablabla ]
someone@localhost:/somedir $

Now your script runs in background mode until it finishes. Note: if you close down the console you typed these commands, the script will be terminated too.

If you need further help, it would greatly help if you would as more specifically what you weren't able to do

Hope this helps

p.s. in my previous post, there was a typo: witht the virtual consoles, the key is: ctrl-alt-f1 up to f6. ctrl-alt-f7 is your graphical session (most likely)
 
Old 10-09-2005, 06:21 AM   #5
abdul_zu
Member
 
Registered: Feb 2005
Posts: 71

Original Poster
Rep: Reputation: 15
Hi Config,

Appriciated for your kind of help. here is the Shell Script which i want to run on the background of the system.

==========check.sh=================
#!/bin/sh
while true
do
ps -aux|grep radiusd > live.log
fsize=$(wc live.log | awk '{print $1}')
if [ $fsize = 2 ]; then
echo "Welcome to FooSoft 3.0"
else
#echo "You must be root to run this script"
killall -9 radiusd
/usr/local/sbin/rc.radiusd start > live.log
#echo "Started";
exit 1
fi

sleep 30
done

==================================

If i will run this script, Is it will make more load on the server?
 
Old 10-09-2005, 08:33 AM   #6
Config
Member
 
Registered: Jan 2001
Location: Switzerland
Distribution: Gentoo
Posts: 376

Rep: Reputation: 30
If I guess right, this is a script that checks whether radiusd is running - if not, start it.

If you want to run it by hand, you can copy it to /usr/local/bin, give it exectute permission (chmod +x /usr/local/bin/check.sh) and run it in background: (check.sh &) - then it will run in background mode. since the script never terminates, it kindof runs in background mode.
If you want to have your script run automatically at boot, then you need to check the docs of your distro, since I don't know how your distro manages startup scripts
 
Old 10-09-2005, 08:59 AM   #7
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,577
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
A few things:

1) Congratulations on being smart enough to put a sleep in your loop. Many people don't do this and are surpirsed when the CPU gets eaten up. The sleep means your performance shouldn't be dramatically impacted.

2) You should modify the "kill -9" as that should only be done as a last resort because it tells a process to die without cleaning up any resources it is using. Typically you should do a progressive kill: That is do "kill -15" then check to see if PID is gone. If not try "kill -1" then check again and finally do a "kill -9". You should do a final check and have the script exit if the "kill -9" doesn't eliminate the PID (or try it multiple times then exit) as it would indicate you have an issue an you wreak havoc on performance by starting over and over when it won't die.

3) Adding the "&" does indeed background it but you should type "nohup" before the command as well so that it doesn't exit when you logout. Background processes die when the terminal they are associated with is logged out.

4) /usr/local/sbin/rc.radiusd would appear to be your startup script for radiusd itself. You might want to examine it and see if it has a "status" option as many startup scripts do. If so rather than relying on "ps" to determine status you can use the script itself to give this to you.
 
Old 10-09-2005, 12:10 PM   #8
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
Code:
ps -aux|grep radiusd > live.log
fsize=$(wc live.log | awk '{print $1}')
You like to do things the long way, don't you?
Code:
fsize=$(ps aux | grep radiusd | wc -l)
Incidentally. to "lose" the filename from wc, use
Code:
wc -l < filename
instead of
Code:
wc -l filename
Hint: when you're using a command, read the manpage to see if you can get just the information you need by option, rather than cutting them out of stdout with filters.
 
Old 10-10-2005, 03:22 AM   #9
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,899

Rep: Reputation: 68
About killing gracefully: it is also useful to 'kill -18' process if 'kill -15' doesn't work, and try 'kill -1' only afterwise. If the process is stopped for some reason, though in this particular case it is unlikely, it may not respond even to 'kill -9' until it is continued with 'kill -CONT'.
 
Old 10-10-2005, 03:36 AM   #10
abdul_zu
Member
 
Registered: Feb 2005
Posts: 71

Original Poster
Rep: Reputation: 15
Hi all,

than you very much for kind of your all helps. now my script is running very well on my need. i wanted to ask one more question how i can check how many deamons are running in my system?
 
Old 10-10-2005, 11:13 AM   #11
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,577
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
No sure way to do this but:

ps -ft? will show you all processes that have no associated terminal. These would be ones that either had been started by some automated item (such as init scripts or cron) or those that had been backgrounded from a terminal which later was logged out.

True daemons will show up in this list but not all that shows up in this list will be daemons. Most daemons process names end in a "d" (e.g. telnetd, ftpd, esd) but not all.

Also note that some daemons ONLY start when required as they are spawned by other daemons. Most of the network stuff (telnetd for example) would only start from inetd or xininetd (depending on your distro and only when there was someone actively telnetted into your server.

Finally note that daemons can be started and backgrounded from a running terminal so would not have ? as the terminal. Additionally there are some that can be started in foreground for debugging purposes so would also not have ? as the terminal (e.g. named).
 
  


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
Shell script inside shell script treotan Linux - General 4 02-19-2009 07:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 04:00 PM
a daemon script robeb Linux - General 0 09-14-2002 04:06 AM


All times are GMT -5. The time now is 03:49 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration