Need help making a start/stop script... | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Need help making a start/stop script...

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-23-2009
phpfreak phpfreak is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 23 December 2010, 9:13 PM EST
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Need help making a start/stop script...

i have two separate scripts that work nicely to curl and generate two files.. one html and one txt so a total of four.

When the script starts up i want it to:
call and run shellscripta
call and run shellscriptb
sleep for about 40 seconds
again run shellscripta
again run shellscriptb
check and make sure both txta and txtb exist then..
check for any differences..
if they are the same than continue loop/sleep
if the two text files contain different data
email user@somewhere.com with the data of both files then continue loop
--loop stops when i run 'xcommand stop'


There are two parts of this that's difficult for me.. one is creating a loop that starts and stops when i tell it.. the other is the portion that checks for differences then emails.. the script has to 'head -n4' both text files to see if there are any differences.. if there are i need it to 'mail' me both parts of the text files 'head -n5' (5th line containing the date so i know what's oldvsnew).. i hope i'm making some sense but any help would be great
Sponsored Links
    #2  
Old 10-24-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Advisor  
The Tutor
 
Join Date: Jun 2008
Last Activity: 9 April 2014, 1:31 AM EDT
Location: INDIA, Bangalore
Posts: 2,049
Thanks: 16
Thanked 106 Times in 102 Posts
what have you tried till now??
Sponsored Links
    #3  
Old 10-24-2009
phpfreak phpfreak is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 23 December 2010, 9:13 PM EST
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
It would be something similar to a initd startup script..


Code:
  #!/bin/sh
  
  case "$1" in
    start)
      #Not entirely sure of how I would loop this sequence w/o cron
      #Also, on second try and consecutive thereafter it needs to 
      #first check to make sure that at least two txt files reside
      #in the current directory.. I haven't implemented this yet here since
      #i'm not sure of how it would loop
        
        ./page4mod0.sh &&;
	./page4mod99.sh &&;
        sleep 45;
        ;;
     #Not sure how to approach this part.. i could have it diff'd then compared in a temp file
     #or throw them into variables then compared.. think i'd prefer memory here though so let's try..
          A=`head -n4 pagemod.txt`
          B=`head -n4 pagemod0.txt`
           if [ "$A" = "$B" ]
           then #Do nothing
           echo >dev/null
           else 
          C=`head -n5 pagemod.txt`
          D=`head -n5 pagemod0.txt`
           mail@user@home.com -f `echo "$C -----  $D" > mailer.txt ;cat pagemod.txt >> mailer.txt ; cat pagemod0.txt >> mailer.txt`
          fi
        ;;
    stop)
        echo -n "Shutdown and cleanup"
        kill -9 page4mod0.sh;
        kill -9 page4mod0.sh;
	rm *.html *.txt;
        ;;
    restart)
        #This isn't entirely necessary now but it would be nice if i could tell it
	#to run the stop sequence.. wait.. then start it back up again.. later

    status)
        # Again not entirely necessary but just check for process other than 
	#the current and report pid
        ;;
    *)
        ## If no parameters are given, print which are avaiable.
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
        ;;
esac


Last edited by phpfreak; 10-24-2009 at 09:34 PM..
    #4  
Old 10-25-2009
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 2 September 2014, 12:03 AM EDT
Location: Amsterdam
Posts: 9,386
Thanks: 273
Thanked 2,349 Times in 2,108 Posts
Hi phpfreak,

Here are a couple of off-the-cuff comments :
  • you need a PATH statement at the start of your script with the directories of all the external programs you are going to call from your script
  • call your own scripts and files you manipulate using absolute paths, not relative paths
  • depending on your intention either:
    - use & instead of && for background processing followed by a wait statement after the sleep statement
    - use && operator to start processes only when previous command succeeds but then loose the semicolons
  • do not use kill -9 unless you have to . Use kill first and then check. If not successful use other kill options or move directly on to kill -9.
  • use a colon ( : ) as an idle statement instead of echo>/dev/null or use [ "$A" != "$B" ]
  • You might want to brush up the mail command
  • You can not kill processes, you need process id's (pids). Either save those at the start of a process in a tmp file or distill it from ps -ef
  • You probably want to put all the statements either in a wrapper script or a function that contains the 45 second loop with the script calls, the comparison and the mail section and that you start perpetually in the background using:
    nohup /path/to/wrapperscript &
    The stop start script then just stops/starts/status checks that wrapper script
  • you can create a perpetual loop using

    Code:
    while : ; do
      your statements
    done

  • for the restart option you can use:

    Code:
    $0 stop; $0 start

  • Use exit statements to indicate error levels
  • Enough for now, have fun!

    Last edited by Scrutinizer; 10-25-2009 at 05:41 AM..
    Sponsored Links
        #5  
    Old 10-25-2009
    phpfreak phpfreak is offline
    Registered User
     
    Join Date: Feb 2008
    Last Activity: 23 December 2010, 9:13 PM EST
    Posts: 54
    Thanks: 0
    Thanked 0 Times in 0 Posts
    Bug

    This is what i've got so far..


    Code:
    #!/bin/sh
      
      case "$1" in
        start)
    	while : ; do
                ./pager.sh &&
                sleep 30
                ./pager99.sh &&
    
                    if [ -f "pagemod.txt" ]
                    then
                        :
                    else
                        echo "The pagemod text was not generated during loop!"
                    fi
                    if [ -f "pagemod0.txt" ]
                    then
                        :
                    else
                        echo "The pagemod0 text was not generated during loop!"
                    fi
    				#COMPARE TXTS AND DETERMINE ACTION
    				A=`cat pagemod.txt | head -1`                                                                        
    				B=`cat pagemod.txt | head -2 | tail -1`                                                        
    				C=`cat pagemod.txt | head -3 | tail -1`                                                                
    				D=`cat pagemod0.txt | head -1`                                                                
    				E=`cat pagemod0.txt | head -2 | tail -1`                                                        
    				F=`cat pagemod0.txt | head -3 | tail -1`                                                        
    				if                                                                                
    					test $D -eq $A                                                                                        
    					test $E -eq $B                                                                                        
    					test $F -eq $C                                                                                        
    				then                                        
    					:                                                
    				else    
    					cp page0.html page0BAK.html
    					cp page99.html page99BAK.html
    					cp pagemod.txt pagemodBAK.txt
    					cp pagemod0.txt pagemod0BAK.txt
    					echo 	"Pager Report
    							unimportant texts and links" >pageralert
    					mail -s "Alert! Pager Report" pager <pageralert                                           
    				fi 
    			done
            ;;
        stop)
                    #TBD/Pending conversion using wrapper
            ;;
        status)
                    echo "Currently running under the following pid:
                    #`ps | grep $SOME_PID | grep -v grep | cut -c 1-5`
                    "
            ;;
        *)
            echo "Usage: $0 {start|stop|status}"
            exit 1
            ;;
    esac

    As far as using relative paths you make a point but i don't intend on moving them besides i want them to break if they do get copied elsewhere. As far as the background process issue, I do want them to complete so I added the extra but I would like to see an example of the wait command. I don't think i've ever used that yet. With the extra & I wonder if the script will run slower although speed is not of the utmost importance. Converted the idles. At first you lost me with the wrapper idea but i think i get it.. is it if I contain the loop, then for stopping the process it'll make it easier for the parent script?


    *yaaaawwnnnss
    BBSTSP
    Sponsored Links
    Closed Thread

    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes

    More UNIX and Linux Forum Topics You Might Find Helpful
    Thread Thread Starter Forum Replies Last Post
    Servers Stop and Start RG18173 Shell Programming and Scripting 3 01-22-2009 01:25 AM
    Making a Script to Start as a Service S.Vishwanath UNIX for Dummies Questions & Answers 3 02-08-2007 12:37 AM
    Start/Stop Script jjv1 UNIX for Dummies Questions & Answers 2 12-16-2003 03:28 PM



    All times are GMT -4. The time now is 12:16 AM.