Tag: shell

By Shamly Faleel

I always run linux applications manually using the terminal during the development stage. This has helped me immensely as I could see all the messages that were printed on to the linux console. I also had the habit of forgetting to start the Java program once the linux machine reboots. Well how many of you have had that experience? Ohh yes, I forgot to mention that I will be using a compiled java program as an example executable file for this tutorial.

Though this is acceptable for a system that is under development, it is not suitable to run an executable program using a terminal in production environment. So I would go about setting up a cron job to check if the process is running and start it up if it is not, using a shell script. This tutorial will give you an understanding of how to check for a running processes and start the process if not running using a shell script in linux.

Please note that shell script is not the only way to check for a running process or to start a process.

The Shell Script:

So let’s name the file testshellscript.sh

You can use any editor like vi to write shell script.


This is the first line of the shell script. It tells the script which interpreter to refer to. Next let us check for the running process

if ps ax | grep -v grep | grep $PROCESSFILE > /dev/null

I am assigning the jar file name to a variable named PROCESSFILE and then using the linux command ps to check if the process is running.

echo "$PROCESSFILE is running, everything is fine"
echo "$PROCESSFILE is not running"
#start the process
/usr/local/jdk/bin/java -jar /opt/$PROCESSFILE > /dev/null 2>&1 &

If there is a running process, we need not do anything to it. So I am simply echoing a message saying that the Java program is running. And if the Java program is not running, I will attempt to start the Java program. The logic of the conditional statement is as follows:

if process running
do nothing
start the process / java program
end if

As the example executable file is a jar file I am using bin\java to execute the file. This is almost the same command used in a linux terminal. Only difference is the use of full paths. Also all the outputs are directed to the null device which is a special file that discards all data written to it.

Here is the complete code…

if ps ax | grep -v grep | grep $PROCESSFILE > /dev/null
echo "$PROESSFILE is running, everything is fine"
echo "$PROCESSFILE is not running"
#start the process
/usr/local/jdk/bin/java -jar /opt/$PROCESSFILE > /dev/null 2>&1 &

Author: Shamly Faleel is a PMP certified IT Consultant, who has over 9 years overall experience in the IT Industry. Check out his blog at http://www.shamly.com/

Tags: , , ,

By Rand Whitehall

If you are just starting out using the command line in Linux, knowing just a few key commands can take you a long way towards fluency in the cli (command line interface) environment. Most new linux users fear the dreaded command line and its archane text-based commands. But if you warm up to the command line slowly and patiently, you’ll start to see you can wield great power over your computer using cli tools. It is truly amazing how much you can do with the cli and how quickly and efficiently it can do things. Some tasks can be done with a few lines of commands in under a second that would take thousands of mouse clicks and hours to accomplish in a typical GUI (graphical user interface).

The ls command is very powerful and useful so we’ll start there, then take a look at the file command.

ls: The ls command lists the contents of a directory. Simply type ls and hit the enter key. You should see a list of the contents of your current directory.

Now, if you want to get a bit fancy, you can tell ls to show you file sizes as well by adding the -l switch like this: ls -l.

This will show you the contents of the directory and files size, file date, file time and file name.

Now, while you are looking at details of the files in your current directory, you may be wondering what type of files they are. For example, if my home directory had a file called joe, I would type file joe to find out what type of file it was.

The file named joe is a text file so the file command outputs: joe.txt UTF-8 Unicode English text.

I’ve also got a directory listed called bob. If I type file: bob, then file outputs: bob/ directory.

The file command can tell us about any type of file. Digging deeper into my filesystem I come across a file that I don’t know. What type of file is it? Run the file command. file: bak.sh. The output of the file command: Bourne-Again shell script text executable. Ahh, it’s a bash script.

What’s a bash script? It’s a list of cli commands strung together in a text file. A bash script is like a small program that can perform just about any number of tasks. Very powerful stuff once you get a few more commands under your belt.

Rand writes about Linux, old school safety razors and blue nitrile gloves! Check out Rand’s site all about latex free nitrile gloves, a great barrier against infection. Black Nitrile Gloves are also available and used often by tattoo artists, mechanics and industries where keeping dirt and grime free is important.

Tags: , , , , ,

Important Unix Commands That You Should Know

By Bernard Peh

Unix is one of the most important operating system today. Its powerful features, scalability, strong security, and support for multiple users have made it the top choice operating systems for server, workstations and mainframes.

It is good to have some knowledge of Unix commands especially if your web host is Unix/Linux based. You could do alot of things by yourself by logging into the server via Secure Shell(SSH). This article will describe some of the important “need to know” Unix commands.

1. ls

This command will show you what files are in your current directory. If you add in a -F option, ie “ls -F xxx”, there will be a “/” appended to the end of directory names, * to executables and @ to links. “ls -a xxx” will display all hidden files as well. This is also the most used command.

2. cd

Change directory. If you type “cd xx”, it means to change to the specified directory “xx”. “cd ~” means to change to your default home directory.

3. cp “a b”

Copy file a to b. If b is a directory, the new file will be named b/a.

4. mv “a b”

Move files from a to b. For example, if I type “mv songs.txt /tmp”, the file songs.txt will be moved to /tmp/songs.txt. Moving a file is the same as renaming a file.

5. echo “text”

Print “text” to the terminal. If “text” is surrounded by double quotes, the text will be printed with any environment variables such as $HOME. If “text” is surrounded by single quotes, the “text” is printed without any special processing.

6. pwd

Print the current working directory. Useful command when you are lost in the directories.

7. cat “file”

Print the contents of the specified file(s) to the terminal.

8. less “file”

Display the specified file one screen at a time. Press the spacebar to go to the next screen. Press Q to quit. You often combine “less” with some other commands such as “cat abc | less”. This command means you print the contents of the file abc and display it one page at a time.

9. ps

Display information about your running programs. This is a good command to use if your server is slow and you suspect that some applications are taking too much memory. The most famous command using ps is “ps aux”. This will display useful information on the running programs.

10. rm

Remove or delete a file. If you type “rm -r directory”, it will remove a directory and all the files underneath it recursively.

11. man

This is the most important command. man means “manual”. If you are stuck with cat command for example, type “man cat” and you can see the help file.

Bernard Peh is a great passioner of web technologies and one of the co-founders of SiteCritic Website Reviews. He works with experienced web designers and developers for more than 5 years, developing and designing commercial and non-commercial websites. During his free time, he does website reviews,freelance SEO and PHP work.

Tags: , , , , , , , , , , ,

By Erick M Aqqa

Unix based operating systems like Linux offer a unique approach to join two discrete commands, and generate a new command using the concept of pipe(lines). For example, consider command1|command2. Here, whatever output is generated by the first command becomes the standard input for the second command. We can develop more and more complex Unix command sequences by joining many commands while maintaining input output relationships.

Another more Linux specific example would be ls -l|grep “^d”. This command displays details of only directories of the current working directory, i.e. the output of the ‘ls -l’ command becomes the input to the grep command, which displays only those lines that start with ‘d’ (they are nothing but the details of the files).

ls -l | grep “^d” | wc -l

This command displays number of directories in the given file.

grep “bash$/ etc / passwd | wc -l

This command displays number of users of the machine whose default shell is bash.

cut -t “: “-f 3 / etc / passwd | sort – n | tail – l

This command displays a number which is the largest used UID number in the system. Here, cut command first extracts UID’s of all the users in the system from the /etc / passwd file, and the same becomes input to sort; which sorts these numbers in numerical order and sends to tail command as input which in turn displays the largest number (last one).

tee command

The ‘tee’ command is used to save intermediate results in a piping sequence. It accepts a set of filenames as arguments and sends its standard input to all these files while giving the same as standard output. Thus, use of this in piping sequence will not break up the pipe.

For example, if you want to save the details of the directories of the current working directory while knowing their using the above piping sequence we can use tee as follows. Here, the file xyz will have the details of the directories stored.

ls -l | grep “^d” |tee xyz | wc -l

The following piping sequence writes the number of directories into the file pqr while displaying the name on the screen.

ls -l | grep “^d” | tee xyz | wc -l |tee pqr

cmp command

The cmp utility compares two files of any type and writes the results to the standard output. By default, cmp is silent if the files are the same. If they differ, the byte and line number at which the first difference occurred is reported.

Bytes and lines are numbered beginning with one.

For example, cmp file1 file2

comm command

comm is a command used to compare two sorted files line by line.

Compare sorted files LEFT_FILE and RIGHT_FILE line by line.

-1 suppresses lines that are unique to the left file.

-2 suppress files that are unique to the right file.

-3 suppress lines that appear in both the left file and the right file. For example, comm p1 p2.

A pipe thus helps connect a set of processes, so that the output of one becomes the input of another. It lets a user browse through a large amount of data in a convenient manner.

Linux2Aix is an upbeat Linux blog containing all the latest and the newest Linus news and how-to’s for both amateur and professional Linux lovers

Tags: , , , , , , , , , , , , ,

An Introduction to Linux Shell Commands

By Erick M Aqqa

The Linux system often intimidates beginners, what with the need to type in a command or two every now and then. However, Linux commands (or shell commands, to put it more accurately) follow a rather logical and sensible pattern and all you need to do is get used to it! Most shell commands are roughly 3, or at most 4 letters long. The more frequently needed commands are shortened further usually.

The man command is a reference to ‘manual’, i.e. the documentation that comes preloaded in Linux systems to help you with common problems faced by a user. These man(ual) pages are certainly not meant to be standalone guides for beginners, but instead, they need to be used as reference material to corroborate what you’ve learnt/tried elsewhere.

The info command is another way to look up reference material for GNU information and troubleshooting suggestions. It isn’t completely the same as the man pages, as there is a scope for using ‘hyperlinks’, to make it easier to browse through the material you need. There are shortcuts for scrolling through pages too.

The date command is self-explanatory. It will tell you about the current time and date on the system. On those lines, the cal command will display a neat calendar of the current month (or that of any particular one you choose).

The ls command is meant to help you list the contents of the directory you choose. This means that you will get to see all the files and directories inside your current directory.This command also has switches to modify its behavior and functionality. For example, adding -l after ls will give you a more detailed description of the contents of the directory you’re in. you’ll get to see details about file permissions, modification dates, groups, sizes and owners. The -a switch helps you see all the files in your current directory, including the hidden ones. (Hidden files in Linux have their filenames starting with a ‘.’ i.e. a period)

In case you lose track of the current directory you are in, type pwd, which prints the current directory you are working in. Once you’ve got to know that, you can navigate around the Linux file system using the cd command, where ‘cd’ stands for ‘change directory’. This can work on both relative and absolute terms, i.e., you can work by specifying the entire directory, starting from ‘/’ (the home directory) to the directory/file you want to go to. Otherwise, you can also use the ‘.’ or ‘..’ system. The latter tells the compiler that you want to go to the parent directory of your current one, making it an easy alternative to typing the entire path every time.

When you start working with simple files, like text files, the cat command will come in handy. It ‘concatenates’ the content of one or more text files, letting you see all of the text they contain.

The ps command lists all the processes currently running on your system in your current terminal.

The shell commands in this article are a good way to start off in the Linux environment. They are simple enough for you to get an idea of how the entire system works, and will probably help you to try out more advanced steps later!

To know more about Linux shell commands, click here

Linux2Aix is an upbeat Linux blog containing all the latest and the newest Linus news and how-to’s for both amateur and professional Linux lovers

Tags: , , , , , , ,

Basic Linux For Ease of Use and Management of a Hosted Website – Getting Started!
By Mike Tremell

Welcome to the beginning of a path to simpler website administration!

This article is the first in a series aimed at the average user with the average hosted website; someone without a vast 180px-KN-Servers2amount of hyper-technical knowledge about the guts of the internet. We will see that utilizing the more “complicated” part of the hosting provider’s service can be easy and can make your website management easier as well. I will provide you with a basic road map to simple Linux utilization that, I believe, will both benefit, please and, ultimately, empower you to better manage and amplify your site’s potential!

As we all know, with the proliferation of user-hosted websites in our age, the market has driven most hosting services to provide users with more user-friendly interfaces; windows, or browser, based menuing systems with simple and easy to understand layouts.

This is a good thing, but it’s also a bad thing. One could make the argument that the simplification of hosted website management through the use of these interfaces makes it possible for the average person to easily set up and manage their own website or e-business. This is true. In fact, it’s one of the main reasons that the web hosting industry is experiencing a boom. Consumers demand ease-of-use from merchants and simple picture-menu based setup and maintenance systems fulfill that demand.

So how can this be a bad thing?

There are a multitude of reasons, but the one I’ll be addressing here is, coincidentally, the exact same thing: Ease Of Use! What most people don’t realize at first (but soon become aware of) is that the “Ease Of Use” supplied by these point-and-click-simple solutions, more often than not, make a lot of simple tasks extremely difficult, if not impossible!

The double-whammy is that they essentially “mystify” the underlying operating system (that place where the work actually gets done) and lock most people into a cycle of endless forum-searching or email-tag with customer support (and experienced Linux users) where they find, mostly, answers that assume they already possess whatever basic knowledge they need to complete the task at hand. I’m not saying that any of these folks are unfriendly or unwilling to help, just that the average user ends up no better off in the end and has to go back to… the windows-like menuing system.

The good news is: You don’t have to settle!

Given the right circumstances, you can enhance your ability to maximize the potential of your website, and, ironically, increase your “Ease Of Use” by making use of that underlying operating system.

By learning just a few of the basics of Linux (the most popular operating system for most web-hosting solution providers), you can dramatically increase the ease with which you can maintain, and even optimize, your website.

Below, we’ll go Point-by-Point, with “Tips” along the way, toward adding use of the Linux operating system to your bag of tricks. The de-mystification begins!

Point Number 1: Don’t let any roadblocks you hit discourage you. What you’re about to accomplish is within the realm of absolute possibility. You CAN use Linux to maintain your website, enhance it, optimize it and much more. None of this is beyond your ability. Societally perpetuated self-doubt, mass-belief that it’s all just too complicated and the pervasive notion that Linux is comprehensible only to computer science majors and “techies” are myth’s and falsehoods.

You can master Linux as easily as you can master your ABC’s. If that seems like over-simplification, believe me when I tell you that it’s not. Remember how much easier it was to remember LMNOP than it was the rest of the alphabet? In simplistic terms, you’ll be learning how to help yourself by learning how to use Linux to your advantage in much the same way. We’ll start with the parts that come easily, and the rest will fall into place over time, seemingly without effort!

Point Number 2: In order to make any of this work, your service provider will have to offer you the option of using a “shell account.” This is the most common terminology for direct access to the underlying operating system. “Shell access” is also commonly understood. If you are already being hosted, be sure to use these terms when requesting the access you’ll need. All service providers understand what a “shell account,” or “shell access” is and will be able to let you know, immediately, whether or not they offer their users that option.

Tip: If you are in the consideration phase, and looking for a hosting provider, be sure to ask them if they offer you the option of a “shell account,” or “shell access.” It’s your call in the end, but, if they don’t, I would advise that you continue your search for a provider elsewhere.

Tip: Most providers offer “shell accounts,” or “shell access,” but they don’t make a point of letting you know. In my experience, it’s never anywhere near the top of the list of features the hosting provider offers, and, most times, you have to go to the support page, or elsewhere, to find out. You just need to ask. If it’s something they offer, they’ll give it to you (however grudgingly); usually with simple login instructions. Hosting providers generally don’t like the thought of “regular” users mucking about with the underlying operating system, so they generally don’t make it a point to let you know you have this option!

Point Number 3: Now you’re going to need to get to your shell account. This is a piece of the puzzle that most service providers will assume you know how to do. We’ll assume for the moment that you don’t. For our purposes here we’ll assume your provider is a company named XYZ.com and you already have access to the internet and have that connection active when you connect to your “shell account.”

There are certain things that you’re going to need in order to access your shell account; all of which are free. Some you’ll have to get yourself and some you’ll most likely have to request from your service provider.

Most importantly, you’ll need your connection information. You will get this from your service provider. It should include:

1. The host name or IP address of the server you’ll need to connect to in order to access your account (e.g. webhost.XYZ.com or somecrazyname.XYZ.com). You’ll almost never be given an IP address exclusively. Your service provider should, however, include this information along with the server name (e.g. webhost.XYZ.com – IP Address: Having an IP address to connect to can be advantageous if, for some reason, you can’t reach the server via the host name.

2. Your login information. This will simply be a user name and password.

3. The method by which you can access your shell account. Generally this will be via “SSH” (Secure Shell), but some hosts still use “Telnet” (Telecommunication Network

‘t let the definitions I’ve included in parentheses put you off. They’re simply provided for completeness and shouldn’t concern you at this point. Their strict “definitions” may never ever concern you – They don’t concern me and I’ve been in the business for well over a decade!

Basically, the difference between the two connection methods is academic. Telnet sends information over the internet as-it-is. This is one of the reasons most providers use SSH. SSH sends information over the internet in “encrypted” form. That is, Telnet is an “unsecure” protocol, while SSH is considered “secure.” It’s much harder (if not nearly impossible) for someone to hack into your connection and “see” what you’re typing if the information is encrypted. Encrypted information is protected. Unencrypted information (what you’ll be sending if you use Telnet) can be read (by the proper hacker) as if he or she were looking over your shoulder watching you type!

Tip: Don’t accept Telnet if it’s offered as a connection option. Insist on SSH. If SSH is not available from your provider, there are other options you can pursue, but they’re beyond the scope of this article.

Next, you’ll need a method by which to connect, using the information given to you by your hosting provider. This is simply going to be some software “client” that you’ll use to connect. Many SSH clients are available for free and can be downloaded at various freeware sites on the internet (Use any search engine and simply type in a search for “SSH client freeware download.” You’ll be surprised at the number of options available!)

Tip: Don’t pay for an SSH client unless it makes you feel better. There are several reputable and highly effective clients available for free. Almost all work right out of the box (just start them up and look for a button that says “new connection,” or something similar, and then you’ll be presented with a screen into which you can type in the host name, user name and password information you received from your hosting provider. Just click connect and you’re logging into your “shell account!”

Point Number 4: Now, strangely enough (with most providers), you’ll be presented with a “menu screen” once you login to your “shell account.” This will generally provide you with several options such as editing files, sending email, uploading or downloading files, etc.

A text-based menu is generally fairly easy to follow. Options are presented on a numbered menu (possibly with letter shortcuts in parentheses alongside), you select the number (or letter) of the option you want to use and then you do whatever that is until you exit and come back to the menu. A simple menu might look like this:

—- Welcome to XYZ.com Shell Access Menu —–

1. Edit Files (e)

2. Send Email (s)

3. Upload Files (u)

4. Download Files (d)

5. Linux Shell (l)

6. Quit (q)

Enter your option

Tip: If you use any option and it isn’t made obvious how you can get back to the menuing system, you can generally get back there by “killing” whatever program you’ve launched by selecting your option. This can usually be done by typing one of the following “control-character” sequences. ctl+c, ctl+x, ctl+v, ctl+d. The key combinations described here are simply the typing of two keys at once (denoted by the + symbol), so for ctl+c you would type the “control” key (usually “ctl” or “ctrl” on your keyboard) while simultaneously typing the “c” key. Just type them both at the same time. Nothing to it!

Please note that all of these options may disconnect you completely from your server and should be used only after you’ve saved any work you’re doing.

Not to worry; if you do get disconnected, all you need to do is connect again. Of course, any and/or all of these options may do nothing at all. If you just “need” to disconnect and can find no remedy in your “shell account,” you can always take the guaranteed step of closing your SSH client.

Now you have arrived!

This part of the lesson is coming to an end, but your journey has just begun. At this point, fool around with the various menu options and try out the various features of your “shell account.” Use them with caution, as you would when interfacing with your window-based menuing system. Try to keep your actions non-destructive (e.g. If you’re going to edit a file in a foreign editor, make sure to back it up, or copy if off, first, etc).

If your hosting provider’s “shell account” is literally that, you’ll end up at a “shell prompt” after connecting. Take some time to investigate. For now, stick to using “info,” “help” or “man” (for manual) commands to learn about your environment. You’ll know you’re at a shell prompt immediately. It may look something like this (But, there’s no mistaking it for a menu!)

/home/user/public_html >_

Practice with the skills you’ve gained so far. You’ll find, with time that they will become second nature. Of course, we’re only part of the way there now, but, as this article is a “Beginning,” you have accomplished your goal.

Remember, with practice and patience, you can learn a thing or two about Linux as you explore your new environment. Be cautious, but have fun. It’s one of the best ways to learn!

“Ease Of Use” in managing your website will take on a new meaning for you as we continue. Hopefully, it has already!

If you’re already slightly farther ahead in the game, or would simply like to take some steps to get your existing, or in-progress site notices, my website might be worth your while to check out.
But, that may be for later.

Mike Tremell – (c) 2007

If you have any questions about this article, or you’d like to be kept informed of updates to this series, please feel free to email me and I will include you in a mailing to announce future updates. Please also note that I respect your privacy and your email will not be resold or used for any other purpose; I do not send out unsolicited email or do any bulk email marketing.

Having trouble passing computer certification test? Look no further than this ebook on Computer Certification Success [http://www.web-advantage.org]!

Tags: , , , , ,

Important Concepts For Linux Beginners – Shells and Utilities
By Levi Reiss

A shell is the command interpreter program that serves as an interface between some users and 250px-Bash_demothe operating system itself. We say some users because most users rely on the graphical user interface. The Windows shell is the DOS command line interface accessed by clicking on Run and then entering the cmd command. The Windows graphical user interface is Explorer. This article describes the Damn Small Linux shell interface and several utilities, useful programs that may be launched from the shell. A subsequent article will describe the corresponding graphical user interface.

Why would anyone want to bother with a shell when the prettier, easier-to-learn and easier-to-use graphical interface is available? The answer is: It depends who you are and what you want to do. For system administrators or their associates it’s often much less cumbersome to use the shell rather than the graphical user interface. While Damn Small Linux commands may be quite arcane, they are often very powerful. And efficient. The Linux way of performing administrative and other technical tasks admittedly takes time to learn and master. But it does the job and does it well. In all fairness, many Windows systems administrators often apply command-line utilities. But they don’t have a powerful shell to help them do their work.

Historically Unix used the Bourne shell, the C shell which resembles the C programming language, and the Korn shell. Linux’s most widely used shell is Bash, also spelled BASH, the (Bourne-Again Shell). Damn Small Linux offers many shells but most people go with Bash both to communicate interactively with the operating system and to write programs known as shell scripts. If you program in Linux no matter which programming language you use you should learn some Bash specifics.

Utilities enable you to handle some very sophisticated processing. You can think of them as commands or as prewritten programs. Unix-Linux people often send the output of one command or utility to another command or utility for further processing. For example, the ps command displays active processes. It tends to generate voluminous output, especially in a busy system. Let’s say that you are interested only in the processes associated with a given terminal. You send (the technical term is pipe, expressed by the | character) the output of the ps command to the grep utility which looks for patterns within the input. You code a single line, multipart command to obtain the list of processes associated with that particular terminal. Unix and Linux are well known for elegant solutions. In contrast the Windows solution to this information need is much more clumsy.

The grep utility has many other uses including validating e-mail addresses. Let’s say that your web site asks potential subscribers to furnish their e-mail accounts when signing up for a newsletter. A sophisticated but relatively short statement coded in grep could validate e-mail accounts.

DSL-logoOther Damn Small Linux text processing utilities include the related egrep and fgrep commands, mawk a pattern scanning and text processing language, sed an editor that handles large files, and diff a utility that compares files. DSL provides utilities that compress and archive files, and a wide range of other utilities. If you need them, these Linux utilities can be very useful and time-saving.

Our next subject is Linux programming support.

Levi Reiss has authored or co-authored ten books on computers and the Internet. He loves the occasional glass of wine as exemplified by his wine websites including http://www.theworldwidewine.com. He teaches Linux and Windows operating systems plus other computer courses at an Ontario French-language community college. Visit his new website http://www.linux4windows.com which teaches you how to download and run Damn Small Linux on Windows computers, even if they are “obsolete.”

Tags: , , , , , , , , ,
Back to top