LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-05-2015, 04:42 AM   #1
murali2489
LQ Newbie
 
Registered: Jan 2015
Posts: 14

Rep: Reputation: Disabled
Best way to run two interdependent scripts


Hi All,

I have two scripts, the aim of these two scripts is, to check whether a particular script is running or not, if it wont runs, then throw a mail.

How i Achieved this output is, I wrote first script.

I created an infinite while loop which performs below steps

1. It creates a touch file
2. Triggers the script which needs to be monitored if its working or not.
3. Removes the touch file.

If the second step fails, then the remove file command will not happen and the script will stuck there itself.

I created an another script which checks the creation time of the touch file and if it is more than ten minutes, it means the second step in the first script is hanged, which also means that particular script is not working.

So if the creation time is more than 2 minutes the second script will throw a mail.

Below are the two scripts.

Code:
#!/bin/ksh



userid="chansd"

filename="/apps/log/check.txt"

while true ;do
touch $filename
pass=`/apps/eDMZ/call_st.ksh $userid`
sleep 20
rm $filename
done
Below script checks the file creation time and throws email if it is older than 2 minutes
Code:
#!/bin/ksh


filename="/apps/log/check.txt"

if [ -f "${filename}" ]
then
if test "`find $filename -mmin +2`"
then
echo "script is not working ! Please act on it" | mail -s "Script  is not working" Example@mail.com
fi


else

        exit 1

fi

What im going to do is

1. I am going to run the first script in background so it runs forever.
2. I am going to run the second script in cron forevry 5 mins to check the file creation time.

3. So if the first script hangs . I will kill the process using process id and after the issue resolves with the inner script, I will run the main script again.

I am new to Linux, Please let me know if this approach will work as expected.
 
Old 03-05-2015, 06:49 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,305

Rep: Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816
Looks ok and feasible, why do you want to run the first script in an infinite loop?
 
Old 03-05-2015, 06:53 AM   #3
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 981
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
daemontools supervise is a program to restart stuff each time it ends.
http://cr.yp.to/daemontools/supervise.html

alarm may be able to limit the run time of the inner script. Watch for situations that could cause longer execution than you expected.

If I'm calling and killing I like to do it in Perl with pipe()+fork()+select(). That way I know the pid without having to search for it, and I know the pid isn't being reused by another process just before my signal (because I haven't reaped it yet).

If your cron job doesn't remove the file you may be getting a lot of mail.
 
Old 03-05-2015, 08:29 AM   #4
murali2489
LQ Newbie
 
Registered: Jan 2015
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Looks ok and feasible, why do you want to run the first script in an infinite loop?
Hi Tentenths ... I created an infinite loop because it has to monitor the inner script for ever.
Is there any better way to do this other than what I did

the ultimate aim is to monitor call_st.ksh in the first script. call_st.ksh always returns string so if the script stops working it wont return anything and also the call to that script will never finish

Is there a better way to check whether it is working or not in any other way.

Hi linosaurusroot

Is this tool available inbuilt on Linux.. Please let me know
 
Old 03-05-2015, 02:11 PM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,484
Blog Entries: 10

Rep: Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643
Never done it, but just did some minor googling on the topic; why not issue the equivalent of waitpid() but the BASH replacement. It appears that you can wait and give PID as an argument. I'm just not versed on the exacting behaviors in BASH. In a C program you will get signals when the process you're waiting for terminates, in fact you should have a parent waiting on the child process to effect closure per the System V programming model. I would experiment with wait in the BASH script to see if it can deal with a script that self terminates: normally, due to error, or was killed by external means.
 
Old 03-05-2015, 03:30 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
From the sound of it, you just want to launch call_st.ksh every 20 seconds and send an email if it hasn't completed in 2 minutes? What do you want to do if it hasn't completed? Kill it or just wait for user intervention?

I would put this in one script, but launch call_st.ksh in the background with nohup and then keep tabs on its PID. If after 2 minutes the PID is still running, do whatever you need to do, otherwise just wait 20 seconds and run again.

eg:
Code:
while [[ 1 ]]; do
   nohup /apps/eDMZ/call_st.ksh $userid >pass.txt 2>/dev/null &
   pid=$!
   count=0
   ps -p $pid &> /dev/null
   status=$?
   while [[ $status -eq 0 && $count -lt 60 ]]; do
      ((count++))
      ps -p $pid &> /dev/null
      status=$?
   done
   if [[ $count -eq 60 ]]; then
      # send an email, kill $pid, whatever
   fi
   sleep 20
done

Last edited by suicidaleggroll; 03-05-2015 at 03:31 PM.
 
Old 03-05-2015, 10:05 PM   #7
murali2489
LQ Newbie
 
Registered: Jan 2015
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
From the sound of it, you just want to launch call_st.ksh every 20 seconds and send an email if it hasn't completed in 2 minutes? What do you want to do if it hasn't completed? Kill it or just wait for user intervention?

I would put this in one script, but launch call_st.ksh in the background with nohup and then keep tabs on its PID. If after 2 minutes the PID is still running, do whatever you need to do, otherwise just wait 20 seconds and run again.

Thanks Verymuch for the suggestion !!.


Please tell me if I want to kill this mainscript which you wrote. how do I do it... And even if I log out from the machine, this script should always run and whenever I need to kill this mainscript I should be able to do it..

Please advise how to achieve this..

Last edited by murali2489; 03-05-2015 at 10:20 PM.
 
Old 03-06-2015, 10:35 AM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
The same way you would kill any other process.
killall "process name"
kill $(pidof "process name")
ps -ef | grep "process name", then kill it manually
etc.

If you want it to continue running when you log out, then start it in nohup.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Interdependent C++ classes MTK358 Programming 17 09-16-2011 09:49 AM
How to run fsck during a shutdown sequence, or how to run one-time init scripts? bgoodr Debian 5 11-28-2010 04:08 PM
How to run .sh scripts piggysmile Linux - Software 2 06-24-2004 07:18 AM
How do i get scripts to run? jihadcman Fedora 3 06-06-2004 11:15 AM
cannot run scripts unless su odious1 Linux - General 5 07-19-2003 01:25 AM


All times are GMT -5. The time now is 05:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration