Tag: script

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.

#!/bin/sh

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

PROCESSFILE='ExecutableFile.jar'
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.

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

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
then
do nothing
else
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…

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

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: , , , , ,

Log Monitoring Tools for Unix: Linux, Ubuntu, Red Hat and Solaris Servers

By Jacob Bowman

How to Easily Monitor and Alert on Unix Log Files…STRESS FREE!

Log Monitoring Tools for Linux and Solaris: Do you wish to monitor UNIX log file on a Linux or Sun Solaris System based on a time-frame (i.e. search for the occurrence of “pattern” in the log file within the last x amount of minutes)? If so, this article will save you hours and days of laborious work.

If you work in a UNIX environment, a time will come when you will wish to have caught a problem on one or more of your servers earlier than you did. When that time comes, you’ll scroll through your system or application logs and you’ll find a number of errors in there that speaks to an oncoming/developing problem.

You’ll wonder how you can monitor these logs in the future and alert yourself in the event of a problem. You’ll search the web for solutions and you’ll discover, to your dismay, that nothing out there is simple and straightforward enough to implement in your particular UNIX environment.

Then, you’ll want to write your own script. But alas, it is at this point that your mind will go blank. Because as you start to write your own log monitoring script, you will sadly discover that it really isn’t an easy thing to do. So you’ll begin to wonder in desperation, what do I do?

Well, I’ll tell you what you ought to do: Download or Write Your own ‘Smart Log Script’ and Keep it Simple!

What do I mean by that? Well, if you want to monitor a log file for errors/strings, and you’re concerned with efficiency, whatever script you write or download MUST follow the outline below. Emphasis on MUST!

For example, say you want to monitor the last x amount of minutes or hours of data in a particular log file for certain strings and alert if the strings are found, you MUST model your log monitoring script after the following tool:

Unix Log Tool: /bin/LogRobot (logfile-absolute-path) (time-in-minutes) ‘(string1)’ ‘(string2)’ (-found) (warn) (critical)

Example: ## /bin/LogRobot /var/log/messages 60 ‘luance’ ‘Err1310′ -found 5 10

So in this example,

/bin/LogRobot is the tool name.

/var/log/messages is the log file.

60 is the amount of previous minutes you want to search the log file for.

“luance” is one of the strings that is on the line of logs that you’re interested in.

“Err1310″ is another string on the same line that you expect to find the “luance” string on. Specifying these two strings (luance and Err1310) isolates and processes the lines you want a lot quicker, particularly if you’re dealing with a huge log file.

-found specifies what type of response you’ll get. By specifying -found, you’re saying if anything is found that matches the specified strings within the 60 minute time frame, then that should be regarded as a problem and outputted out.

5 specifies Warning. By specifying 5, you’re telling the program to alert as WARNING if there are at least 5 occurrences of the search strings you specified, in the log file within the last 60 minutes.

10 specifies Critical. By specifying 10, you’re telling the program to alert as CRITICAL if there are at least 10 occurrences of the search strings you specified, in the log file within the last 60 minutes.

Summarized Explanation:

As you can see, the LogRobot tool is monitoring a log file. The arguments that are passed to this log monitoring tool instructs it to do the following:

Within the last 60 minutes, if the tool finds less than 5 occurrences of the specified search strings in the log file, it WILL NOT alert. If the script finds at least 5 to 9 occurrences of the specified strings in the log, it’ll alert with a WARNING. If the script finds at least 10 or more occurrences of the strings in the log within the last 60 minutes, it’ll alert with a CRITICAL.

How easy is that? EXTREMELY!

For more information on how this tool works and to DOWNLOAD it for your own use, visit the following page:

http://www.jacobhtbowman.com/unix-monitoring-tools/unix-log-monitor-linux-sunos.htm

Tags: , , , , ,

Running Commands at Startup in Debian and Ubuntu – The Simplest Approach

By Austin White

Running custom scripts on startup is a common operation in the Linux community. In my case, when the machine hosting my website needs to be rebooted or even crashes, it is critical that the backend processes that the website depends on start correctly. For other Linux or BSD users, it can be useful to start up useful background processes, perhaps servers for accessing your machine remotely.

The Classic Method for Running Processes at Startup

The most documented way of starting processes when the machine boots is to add a control script to /etc/init.d. This script must take an argument that can be one of “stop,” “start,” and “restart.” An example of such a script would be /etc/init.d/ssh, which is used to start and stop the ssh server. When a machine shuts down, it is important for many daemons to clean up their pid files and otherwise shut down nicely. However, for user-run processes, simply being sent SIGTERM as part of normal shutdown is sufficient.

Here is an example of a script that is used only for starting a process.

$ cat /etc/init.d/boot_server

#!/bin/sh -e

case "$1" in

start)

/home/prod/start_server.sh

;;

stop)

;;

reload|restart|force-reload)

/home/prod/start_server.sh

;;

*)

echo "Usage: [this] {start|stop|restart|reload|force-reload}" >&2

exit 1

;;

esac

exit 0

To ensure that daemons are started and stopped, particularly in the correct order, the machine runs special symlinks to these scripts. The symlinks have special names that either begin with an S or a K. For example, my machine has /etc/rc3.d/S20lighttpd and /etc/rc0.d/K20lighttpd. (The numbers in the rc directory names are known as runlevels. A discussion of runlevels is beyond the scope of this article, and if you wish to know more, there are a number of good resources on the internet.) Scripts beginning with S are used to start a process during bootup. Those beginning with K are used to kill a process during shutdown. The number in the link name is used to determine the order in which these processes are started and killed.

Thus, to run a process at startup on your Linux machine, you would need to both add a script to /etc/init.d that takes “start” as an argument, and you would want to add symlinks to your script to the /etc/rc*.d directories. Your scripts have to follow the naming convention described above, probably starting with S99 or S98 to ensure that your processes start after all the important system daemons. The K symlink is unnecessary.

Using /etc/rc.local – A Better Way to Start Processes on Debian and Ubuntu

Instead of adding a startup script and the related symlinks, a much easier approach is to add your commands to the bash script /etc/rc.local. A quick look at /etc/rc.local demonstrates that it is rather self-explanatory.

$ cat /etc/rc.local

#!/bin/sh -e

#

# rc.local

#

# This script is executed at the end of each multiuser runlevel.

# Make sure that the script will "exit 0" on success or any other

# value on error.

#

# In order to enable or disable this script just change the execution

# bits.

#

# By default this script does nothing.

exit 0

At the end of /etc/rc.local, but before the exit 0 line, I can simply add a call to my server startup script:

# Run website processes

/home/prod/start_server.sh

It is a one-line change, instead of adding an overly complicated script and the related symlinks. Of course, this is not an option if you require additional commands to be run at shutdown. In addition, if you need your process to be started before some other system process, you must resort to the classic startup script as discussed above. /etc/rc.local almost the last script to be run as part of the boot process.

Conclusion: Use /etc/rc.local to Run Processes at Startup in Linux

Classic startup scripts in /etc/init.d and /etc/rc*.d are appropriate for many daemons and some more complicated user processes that must either start before a system process or be cleaned up during shutdown. However, /etc/rc.local is preferred for all other cases. It is a simple bash script you can edit as root on your machine.

Austin is a software engineer working on askR.com, a social recommendations site.

View his personal website.

Looking for exam help? Subscribe for testking 642-426 online training and get certified testking 1z0-051 study material including testking 642-566 Q &A for practice and pass the real test on first try.

Tags: , , ,

The Complete Steps to Create and Run a Linux Script to Run Linux Commands
By Clyde E. Boom

Creating and running a Linux script to automatically run a series of Linux commands that you commonly run is easy!

With a Linux script, you put a series of routinely run commands in a text file, and then run all of them by simply typing in the name of the file and pressing Enter.

Linux Script Example to Create a Script to Automatically Run A Series of Linux System Administration Commands2455513753_282aa586b7

Follow along with the steps in the example below to create and run your first Linux script!

1. Run a Linux text editor.

2. Put the following text at the top left of the text file (indented below for emphasis):

#!/bin/bash

This indicates that the text file is a Linux script file.

Press Enter twice to have a blank line below the line above.

3. Put the Linux command(s) in the script file.

The Linux commands below are used to provide an example. You can put any commands in a script.

The Linux commands below will: clear the screen, change into the /etc directory path, and then show the current path with the Linux pwd (path to working directory) command.

Then provide a long list of the fstab file (to show you that it’s there) and then change into your home directory (represented by the ~ symbol) and then show the path of the current directory.

The Linux echo command is not required, but has been put in the file to show the progress of the execution of the script.

Also, you don’t need to indent the commands below in the Linux script – they are just indented here for emphasis.

clear
echo The screen has been cleared

cd /etc

pwd

echo This is the etc directory

ls -l fstab

echo This is a long listing of the fstab file

cd ~

pwd echo Now in my home directory

Linux Commands Training Tips: A Linux script can contain hundreds of lines of text if necessary – and also include complex programming logic, such as if . . . then statements.

4. Save the text / script file with a meaningful name to create it and by give it a name.

For example, if you want to list files in a few directories, call the file: listdirs

5. Run the Linux chmod command to change the permissions of the file and make the Linux text file “executable”.

In our example, the file is named: listdirs

Below is a Linux chmod command example for running the chmod command to change the permissions of the Linux script file – and to make the listdirs text / script file “executable”, so that you can run the script file in the same way as you run a command.

The $ (dollar sign) below is the Linux command line prompt. Don’t type in the $ (dolar sign), type in the command that appears at the right of the $ prompt.

$ chmod u+x listdirs

The Linux command above is chmod and it is being used to assign the x (executable) permission to the u (user) of the file with: u+x and the script file name is listdirs.

Running a Linux Script to Run System Administration Commands

To run a Linux script (that is in the “current” directory), such as the listdirs script, simply type in a period (dot) and a space and then the name of the file and press Enter.

$ . listdirs

The concepts and Linux command examples shown above work in Red Hat, Ubuntu, Fedora, Slackware, and Debian Linux – and also ALL Linux distributions.

By the way…do you want to learn exactly how to use Linux and run Linux commands for Linux System Administration and get real, practical Linux training experience by running hundreds of examples of Linux commands?

Just click to download my free new Linux commands training course book and Linux audio podcast (.mp3) files here: Linux Commands Training Mini-Course

Clyde Boom says “Learn how to use Linux commands with easy, self-paced Linux training materials that show you how to run hundreds of examples of the essential Linux System Administration commands – and get that new and better job, promotion, raise – or keep your current job!”

You can get your instant access to my free Linux commands training course at:
http://www.LinuxCommandsTrainingCourse.com

Tags: , , , , , , , ,

Make a Linux Home Data Server of an Old PC

By Adam Knife

So, you’ve got an old computer, and you don’t know what to do with it. Sure, it can’t play new video games, maybe can’t run the latest and greatest software, don’t fret, it’s not totally worthless. Converting your old computer in to a home data server opens a range of possibilities, and a range of new things you can do with your computer(s) and the network.

So, how do you do it? Well, if it’s already set up for Windows Networking, you’ve got the basics set up for a Windows file server, and can simply use your local area network for transfering/working with files, however, this article is going to show you the more effective, and more powerful way: setting up a Linux file server.

The first step is to pick a Linux distro, DistroWatch.com lists the most popular distributions, and reviews a range of ubuntuimagesdistributions, we’re going to use the Ubuntu [5.10] operating system, with a server installation, simply because it’s the operating system this author uses for his desktop, and is quickly becoming the most popular distribution around.

Your old computer likely has enough memory, and a powerful enough CPU to run Ubuntu, however, if you intend to use this server as a major central file server, it will likely need a new hard-drive. You can deal with that on your own.

When you insert the Ubuntu CD, and boot to it, instead of just pressing [enter] at the boot screen, type ‘server’ then press enter – this will prevent it from installing any of the *-desktop packages, and not setting up any unneeded applications.

After following the steps of installation, you will be prompted with a logon screen – enter the username and password you provided during installation, and you are in your brand new Linux system. From here, you can do everything from browse the web, to set up the computer for various networking tasks, to play a range of Linux-based games.

Package management is a critical part of running a Linux system, luckily Ubuntu comes with two distinct and useful tools to aid in your package managing. Aptitude [which, is actually just a UI for apt-get] and apt-get.

A package called “samba” will allow you to set up proper networking between Linux and Windows computers (at least, we hope you’ve got your networking issues sorted out). Running “sudo apt-get install samba” in your new command line will tell the apt-get application to install the samba package, and set it up with default settings.

Once samba is installed, you’ll want to set it up to share certain files/directories, and set them up on your network – samba networking is a massive topic of it’s own, and way beyond the scope of this article, however, running “man samba” will give you the samba manual file, which lists off a series of other manuals to look at. Google’s always helpful too. :)

Now, once you have networking and samba set up, you should be able to transfer files between Windows and Linux through Network Neighborhood/smbclient – you’ve now got a basic data server set up. That was easy, wasn’t it?

For those who want to go further, Pure-FTPd will allow you to set up a fully featured FTP (file transfer protocol) server on this box, which you could use to access your files remotely from any computer set up with an FTP client (Windows Explorer has one built in!), setting up an Apache based web-server is fairly simple with Ubuntu’s apt-get packages, and OpenSSH allows the user to remotely log in to the Linux shell from any computer equipped with an SSH client.

A slight advancement to this system could allow you to set up Bash scripts combined with cron would allow you to set up scripts which immediately backup files every X days, or scripts to do certain processing to files at certain times – the possibilities are effectively endless.

Adan X. Knife is a computer scientist, entrepreneur and web developer. He currently runs a network of websites including one about High Definition Technology and a Free Games Library. He also runs a range of communication related sites including his cellular phone reviews site.

Tags: , , , , , ,
Back to top