For loop for checking processes


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers For loop for checking processes
# 1  
Old 12-16-2010
For loop for checking processes

Hi Gang,

I need part of my script to be able to loop, check for processes running and if they aren't running, start them. It needs to loop 5 times, do a check each time, and make sure a process starts, and if its running; skip it.

I've worked with loops and checking for processes before, but this one has me stumped.

Last edited by jeffs42885; 12-16-2010 at 10:54 AM..
# 2  
Old 12-16-2010
A while loop is more appropriate...but where is your script?
# 3  
Old 12-16-2010
Code:
now=$(date + %u%H)                                                                                          
for i in 1 2 3 4 5                                                                                          
do                                                                 
process=$(ps -ef | grep process | awk '{ print $2}')                                        
if [ -n "$process" ]
 then
  process1=$(ps -ef | grep process1 | awk '{ print $2}')                                                
   if [ -n "$process1" ]                  
    then                          
     if [ $now -gt 100 ] || [ $now -lt 108]  # If sunday between 12 and 8                            
      cd /dir/startprocess1.sh
      cd /dir/startprocess2.sh
      cd /dir/startprocess3.sh
      
     else
      cd /dir/startprocess1.sh
      cd /dir/startprocess2.sh
      cd /dir/startprocess3.sh
      startcustomprocess1.sh
      startcustomprocess2.sh
      
     fi  
                                       
    else                                           
     cd /home/dir/process1.sh                                    
    sleep 10                                      
    
     if [ $now -gt 100 ] || [ $now -lt 108]  # If sunday between 12 and 8                            
      cd /dir/startprocess1.sh
      cd /dir/startprocess2.sh
      cd /dir/startprocess3.sh
      
     else
      cd /dir/startprocess1.sh
      cd /dir/startprocess2.sh
      cd /dir/startprocess3.sh
      startcustomprocess1.sh
      startcustomprocess2.sh
      
     fi 
   fi

   
else
  mail -s "Process isnt running" me@email.com
  sleep 15
  
 
fi
 
done

# 4  
Old 12-16-2010
What OS, what shell?
Code:
now=$(date +%u%H)  # no space between + and what follows

---------- Post updated at 17:08 ---------- Previous update was at 16:50 ----------

Code:
      cd /dir/startprocess1.sh
      cd /dir/startprocess2.sh
      cd /dir/startprocess3.sh

Im missing something here: Are you changing 3 time of directory (if so wht is the point?), or is there something missing ( cd; or cd dir1; startprocess1.sh)?
# 5  
Old 12-16-2010
It's KSH..AIX, but I was actually able to figure something out.

Heres what I want to do.

Code:
 
proc1=$(ps -ef | grep proc1| awk '{ print $2}')                      
proc2=$(ps -ef | grep proc2| awk '{ print $2}') 
while proc1 or proc2 is not running
do
script
done

I just need help setting up the part of the logic for "or" and "if it isn't running..would it be something like this.

Code:
while [$proc1 -lt 0 -o $proc2 -lt 0 ]
do
script
done

# 6  
Old 12-18-2010
Nikhil Nambiar

Hi Jeff,
provide me the processes that should run every day and put it in a file say file 1
and find the current that would compare the current processes alive and put the output 2 file say file 2
use diff command to find those processes not running .
and restart those process .

also to check for loops ,
if you require you can create another script which wuld run the above script with while condition and that condition which will always gets satisfied. say
Code:
#!/bin/bash
a=10
while [ "$a" -eq "10" ];
do
  run the above script 
  sleep (put the min u want to check) 
done


Last edited by Franklin52; 12-18-2010 at 10:54 AM.. Reason: Please indent your code and use code tags, thanks
# 7  
Old 12-20-2010
You might have trouble with using ps & grep, because sometimes
Code:
ps -ef|grep something

will find a processes "grep something", so you can get spurious errors.
You would be safer using a regular expression in the grep, even if there is nothing to expand. Something like:-
Code:
ps -ef|grep somethin[g]

will never match the grep statement itself. Apart from that, you code looks okay, but it could be simplified. If I may suggest:
Code:
proc1=$(ps -ef | grep -c proc[1])
proc2=$(ps -ef | grep -c proc[2])
if [ $proc1 -eq 0 -o $proc2 -eq 0 ]
then
   ....whatever....
fi

Of course, if you logic is that if neither process is running, you would want an AND condition, so a -a flag in the if statement. Of course, if that is the case, you can save some processing by combining the first two lines, so:
Code:
procs=$(ps -ef | egrep -c "proc[12]")
if [ $proc1 -eq 0 -o $proc2 -eq 0 ]
then
   ....whatever....
fi


I hope that this is useful, but if i have missed the point, please write back and hopefully I or someone else will correct it.




Robin
Liverpool/Blackburn
UK
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop without checking file exists

In several scripts that process files matched by name pattern I needed to add a check for file existence. Just to illustrate let's say I need to process all N??? files: /tmp$ touch N100 N101 /tmp$ l ?10 -rw-rw-r-- 1 moss group 0 Apr 19 11:22 N100 -rw-rw-r-- 1 moss group ... (10 Replies)
Discussion started by: migurus
10 Replies

2. Solaris

Solaris zones - checking processes and lofs file system

Hi all, q1) If i am in a global-zone, is there any command or anyway to check if a particular process in "ps -ef" output is running in which zone ? q2) if i have created and mount a lofs filesystem/mountpoint for my non-global zone, can i say the following e.g. /dev/md/dsk/d60 /data --... (1 Reply)
Discussion started by: javanoob
1 Replies

3. UNIX for Dummies Questions & Answers

Checking for processes in Linux

I want to check processes on my Linux server Normally we do a ps-ef|grep "search code" Now we want to check for certain processes which are up and running and we want to get an email, whenever the processes we are checking goes down? How can i implement this? (1 Reply)
Discussion started by: saggiboy10
1 Replies

4. Shell Programming and Scripting

while loop checking for file

Hi I my shell scripting(Main.ksh) i am calling the another loader.ksh using nohup inside the for loop ...so the above command runs parallel. Loader.ksh generate the dummy file seq_n.run file and deleted in the end of the Loader.ksh In the main.ksh .. after the for loop .. i have to check... (1 Reply)
Discussion started by: gavemani
1 Replies

5. Shell Programming and Scripting

Process checking loop

Hi, I want to create a script who will check if the java process is running & if it finds the process is still there it continues to execute & when the process completes it exit from the script. I have written a code to check & notify the process existence but i am not getting how to write... (4 Replies)
Discussion started by: d8011
4 Replies

6. Shell Programming and Scripting

Checking condition inside the loop

Hi all, I have one clarification i am using the loop which will process for each record .suppose there is f ailure in the first record it need to send mail and process the next .my code: defcount=`cat <filename>|wc -l` while ] do if <some condiotion> then echo "mail" fi done so... (1 Reply)
Discussion started by: ithirak17
1 Replies

7. Programming

forking n number of processes in a loop and not 2^n

Hi, Am working on linux. while forking in a loop how to avoid the child process from forking..for example int n = 5; pid_t pid; pid_t ch_pid; /*exactly wanted to create n processes and not 2^n processes*/ for(i = 0; i < n;i++) { if(pid = fork()) { /* parent... (4 Replies)
Discussion started by: rvan
4 Replies

8. Shell Programming and Scripting

Checking before start and stop processes

Hi, I have 2 start and stop sh. Start sh -------- This will start few processes. Example code: echo "start process : lgz200 /pipe=test_jobs" nohup lgz200 /db=test/test1@test1 /pipe=test_jobs > ../log/lgz200_j.log & echo "echo \"stop process (pid=$!): lgz200 /pipe=test_jobs\"" >>... (3 Replies)
Discussion started by: maldini
3 Replies

9. UNIX for Dummies Questions & Answers

checking Processes

I have aix version 5.1 I was wondering how I can check all running processes? I have used several ps commands but have been unsuccessful at it. I can see all with {ps aux } I have a little trouble knowing what I am looking for also. I should be looking at length of time and processor usage? I am... (3 Replies)
Discussion started by: rocker40
3 Replies

10. Shell Programming and Scripting

Checking Return Codes of Background Processes

I'm trying to do the following: 1) Run a bunch of jobs in the background 2) Determine if any one of them returns with a non-zero exit status Here's what I've come up with so far: ########################################## #!/bin/ksh while } -lt 1024 ] do SLEEP_TIME=`expr 1024 -... (2 Replies)
Discussion started by: bergerj3
2 Replies
Login or Register to Ask a Question