Why do I need this in the script tutorial?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why do I need this in the script tutorial?
# 1  
Old 06-11-2011
Why do I need this in the script tutorial?

I'm taking a tutorial - intro to Unix Shell Scripting - and the first exercise is a walk-through of writing a script to achieve the following:

"As a network administrator you are collecting configuration information about all FTP servers in the organisation. You need to write a script to collect information about:
- The FTP server that is running.
- The configuration file and its location on the FTP server.
- Permission information of the configuration file.
- Useful content of the configuration file."

I'm sure this is probably a naive script that would make some of you gosu's vomit but just go along with it for now please. SO anyway, it has me enter the following commands to start the script:

ps ax | grep -v grep | grep ftpd 1> /dev/null || echo "NO FTP service running"
#### end of snippet #####

Now obviously when I finish the script the tutorial prints the desired output. However I don't understand why there are so many greps in this command. For example, I ran the same command on a Ubuntu vm:
dvorjak@dvorjak:~$ ps ax | grep -v grep | grep ftpd
968 ?         Ss                0:00 /usr/sbin/vsftpd
dvorjak@dvorjak:~$ ps ax | grep -v grep | grep ftpd 1> /dev/null || echo "NO FTP service running"

Why do I need so many greps in that command? Again - I realise this is some tutorial that is presenting this script, not you, but can you guess why they would require this? Just fyi - here's the whole script:
ps ax | grep -v grep | grep ftpd 1> /dev/null || echo "NO FTP service running"
CONFIG_FILE=$(ps ax | grep -v grep | grep ftpd | awk '{print $6}')
echo "FTP configuration file on `hostname` is"
basename $CONFIG_FILE
echo "It is located in the directory"
dirname $CONFIG_FILE
echo ________________Detailed information about the file_______________
echo "_______________Non-commented lines in the ${CONFIG_FILE}__________"
cat $CONFIG_FILE | grep -v ^#

# 2  
Old 06-11-2011
Since you are testing return you need to remove the grep process when greping Smilie

For instance, i will grep for nonexisting process
$ ps ax | grep mpd2 || echo "No MPD2 running"
29221 pts/6    S+     0:00 grep --color=auto mpd2

As you see, grep returned 0 (success), since ps 'caught' the actual grep command with parameter mpd2.

We avoid this by using :
$ ps ax | grep -v grep | grep mpd2 || echo "No MPD2 running"
No MPD2 running

Second grep now returned 1, since mpd2 process wasn't found and we greped out the grep 'caught' by the ps command.

Try testing $? after command in shell without the || or && and you will come to understand.
Also check the && and || operators and what they mean.

Last edited by Peasant; 06-11-2011 at 02:39 PM..
This User Gave Thanks to Peasant For This Post:
# 3  
Old 06-11-2011
Oh I forgot about that - kind of like when I run ps there will be a "ps" process showing in the list of processes simply because I ran the ps command. Ok I should have caught that.
# 4  
Old 06-11-2011
I read the other day, a better way to do this:
ps -ax | grep [f]tpd

# 5  
Old 06-11-2011
Better still, use pgrep/pkill. They may not be universal, but they're not rare either. A lot of systems have them.

If not, your ps probably has a way to modify its output format to only include the command's name in the output (argv[0]). This way, you don't have to defend against command line arguments colliding with command names (or user names, or group names, or ... you get the picture).

Linux's ps seems to have a -C option for filtering on command name, which could also be of use. But, if using Linux, pgrep is almost certainly available.

From Man Page for ps (Linux Section 1) - The UNIX and Linux Forums
Print only the process IDs of syslogd:

ps -C syslogd -o pid=
Sadly, ps is one of those commands that is not at all portable. ps on a *BSD system and ps on a linux box and ps on one of the commercial unices tend to require different arguments for everthing but the most trivial invocations.

# 6  
Old 06-11-2011
which is exactly why it is extensively used in combination with grep, in order to make it work no matter what teh underlying system supports in the ps command.
# 7  
Old 06-11-2011
Originally Posted by redhead
which is exactly why it is extensively used in combination with grep, in order to make it work no matter what teh underlying system supports in the ps command.
It may work, mostly, regardless of the underlying system, but it's not a robust solution. False positives remain a possibility and could result in killing the wrong process or mistakenly concluding that a dead process is still running.

A few years ago I helped someone troubleshoot an intermittent failure which I found to be caused by grepping ps output for a short command name which sometimes occured as a substring in a related command's arguments.

You could improve the situation by tightening up the regular expression a bit, or perhaps using AWK to search for the term in a specific field, but at that point you may be depending on non-portable output format characteristics. May as well make life easier and just use the non-portable ps options.

For this task, I'm not sure you can achieve portability and correctness using ps. I think with pgrep you can, but I haven't taken a close look at many pgrep implementations. That said, I realize that many systems and tasks are not mission critical, and so an 80% solution is often sufficient.

PID files? Mostly useless and racy.

Originally Posted by William Wallace -- Scottish Sysadmin
"They may take our lives, but they'll never take... OUR P{GREP,KILL}!!!"
Okay, maybe I went a little off the rails there. Smilie

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Web Development

Vue JS 2 Tutorial by The Net Ninja: A Recommended Vue.js Video Tutorial Series

A number of people have asked me how to get started with Vue.js and my reply before today was to Google "Vue.js". That has changed and my recommendation to anyone who wants to learn the fastest growing, easiest to learn and use Vue.js web dev framework is to watch this video tutorial series: ... (0 Replies)
Discussion started by: Neo
0 Replies

2. Solaris

Need LAMP tutorial!

Hello Everybody, Does anybody know or recommend me a tutorial for LAMP server on Solaris 10 x86? (3 Replies)
Discussion started by: ahmedamer12
3 Replies

3. AIX

AIX useful tutorial...

hi guys, I want to learn AIX, I am a beginner for this operating system of IBM. Can anyone give me some useful tutorial for a beginner like me?... Thanks... + GOD bless + ^_^ (1 Reply)
Discussion started by: TechReader
1 Replies

4. Shell Programming and Scripting

Shell script tutorial?

Hi All, I have started preparation of Shell script...Can anybody send me the link where I can get a right tutorial or pdf file of shell script... Thanks in Advance (2 Replies)
Discussion started by: kunalpatil09
2 Replies

5. IP Networking

RouterSim tutorial.

Hi everyone... Well, this is a kina of help tutorial question. I'm using actually the “RouterSim Network Visualizer” an environment where you can practice and create your own LAN, I will be honest, I'm new to creating and managing networks, but not to new about IP´s. So, the question, anyone... (0 Replies)
Discussion started by: TARFU
0 Replies

6. Post Here to Contact Site Administrators and Moderators

Tutorial forum?

I got this account a while ago and didn't have much time to try it out. After having had some frustration by posting tutorials regarding various Linux activites of mine in my Slashdot journals, it occurred to me that this forum might be better suited. I know I can probably post in the OS specific... (1 Reply)
Discussion started by: deckard
1 Replies

7. Shell Programming and Scripting

Tk tutorial

Anyone know any good website with a step by step instructions on writing Perl GUI scripts with Tk ??? (3 Replies)
Discussion started by: perleo
3 Replies

8. UNIX for Dummies Questions & Answers

Unix Tutorial

Any good UNIX tutorials to download? Any good APACHE tutorials? thanks (2 Replies)
Discussion started by: TEKGALAHAD
2 Replies

9. UNIX for Dummies Questions & Answers

Procmail Tutorial

Hello, Somebody knows a good procmail tutorial in the net? Thanks!:) (1 Reply)
Discussion started by: luiz_fer10
1 Replies
Login or Register to Ask a Question