Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ps: no controlling terminal when running shell script from cron

527 views
Skip to first unread message

kiran kodith

unread,
May 31, 2006, 11:36:46 AM5/31/06
to
Hi,

I am getting "ps: no controlling terminal' when I run a shell script
from within cron. I have a shell script that has to run sequentially
and wait for the previous step to finish before the next step gets
executed. I have implemented that in shell by using ps. Here's the
example -

sh $JOB
#sleep in 3 second intervals until the job is finished
while ps |grep ${JOB}
do
sleep 3
done

The steps are dynamic; so I cant hardcode the steps in a shell script
and run. I dont want to build a shell script and run that instead (al
though that would be the last resort). The shell script works
flawlessly when I am logged on to the machine and run the job from
command prompt; but when I set it up as a cron job; I get "ps: no
controlling terminal' and it skips the while loop.

Any help will be greatly appreciated
Kiran Kodithala

Robert Lawhead

unread,
May 31, 2006, 11:57:46 AM5/31/06
to
kiran kodith wrote:
> Hi,
>
> I am getting "ps: no controlling terminal' when I run a shell script
> from within cron. I have a shell script that has to run sequentially
> and wait for the previous step to finish before the next step gets
> executed. I have implemented that in shell by using ps. Here's the
> example -
>
> sh $JOB
> #sleep in 3 second intervals until the job is finished
> while ps |grep ${JOB}
> do
> sleep 3
> done
>
> The steps are dynamic; so I cant hardcode the steps in a shell script
> and run. I dont want to build a shell script and run that instead (al

Would you elaborate a bit? Is the job you are waiting on started
from the shell that is waiting? Would "wait $pid" (man -s1 wait)
to the job? Why wouldn't something like
$step1 && $step2 && $step3 ...
work for serializing the steps?

Dexthor

unread,
May 31, 2006, 12:00:56 PM5/31/06
to


ps without any arguments displays processes associated with your TTY.
If you are running this command from a Cron, you wont have a terminal
and the command fails.

You may want to try: " pgrep your_cmd_pattern" or "ps -ef|grep ..."

HTH
Dexthor.

kiran kodith

unread,
May 31, 2006, 12:59:40 PM5/31/06
to
Dexthor,

That must be it, duh!!!!

I will try ps -ef and will let you know. I have a good feeling it will
work.

Thanks a lot!!!!
Kiran

kiran kodith

unread,
May 31, 2006, 1:02:12 PM5/31/06
to
Robert,

I have never used && - I am a newbie for shell scripting. Would that
serialize my steps?

I can grab the pid; but I need ps to get the pid too right? one of the
users suggested using ps -ef I think that will definitely work. I will
try that one also.

Kiran

Casper H.S. Dik

unread,
May 31, 2006, 1:31:50 PM5/31/06
to
"kiran kodith" <kiran.k...@gmail.com> writes:

>I am getting "ps: no controlling terminal' when I run a shell script
>from within cron. I have a shell script that has to run sequentially
>and wait for the previous step to finish before the next step gets
>executed. I have implemented that in shell by using ps. Here's the
>example -

By default, the current terminal is used andps will quit if can't find
it so you will need to use one of the flags such as "-u user" or "-e".

But you are probably better off using something like:

pwait `pgrep job`

Casper

Robert Lawhead

unread,
May 31, 2006, 1:58:53 PM5/31/06
to
kiran kodith wrote:
> Robert,
>
> I have never used && - I am a newbie for shell scripting. Would that
> serialize my steps?

job1; job2 ... starts job2 upon the completion of job1
job1 && job2 ... starts job2 upon the successful completion of job1
(ie exit code was 0)
In either case, they are "serialized", which is why I'm a bit
confused about your methodlolgy.


>
> I can grab the pid; but I need ps to get the pid too right? one of the
> users suggested using ps -ef I think that will definitely work. I will
> try that one also.
>
> Kiran

How you get the pid depends on how you started the process...
Within your shell "$!" is the pid of the most recently started
backgrounded process. Since it appears that you know the process
by name (when using ps) and that is unique, you could use pgrep
to get the pid, so something like "wait `pgrep $job`" should work
as would "pwait `pgrep $job`". I'd guess that it would be preferable
to use the shell built-in "wait", but haven't used apptrace or similar
to confirm this.

Bob

0 new messages