Useful Linux Commands Reference

Read through, try out, understand, enjoy and more importantly share!

As a server administrator, there’s nothing that bugs me more than services that die all of a sudden. For a web server, an hour’s down time could mean losing a few hundred visitors. This could translate to a loss of advertising income too if you serve ads on your web pages.

I found a snippet of code that I modified to use as a simple service monitoring and restarting tool that I would like to share with all of you.

First off, let me state that I did not write the following script myself. It was adapted from a script I found somewhere (but I didn’t bookmark the source). Just create an empty file and enter the following lines in it:

#!/bin/bash # Services Restarter - Automatically restart httpd and pure-ftpd if they die /bin/netstat -ln | /bin/grep ":80 " | /usr/bin/wc -l | /bin/awk '{if ($1 == 0) system("/sbin/service httpd restart") }' /bin/netstat -ln | /bin/grep ":21 " | /usr/bin/wc -l | /bin/awk '{if ($1 == 0) system("/sbin/service pure-ftpd start") }'

Now just save the file somewhere. As an example, I going to assume that you saved the file as /scripts/svcschk.

So what does the script do? Essentially, the script uses the netstat command line tool to see whether the server is listening on ports 80 (http service) and 21 (ftp service). If the server is not listening to the ports, then it will attempt to start the appropriate services.

Let’s chop the line that monitors the http service into smaller bits so we can understand what’s being done:

/bin/netstat -ln

Invokes the netstat command to list all listening ports and using presenting bound IP addresses in numeric format. The reason we’d want to use the numeric format for IPs is that it is much faster because we’re not resolving the IPs to hostnames.

| /bin/grep ":80 "

We then pipe the output of netstat to grep looking for addresses that are listening to port 80.

If you noticed, we actually checked for ":80 "; ie 80 with a space afterwards. The reason for the space is to filter out other services that run on ports 8000 or 8080 for example, which will be matched if we were to just use :80.

| /usr/bin/wc -l

Next, we pipe the output of grep to wc, which is essentially a command line word counter.

The -l flag used simply tells wc to count the total number of objects passed to it. Since we already grep-ed the relevant ports, this would return either 0 if the service has died or 1 if it’s alive and listening.

| /bin/awk '{if ($1 == 0) system("/sbin/service httpd restart") }'

Following that, we pass the output sent out by wc to awk, a fast text processing engine that can be programmed to do perform simple text processing jobs.

Here, we simply want awk to tell us if the argument passed to it was 0 (the number zero). If this is the case, then run /sbin/service httpd restart (the command to start the httpd service).

To make the script functional, you should set the appropriate ownership and permissions to it. For example: chown root:root /scripts/svcschk; chmod 700 /scripts/svcschk. This will set the ownership of the script to root and make it executable only to this user.

You can then set it to run every minute by adding an entry to your crontab similar to this:

*/1 * * * *    /scripts/svcschk > /dev/null 2>&1

Hope this code snippet and the explanations are useful to you. If you have any questions on them, feel free to ask me by commenting below.

Can You Make Money From Linux?

The short answer is a resounding yes. Whether you work as a Linux server administrator, or do custom Linux installations, you can make a decent income from it.

Many people assume that just because Linux is a free open source software, there’s no money in it. How wrong these people are. Heck, almost every Linux kernel hacker is paid or sponsored in some way by reknowned IT companies for their work. Almost every IT company in the world has an interest in the Linux kernel. Of course there are others such as Microsoft and its cronies who fear the tremendous growth and stability of Linux and would do anything including spreading FUD about it.

I too have made a few thousand bucks from various Linux related projects including:

And probably a few others I can’t recall right now.

Back to the topic of this post, Can you make money from Linux? Of course! In fact, it’s easier than you think. Before going onto the things you need to do, let me cover some of the things you don’t need in order to make money from Linux:

  • Paper qualifications of any sort
  • A tremendously high IQ (heck, mine is only 147)
  • Any initial monetary investment (although investing in a few good books is recommended)

What you do need are:

  • The willingness to learn
  • Perseverence
  • A keen, observant and analytical approach to facing problems and solving them
  • Decent googling skills
  • Absence of shyness (you need to be willing to pimp your services)

Believe it or not, that’s all there is to it. Other than being profitable, Linux is fun and challenging. Heck, it beats problogging any time! 😉

I wanted to play around with PostgreSQL on the test server at work today, and found the slackpack over at LinuxPackages. I was very pleased that I wouldn’t have to compile PostgreSQL from source, as such complex apps will take more time than I would like to invest.

Anyway, once I’ve installed the package, and initialised the database, I decided to give phpPgAdmin a whirl on my freshly baked PostgreSQL installation, but when I try to use it I get an error message telling me that I have not compiled proper database support into my PHP installation.

Bargh, I checked out my phpinfo(); and sure enough, Slackware‘s package was not built with the --with-pgsql option.

Most of my googling results metion that the only solution is to recompile PHP from scratch, including the --with-pgsql flag. Hmm, I’d really hate to replace my original PHP installation which was done in the “proper” way, ie. via the available slackpack. Who know what the hell I can possibly misconfigure and break!

Anyway, here’s how I got my PostgreSQL to work with the default Slackware PHP installation:

  1. Download the PHP source code (be sure to use the PHP version that’s the same with your installation).
  2. Unpack the archive and go to the source directory (well, duh!).
  3. Run ./configure ---with-pgsql=shared,/usr (I know it’s too short, but just trust me on this, OK).
  4. Run make.
  5. Don’t run make install or checkinstall (if you’re using checkinstall, that is) like you usually would.
  6. Just copy <php source dir>/modules/ to /usr/lib/php/extensions.
  7. As root, edit your /etc/apache/php.ini file and look for a line saying
  8. Under this line type in: and save the file.
  9. Restart your apache service by running /etc/rc.d/rc.httpd restart (again, as root)to activate PostgreSQL support in PHP.

Hope this helps other Slackers as well as Linux users in general.

Slackware 10.2 Released

Slackware users rejoice! The long awaited Slackware 10.2 has been released. The official announcement can be read here. You might also want to check out the changelog.

You can get Slackware Linux 10.2 from The Slackware Store (official boxed set)BitTorrent or mirrorsites (ISOs).