Check process count


 
Thread Tools Search this Thread
Operating Systems AIX Check process count
# 1  
Old 01-10-2014
Linux Check process count

Hi,

We have scheduled few jobs/scripts in cron and it runs on daily basis at a specified time. We have a cron jobs has various parameters for main scripts like

/home/test/mainscript.ksh main_param

when the above jobs triggers, mainscript.ksh has below code and it refers

main_param="param1 param2 param3 param4"

the script1.ksh triggeres one by one for these params like script1.ksh param1 (once it finishes its execution), then it triggers script1.ksh param2

So, consider script1.ksh param1 is still running till next day the same param1 triggers (as said cron jobs runs on daily basis), it overlaps the logs and makes more usage in the database/server.

In order to avoid that, I have included the below code

Code:
check=`ps -ef | grep-w "script1.ksh param1"| grep -v "grep" | wc -l`
if [ $check -gt 1 ]; then
echo "Already process running"
fi

It was running perfect in Sun solaris box, but in AIX, it started misbehaving like when the next param runs on the same day, it says that älready process is running"

script1.ksh param1 -- after it completion, it says that script1.ksh param2 is running (more than 1 instance condition) but there is no chance of param2 is running currently.

I have checked the condition as $check -gt 2, but it throws the message rarely that "Already process running"

Please help me to identify the correct process and throw this message.

your timely help is much appreciated as I m in the neck moment.
# 2  
Old 01-10-2014
You avoid the ps | grep | grep | awk | sed | cut | wc -l | kitchen | sink junk as well as implementation-dependent ps difference problems, by using PID files.

At the beginning of your scripts:

Code:
if [ -f /tmp/myscriptlock ]
then
        read PID < /tmp/myscriptlock
        if ps $PID >/dev/null
        then
                echo "Already running" >&2
                exit
        else
                echo "PID file but no process, did it quit unexpectedly?" >&2
        fi
fi

echo $$ > /tmp/myscriptlock
trap "rm -f /tmp/myscriptlock" EXIT

# rest of your script

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-10-2014
The trap .......... EXIT is very useful, but make sure that your code does not exec another program or reset the traps anywhere or you will not clean up the file. I've fallen over that one. Smilie

Also be sure that you don't have something that cleans up /tmp as I have fallen over this before when someone thought it would be a good idea to remove temporary files at 2am every night. It gets worse when the schedule runs late, but it's all so badly written that we need something to clean up every now and then so without a huge re-write of the schedule we're stuck. Can you see I'm experienced in it all going wrong? Smilie


This is still a much better idea than just counting processes, as Corona688 says. Of course, you need to ensure that /tmp/myscriptlock is unique in every script. Perhaps you could base it on the script actually running with something like:-
Code:
lockfile="${0##*/}.lock"



I hope that this helps,

Robin
Liverpool/Blackburn
UK
This User Gave Thanks to rbatte1 For This Post:
# 4  
Old 01-10-2014
The traditional place for PID files is actually in /var/run for that reason, but user scripts usually can't create files there, just daemons.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Telnet and get process count

Hi guys, I'm writing a script on a Solaris 10 server (server A) that needs to telnet 2 servers (server B & server C) and get a certain process count from these 2 servers. Then on server A, I check if both counts are greater than 17, I do a sendmail to concerned people. For the telnet part,... (7 Replies)
Discussion started by: frum
7 Replies

2. Shell Programming and Scripting

Process a file for line count using for loop in awk

Hi, I have a file with contents So what I have to do is In short, break the file after every 6 lines and then truncate new line to tab for these 6 lines. I am not able to execute the for loop in awk properly. The idea is something like this: less file| awk '{for .... {if ((nr>=1)... (7 Replies)
Discussion started by: rossi
7 Replies

3. Shell Programming and Scripting

Process count problem in script

Hi, I am trying to make a script which should execute if the same is not getting executed already. To implement this check, I wrote a fragment of code as below $ cat abc.sh #!/bin/bash check=0 #grepping "sh -x abc.sh" because if i check only for "abc.sh" and the file is open in say... (12 Replies)
Discussion started by: sam05121988
12 Replies

4. Shell Programming and Scripting

Request to check:count specific entries

Hi all I have an input file which contains so many entries like this: And, I want to count hw many among ASN in one column are converted to LYS in third coulmns. which means output shuld contain only "ASN number LYS" Kindly let me know wny programm for this My input is ASN 217 LYS... (2 Replies)
Discussion started by: manigrover
2 Replies

5. Shell Programming and Scripting

Using backspace in echo to show process count

i have something like this tablesName="abc def hij akn ... etc etc" count=0 for i in $tablesName do echo -en "\b\b\b\b\b\b\b\b\b\b\b\b\bTableCount: $count" count=`expr $count + 1` done the above is just a description wha i need is when the loop executes the... (1 Reply)
Discussion started by: vivek d r
1 Replies

6. UNIX for Dummies Questions & Answers

How a process can check if a particular process is running on different machine?

I have process1 running on one machine and generating some log file. Now another process which can be launched on any machine wants to know if process1 is running or not and also in case it is running it wants to stream the logs file generated by process1 on terminal from which process2 is... (2 Replies)
Discussion started by: saurabhnsit2001
2 Replies

7. Shell Programming and Scripting

How to check count of rows in excel sheet in Unix

Hi, Would anyone be able to tell me how to check the number of rows in an excel sheet on unix box, please? Cheers, Girish. (2 Replies)
Discussion started by: girish1428
2 Replies

8. UNIX for Dummies Questions & Answers

Thread count for a process id

Hi, I want to know a command/program to get thread count for a process id in unix box. Please help me in this regard. (1 Reply)
Discussion started by: manaac
1 Replies

9. HP-UX

how to see the threads count of a process in hp unix?

hi,all: how to see the threads count of a process in hp unix? thanks (2 Replies)
Discussion started by: bugbugbug
2 Replies

10. Programming

Count Number Of Threads in a Process

I am trying to find out that how many number of threads are currently running or in any other state which is created by POSIX standard in a process. First I have defined a variable called proc_var of type proc defined in sys/proc.h.Next I open up the dir /proc and per directory wise I do an ioctl... (7 Replies)
Discussion started by: S.P.Prasad
7 Replies
Login or Register to Ask a Question