Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"
Linux Magazine: The source for advanced Linux know-how

 Create an AccountHome | Submit News | Your Account  

Tutorial Menu
Linux Tutorial Home
Table of Contents
Up to --> Linux Tutorial

· Shells and Utilities
· The Shell
· The Search Path
· Directory Paths
· Shell Variables
· Permissions
· Regular Expressions and Metacharacters
· Quotes
· Pipes and Redirection
· Interpreting the Command
· Different Kinds of Shells
· Command Line Editing
· Functions
· Job Control
· Aliases
· A Few More Constructs
· The C-Shell
· Commonly Used Utilities
· Looking for Files
· Looking Through Files
· Basic Shell Scripting
· Managing Scripts
· Shell Odds and Ends

Glossary
MoreInfo
Man Pages
Linux Topics
Test Your Knowledge

Site Menu
Site Map
FAQ
Copyright Info
Terms of Use
Privacy Info
Disclaimer
WorkBoard
Thanks
Donations
Advertising
Masthead / Impressum
Your Account

Communication
Feedback
Forums
Private Messages
Recommend Us
Surveys

Features
HOWTOs
News
News Archive
Submit News
Topics
User Articles
Web Links

Google
Google


The Web
linux-tutorial.info

Who's Online
There are currently, 210 guest(s) and 4 member(s) that are online.

You are an Anonymous user. You can register for free by clicking here

  
Linux Tutorial - Shells and Utilities - Managing Scripts
  Basic Shell Scripting ---- Shell Odds and Ends  


Managing Scripts

A very common use of shell scripts that you write is to automate work. If you need to run the command by hand each time, it often defeats the intent of the automation. Therefore, it is also very common that commands are started from cron.

As Murphy's Law would have it, sometimes something will prevent the script from ending. However, each time cron starts, a new process is started, so you end up with dozens, if not hundreds of processes. Depending on the script, this could have a dramatic effect on the performance of your system. The solution is to make sure that the process can only start once, or if it is already running, you want to stop any previous instances.

So, the first question is how to figure out what processes are running, which is something we go into details about in another section. In short, you can use the ps command to see what processes are running:

Note that when you run this command, it will also appear in the process table. Since your process name is an argument to the grep command, grep ends up finding itself. The grep -v grep says to skip entries that containing the word "grep" which means you do not find the command you just issued. Assuming that the script is only started from cron, the only entries found will be those started by cron. If the return code of the command is 1, you know the process is running (or at least grep found a match.)

In your script, you check for the return code and if it is 1, the script exits, otherwise it does the intended work. Alternatively, you can make the assumption that if it is still running, there is a problem and you want to kill the process. You could use ps, grep, and awk to get the PID of that processes (or even multiple processes). However, it is a lot easier using the pidof command. You end up with something like this:

The problem with that is the danger of killing a process that you hadn't intended. Therefore, you need to be sure that you kill the correct process. This is done by storing the PID of the process in a file and then checking for the existence of that file each time your scripts starts. If the file does not exist, it is assumed the process is not running, so the very next thing the script does is create the PID file. This could be done like this:

This is already done by many system processes and typically these files are stored in /var/run and have the ending .pid. Therefore, the file containing the PID of your HTTP server is /var/run/httpd.pid. You can then be sure you get the right process with a command like this:

Note that in your script, you should first check for the existence of the PID file before you try to kill the process. If the process does not exist, but the PID file does, maybe the process died. Depending on how long ago the process died, it is possible that the PID has been re-used and now belongs to a completely different process. So as an added safety measure you could verify that the PID belongs to the correct process.

To get some ideas on how existing scripts manage processes take a look at the init scripts in /etc/rc.d.

Details on if-then constructs in scripts can be found here.
Details on using back-quotes can be found here.
Details on file redirection can be found here.

 Previous Page
Basic Shell Scripting
  Back to Top
Table of Contents
Next Page 
Shell Odds and Ends


MoreInfo

Test Your Knowledge

User Comments:


You can only add comments if you are logged in.

Copyright 1997-2004 by James Mohr. Licensed under modified GNU Free Documentation License. See here for details. All rights reserved.
  
Show your Support for the Linux Tutorial

Purchase one of the products from our new online shop. For each product you purchase, the Linux Tutorial gets a portion of the proceeds to help keep us going.


Login
Nickname

Password

Security Code
Security Code
Type Security Code


Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Help if you can!


Amazon Wish List

Did You Know?
You can get all the latest Site and Linux news by checking out our news page.


Friends



Tell a Friend About Us

Bookmark and Share



Web site powered by PHP-Nuke

Is this information useful? At the very least you can help by spreading the word to your favorite newsgroups, mailing lists and forums.
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Articles are the property of their respective owners. Unless otherwise stated in the body of the article, article content (C) 1994-2013 by James Mohr. All rights reserved. The stylized page/paper, as well as the terms "The Linux Tutorial", "The Linux Server Tutorial", "The Linux Knowledge Base and Tutorial" and "The place where you learn Linux" are service marks of James Mohr. All rights reserved.
The Linux Knowledge Base and Tutorial may contain links to sites on the Internet, which are owned and operated by third parties. The Linux Tutorial is not responsible for the content of any such third-party site. By viewing/utilizing this web site, you have agreed to our disclaimer, terms of use and privacy policy. Use of automated download software ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and are therefore expressly prohibited. For more details on this, take a look here

PHP-Nuke Copyright © 2004 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.08 Seconds