Saturday, December 5, 2009

LinuxInterview: How to make script run as service?

Making a script run at boot time needs certain consideration and configuration.The Startup script are usually under /etc/init.d directory.
Some Admin think that by just putting the script under /etc/init.d will make it run as service.
No !! The Story is little different.

Say, I have a script called myscript which I want to run as service either:

/etc/init.d/myscript [restart|reload|start|stop]
or
service myscript start

But little more steps are usually needed.
I am talking about Red Hat Distribution but may run comfortably in other *nix too.

I have a script called myscript which contains:

#cat myscript

#!/bin/sh
service nfs stop

Being a Linux Admin, I will try to put it under /etc/init.d/ directory.
Try to run it as:

service myscript start

And It does run comfortably:

#service myscript start
Shutting down NFS mountd: [ OK ]
Shutting down NFS daemon: [ OK ]
Shutting down NFS services:

But when I try making this service run at boot time:

#chkconfig myscript on
service myscript does not support chkconfig

Hmm...Not working !!!
How to fix it????
So here is the complete solution:

(Assume the name of my script is myscript)

1 - Copy your script into /etc/init.d folder
2 - cd /etc/init.d
3 - chmod +x myscript
4 - Add these lines, including #, right after #!/bin/bash or #!/bin/sh:

# chkconfig: 2345 95 20
# description: Some description
# What your script does (not sure if this is necessary though)
# processname: myscript

5 - chkconfig –level 2345 myscript on

Try now !!!
It will work.

LinuxInterview: Difference between Hard Link and Soft Link? - Creative Understanding

There's an enormous difference, actually. There's really no such thing as a "hard link". If anything, there is an action that you can call "creating a hard link".

The thing is, a file can have several names. If you create a hard link (by running `ln' without `-s'), you simply create a new name which points to the exact same file as the original name. For example, if you run the following commands:

Code:
cd /tmp
echo test >file1
ln file1 file2
Then you will have two filenames, /tmp/file1 and /tmp/file2, which both point to the same actual file. Notice the important distinction between a file and its name(s): A name knows which file it points to, but a file doesn't know what name(s) it has.

The reason that I say that there's no such thing as a hard link is because the names file1 and file2 are equivalent. There's no "original" name. You can in no way tell that "file2 is a hard link to file1", or the opposite, or anything evem similar -- they are simply two different names for the same file.

An interesting implication of this is that the `rm' command doesn't actually remove files. Instead, it only removes filenames (the system call that it uses is called `unlink', not `remove' or `delete'). The actual file is only automatically deleted by the system when it has no more names. Thus, if you delete file1, file2 will still points to the same file, and conversely, if you delete file2, file1 will still be pointing to the same file.

A symlink, on the other hand, is a file in its own right. It contains the filename of the file that it points to. If file2 were a symlink to file1, and you deleted file1, the file2 symlink would be "broken" (since the filename that it points to no longer exists). In this case, file1 is the name of the actual, original file, and file2 is just a symlink that points to it by name.

For bonus points: What would be the difference in output from the cat command in the following two sequences of commands, and why?

Sequence 1:

Code:
cd /tmp
echo 1 >file1
ln file1 file2
rm file1
echo 2 >file1
cat file2
rm file1 file2
Sequence 2:

Code:
cd /tmp
echo 1 >file1
ln -s file1 file2
rm file1
echo 2 >file1
cat file2
rm file1 file2