1)Define a deamon?
Ans: In Unix or Other Multitasking Operating Systems a Daemon is a computer program that runs in Background, rather than under the control of a user. These are usually Intiated as Background processes.Typically Daemons have names that ends with "d" e.g. mysqld,syslogd,pptpd,sshd etc.
2.) Can we use crontab to run a script per second? if yes how? if no why?
Ans: I think we can't. In crontab (the one the user can edit) the smallest timeperiod is 1 minute. The crontab deamon, which checks the crontab file, runs every 30 seconds.
But we can make use of some scripting to run script after some seconds but not every second.
3)I have a file each line contains “/var/www/html”, replace this entry in all lines with “/home/xyz/red” with single command or an editor.
Ans: For this make the use of sed(Stream Editor) command by using some other separator than "/".
AS : #sed -i 's#/var/www/html#/home/xyz/red#g'
4)Please give me syntax for a FOR loop in Bash scripting(please specify 2 types too).
Ans:
1.for (( EXP1; EXP2; EXP3 ))
do
command1
command2
command3
done
e.g.
for (( c=1; c<=5; c++ ))
do
echo "Welcome $c times..."
done
2.) for VARIABLE in 1 2 3 4 5 .. N
do
command1
command2
commandN
done
e.g.
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
5) How to create a deamon with shell scripting?
Ans : Creating a daemon is easy in shell script called monitor.sh
Code:
#!/bin/bash
while :
do
# add code to monitor dir here and take same action
done
Run monitor.sh in background or use nohup or friends:
Code:
nohup monitor.sh &
6)How can i check exit status of my previous command?
Ans: #echo $?
If it gives 0 then last command you have run is executed successfully.
Whenever you execute a command in Linux, the command actually returns an exit status code which is useful when you need information on how the execution went like if it failed or succeeded. An exit status code of:
* 0 means the command executed properly without any errors
* 1 means the command executed but there may have been some minor issues (think of these as warnings)
* 2 means the command executed and there was a major problem (think of these as errors)
* Getting the exit codes is easy, simply execute any command in Linux. Immediately after, to check its exit status, type
echo $?
and you will see a number printed, which will be 0, 1, or 2.