Showing posts with label Scripting. Show all posts
Showing posts with label Scripting. Show all posts

How To Create A Daemon In Linux?

0 comments

Recently we struck up with a problem. The problem is to run a script continuously in background and continuously check for a folder content changes. If any modifications are done in that folder, the script once again should start one more script. We thought of doing this by using crontab. But the problem with crontab is we have to wait at least one minute to run our script. One minute is too long for my requirement. We require a solution which runs continuously in background at every micro second, it should be similar to a normal Linux daemon such as httpd, ssh, ftp etc. I have searched in Google for creating daemons in Linux. But most of the people suggested to write a daemon in C language, which is alien to me(I have learnt C language some 9 years back but now totally forgot it :( ). Here is one such link which will describe you how to create a daemon in Linux using C programming.

http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html. 

I went through many documentations and other stuff but come to a conclusion to go with Shell script. Which will work same as daemon. Here is the code for the daemon which we accomplish using while loop

while true;
do
if [ -f /testing/*.txt ]
then
echo "file created"
mv /testing/*.txt /tst/
fi
done

This while loop continuously runs because we give condition as true for this while loop and then written a if statement what to do. Once you create above script there are other points to mention, such as below once to make above script as a daemon..



So how to run the above script?
Ans : Use nohup when running the script. For those people who don't know nohup command here is the explnation. 
nohup is a command to run a program thought you logout from the machine. For example here is my script with nohup. 

nohup sh daemon.sh 

But some times this will not work. At that time run this script from crontab once and then remove the entry from crontab. 


So how to make this permanent?
Ans : Its simple. keep your script in /etc/init.d with execute permissions

cp /path/to/script/daemon.sh /etc/init.d/
chmod +x /etc/init.d/daemon.sh

Then create a link file to this script to the corresponding run-level. This is required at the time of booting.


ln -s /etc/init.d/daemon.sh /etc/rc.d/rc3.d/S43daemon.sh
ln -s /etc/init.d/daemon.sh /etc/rc.d/rc3.d/K43daemon.sh



What are those above two command will do?
Ans : The S43 will tell the system to start the script as 43 script when it boots up. 
The K43 will tell the system to shutdown cleanly when you do a shut down. 


Please share your thoughts if you have better idea to do it:-)

  • Like the post? Please Subscribe to free RSS feed to get updates
  • Batch Programming (Dos Scripting) VS Shell Scripting

    0 comments

    When I started my career in IT field. I have landed into windows world, as i know that no companies give a chance to fresher to work on production Linux servers.. Its a good experience to work on DOS scripts(aka Batch programming). There are so many similarities/differences/advantages/disadvantages between Batch programming and Shell scripting. Lets see what they are.

    Similarities


    Sl.No Batch programming Shell script
    1 Batch execution of commands in a batch file Batch execution of command in shell scripts
    2 Can read inputs from users Can read inputs from users
    3 Has control structures such as for, if, while, switch for better automating/scripting Has control structures such as for, if, while, switch for better automating/scripting
    4 Supports advanced features such as Functions and Arrays Supports advanced features such as Functions and Arrays
    5 Supports regular expressions(using findstr) Supports regular expressions
    6 Can include other programming codes such as perl(ie.. in middle of dos script we can include some other programming language code for effective scripting) Can include other programming codes such as Perl, AWK, SED etc.
     

    Differences

    Sl.No Batch programming Shell script
    1 Lack of richness of tools/commands Have good number of tools(as of my knowledge there are more than 75000 commands)
    2 Supports only one vender(ie windows) Supports for number of venders such as Sun/apple/IBM AIX/HP-UX etc.
    3 No other variants for DOS In Shall there are no of variants such as bash, ksh, csh, zsh etc..
    4 Low capabilities of integrating with other programming code in batch scripting Good capabilities of integrating other programming code in shell script
    5 Cannot handle complex regular expressions Can handle complex regular expressions.
    6 A batch file should always end with .bat There is no such concept like file extension, but a shell script file permission should be set to executable.
    7 To execute a batch program just enter the file name at CLI To execute a shell script, here are the ways to execute it
    1)chmod +x shellscript.sh;./shellscript.sh
    2)sh shellscript.sh



    Good article on how to convert your Batch program to a Shell script.
    http://tldp.org/LDP/abs/html/dosbatch.html

  • Like the post? Please Subscribe to free RSS feed to get updates
  • Archive

    Translate this page

     

    The Linux Juggernaut | Copyright 2006-2009 Surendra Kumar Anne | Surendra's Home Page | Give us feedback how we are doing, Click here