Avoiding 'sh -c' when running ps from CRON


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Avoiding 'sh -c' when running ps from CRON
# 1  
Old 03-07-2012
Avoiding 'sh -c' when running ps from CRON

Hi,
I have a script which has the below line:
Code:
ps -ef | grep ${SCRIPT_NAME} | grep ksh | grep -v grep >> /tmp/instance.tmp

When the script is invoked through CRON, I get 2 lines in instance.tmp when actually only one instance is running:
Code:
    cdrd 17790 17789  0 15:14:01 ?        0:00 /bin/ksh /scripts/dispatch.ksh CLIENT.cfg
    cdrd 17789   191  0 15:14:01 ?        0:00 sh -c /scripts/dispatch.ksh CLIENT.cfg 2>/dev/null >/de

Why is the 'sh -c' entry getting displayed in ps -ef ? Is there anyway to avoid the 'sh -c' entry from the output of 'ps -ef' ?
I could always use 'grep -v "sh -c"', but I am looking to avoid grep.

Please help.

Thanks in advance.

Last edited by Franklin52; 03-07-2012 at 03:53 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 03-07-2012
this line:
Code:
cdrd 17789 191 0 15:14:01 ? 0:00 sh -c /scripts/dispatch.ksh CLIENT.cfg 2>/dev/null >/de

is the parent process of this line:
Code:
cdrd 17790 17789 0 15:14:01 ? 0:00 /bin/ksh /scripts/dispatch.ksh CLIENT.cfg

There are usually better ways to find running scripts. What OS are you on?
# 3  
Old 03-07-2012
Quote:
Why is the 'sh -c' entry getting displayed in ps -ef ?
It's the way cron itself runs a cron line.

Your "grep ksh" is finding the string "ksh" in the name of the script.

If you are trying to prevent two occurances running at the same time, this is better achieved by using a flag file. Beware that the unix "ps" command can sometimes miss processes if the kernel is under stress.
# 4  
Old 03-07-2012
I am using the Sun solaris OS.

So if flag file the only option I can look forward to ?
Please suggest if there is any other option along with ps that will prevent 'sh -c' from being fetched..
# 5  
Old 03-07-2012
There is no solution which will stop cron from using sh -c. It is the way cron works. When you post a crontab take note of the message you receive.


Ps: "I am using the Sun solaris OS"
This could be almost any version of a Berkeley Unix or Unix System V Operating System amongst the Sun (now Oracle) hybrids:
Solaris (operating system) - Wikipedia, the free encyclopedia

Use the uname -a command and blot anything confidential like computer names with X's.

Interrogating "ps -ef" with "grep" is not the best way to determine if another instances are running (and it is unreliable). Use semaphore files.
In your context if you really want to use ps, then ps -fu<username> reduces the possibilities for mismatches (but sometimes ps will still omit processes).


Ps. The simple answer to your question is to use grep -v but you have rejected this for some reason.

Last edited by methyl; 03-07-2012 at 06:55 PM.. Reason: grammar, ambiguity and layout
This User Gave Thanks to methyl For This Post:
# 6  
Old 03-08-2012
Thanks methyl.

My uname result is:
SunOS xxxxxx Generic_118558-02 sun4u sparc SUNW,Sun-Fire-V240.

Can you please detail on the semaphore method that you talked about ?
I could use that probably if the 'ps' is unreliable as you suggest.

Thanks.
# 7  
Old 03-08-2012
Quote:
Originally Posted by cavallino4u
Hi,
I have a script which has the below line:
Code:
ps -ef | grep ${SCRIPT_NAME} | grep ksh | grep -v grep >> /tmp/instance.tmp

When the script is invoked through CRON, I get 2 lines in instance.tmp when actually only one instance is running:
Code:
    cdrd 17790 17789  0 15:14:01 ?        0:00 /bin/ksh /scripts/dispatch.ksh CLIENT.cfg
    cdrd 17789   191  0 15:14:01 ?        0:00 sh -c /scripts/dispatch.ksh CLIENT.cfg 2>/dev/null >/de

Why is the 'sh -c' entry getting displayed in ps -ef ? Is there anyway to avoid the 'sh -c' entry from the output of 'ps -ef' ?
I could always use 'grep -v "sh -c"', but I am looking to avoid grep.

Please help.

Thanks in advance.
try like this Smilie
Code:
ps -ef | grep ${SCRIPT_NAME} | grep [^.]ksh | grep -v grep >> /tmp/instance.tmp

This User Gave Thanks to ygemici 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

Script not running in cron

Hi All, I have a script which is running fine while triggered manually, However if I placed in crontab it throwing an error. #!/usr/bin/ksh set -vx lc=1 st_date=$(`date "+%Y%m%d"`) LOGFILE=/home/transfer.log.$st_date file="/home/OM_WF.log.$st_date" Manual run - lc=1 + lc=1... (4 Replies)
Discussion started by: nag_sathi
4 Replies

2. Solaris

Cron job running even after cron is removed

Hi , I have removed a cron for particular user , but cron job seems to be running even after the cron entry is removed. The purpose of the cron was to sendmail to user ( it uses mailx utility ) I have restarted cron and sendmail service still user is getting mail alerts from the cron job. And... (4 Replies)
Discussion started by: chidori
4 Replies

3. UNIX for Advanced & Expert Users

sys cron not running

Hi all, I have a solris 10 machine and i am facing some strange issues. My sys user cron is not executing. Can any one help on this issue? root@antsdp1b>svcs -a|grep -i cron online 1:08:55 svc:/system/cron:default root@antsdp1b> the cron process is also running. but whatever i... (5 Replies)
Discussion started by: vivek.goel.piet
5 Replies

4. Solaris

running scripts from cron

I need to run a script (from cron) that runs a second script. What worries me is that second script, when run from bash, upon finishing doesn't return to bash unless I press the 'Enter' key. I presume that's because last command in that script ends with & Could that be a problem for cron?... (2 Replies)
Discussion started by: orange47
2 Replies

5. UNIX for Advanced & Expert Users

Cron not running

Hello All, I have installed a few crons on a machine. But for some reason the crons just don't run. I have checked the permissions on the files and also restarted the cron daemon. But it doesn't seem ti help. Can anyone suggest any other things I can do to get it running again? Regards,... (6 Replies)
Discussion started by: garric
6 Replies

6. Shell Programming and Scripting

Cron not running .. please help

i have a simple script that ftp a file out, manually running it works fine. #!/bin/sh ftp 152.226.69.48 << cmd bin get applicant_vw.txt quit cmd but when I cron it, 14 9 * * * /opt/home/XXX/XXX/dailyjob.sh > /opt/home/XXX/XXX/cronlog.txt seems that it was executed because i can... (2 Replies)
Discussion started by: beisaikong
2 Replies

7. UNIX for Dummies Questions & Answers

running cron jobs

I would like to add a job using cron. I have following questions - cron jobs are launched using what login-id e.g. is the script launced using my login-id - In which directory would the cron jobs be launched ? - where do the environment variables required in the script being lauched come... (3 Replies)
Discussion started by: sharanbr
3 Replies

8. HP-UX

Cron Job Not Running

Hi, I have a cron schedule like this 04,16,28,40,52 * * * * /nag/startProcessABatch (unix script) i want to add new lines in this file (like Logging), i just copy this file into a /tmp folder (for backup copy), and i have edited this file (added few lines of code for logging). ... (1 Reply)
Discussion started by: nag_sundaram
1 Replies

9. UNIX for Dummies Questions & Answers

cron is already running message...

I am trying to schedule a new job but I get # cron # ! cron is already running Mon Jul 11 08:28:51 WAT 2005 ! ******* CRON ABORTED ******** Mon Jul 11 08:28:51 WAT 2005 I tried the -l and -e switches but I get the same message I am using HP-UX Thanks. (3 Replies)
Discussion started by: GNMIKE
3 Replies

10. UNIX for Advanced & Expert Users

not running in cron

I have written a shell script and when i run it from shell prompt it runs fine but in the cron it only runs partially. That means cron is working fine. there is a command which i am using in the script which doesn't run at all. That comman uses different user privileges like ptadmin i am not... (4 Replies)
Discussion started by: ajnabi
4 Replies
Login or Register to Ask a Question