LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   daemon Shell Script (https://www.linuxquestions.org/questions/linux-newbie-8/daemon-shell-script-370258/)

abdul_zu 10-06-2005 05:56 AM

daemon Shell Script
 
Hi all,

How i can run my shell script in daemon?

Config 10-06-2005 06:23 AM

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

abdul_zu 10-06-2005 03:07 PM

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.

Config 10-07-2005 02:59 AM

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)

abdul_zu 10-09-2005 05:21 AM

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?

Config 10-09-2005 07:33 AM

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

MensaWater 10-09-2005 07:59 AM

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.

eddiebaby1023 10-09-2005 11:10 AM

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.

raskin 10-10-2005 02:22 AM

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'.

abdul_zu 10-10-2005 02:36 AM

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?

MensaWater 10-10-2005 10:13 AM

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).


All times are GMT -5. The time now is 07:18 AM.