[comp.lang.perl] Questions about exec and scripts called from cron

okamoto@hpcc01.HP.COM (Jeff Okamoto) (09/28/90)

Two more puzzling questions for patchlevel 18:

1. Why does this construct not do what I think it should?

	exec "/non-existent-file" || die "Can't exec: $!\n";

2. Why does this construct not work when this script is called
   from cron?

	#!/usr/local/bin/perl
	eval "exec /usr/local/bin/perl -S $0 $*"
		if $running_under_some_shell;

   The output here is a message from cron, saying:

	sh: <first_file_in_home_directory>: execute permission denied

   (or something like that).
-- 
 \      oo	The New Number Who,
  \____|\mm	Jeff Okamoto
  //_//\ \_\	HP Corporate Computing & Services
 /K-9/  \/_/	okamoto@ranma.corp.hp.com
/___/_____\	
-----------	(415) 857-6236

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (09/29/90)

In article <1180002@hpcc01.HP.COM> okamoto@hpcc01.HP.COM (Jeff Okamoto) writes:
: Two more puzzling questions for patchlevel 18:
: 
: 1. Why does this construct not do what I think it should?
: 
: 	exec "/non-existent-file" || die "Can't exec: $!\n";

Because exec is a list operator, and thus has a lower precedence than ||.
Say this:

	exec("/non-existent-file") || die "Can't exec: $!\n";

or

	exec "/non-existent-file";
	die "Can't exec: $!\n";

: 2. Why does this construct not work when this script is called
:    from cron?
: 
: 	#!/usr/local/bin/perl
: 	eval "exec /usr/local/bin/perl -S $0 $*"
: 		if $running_under_some_shell;

I don't have enough to go on here, but bear in mind a coupla three things.
First, cron runs without much of a PATH, and the -S switch above
uses PATH to try to locate the script.  Second, cron could conceivably
be feeding the text above to /bin/sh through stdin, in which case $0 isn't
going to be meaningful, and you'd probably try to execute the first argument
of $*.  Third, if the arguments you're passing contain any whitespace,
you should instead use

	eval 'exec /usr/local/bin/perl -S $0 "${1+$@}"'
		if $running_under_some_shell;

Hope that helps.  You might just invoke perl directly from cron instead
of relying on the eval-exec hack.

Larry

okamoto@hpcc01.HP.COM (Jeff Okamoto) (10/03/90)

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) says:

> Cron could conceivably be feeding the text above to /bin/sh through
> stdin, in which case $0 isn't going to be meaningful, and you'd
> probably try to execute the first argument of $*.

I suspect this is exactly what is happening.  Perhaps cron checks the
file type and only force-feeds through sh if it isn't a legitimate
executable?

> If the arguments you're passing contain any whitespace, you should
> instead use
> 	eval 'exec /usr/local/bin/perl -S $0 "${1+$@}"'
> 		if $running_under_some_shell;

Fortunately, my arguments are vanilla, but I'll remember this. 
Perhaps this should become the new standard for non-#! machines?

As for the original problem, I've tried all the following cron
entries and not only do they not work correctly, but they all give
me the same error message from cron:

	40 10 * * * * /usr/staff/bin/identck.pl
	40 10 * * * * /usr/local/bin/perl /usr/staff/bin/identck.pl
	40 10 * * * * /usr/local/bin/perl < /usr/staff/bin/identck.pl

I've also tried removing the eval/exec (since my machine does
understand #!) and that still doesn't work....
-- 
 \      oo	The New Number Who,
  \____|\mm	Jeff Okamoto
  //_//\ \_\	HP Corporate Computing & Services
 /K-9/  \/_/	okamoto@ranma.corp.hp.com
/___/_____\	
-----------	(415) 857-6236

vixie@wrl.dec.com (Paul Vixie) (10/04/90)

# > Cron could conceivably be feeding the text above to /bin/sh through
# > stdin, in which case $0 isn't going to be meaningful, and you'd
# > probably try to execute the first argument of $*.
# 
# I suspect this is exactly what is happening.  Perhaps cron checks the
# file type and only force-feeds through sh if it isn't a legitimate
# executable?

Sounds fishy, and stupid, which means that cron is probably doing it.

I recommend use of my cron, which can be had from gatekeeper.dec.com's
anonymous FTP, in /pub/misc/vixie.  This is the released 2.0 version,
which was due in comp.sources.unix last month and may appear in 4.4bsd
whenever that comes out.
--
Paul Vixie
DEC Western Research Lab	<vixie@wrl.dec.com>
Palo Alto, California		...!decwrl!vixie

aem@mthvax.cs.miami.edu (a.e.mossberg) (10/04/90)

In <1990Oct4.064946.25547@wrl.dec.com> vixie@wrl.dec.com (Paul Vixie) writes:
>I recommend use of my cron, which can be had from gatekeeper.dec.com's
>anonymous FTP, in /pub/misc/vixie.  This is the released 2.0 version,
>which was due in comp.sources.unix last month and may appear in 4.4bsd
>whenever that comes out.

I second the recommendation. Vixie's cron is excellent, and I've been
using it for awhile now. It is also available from here (mthvax.cs.miami.edu)
as ~ftp/pub/VixieCron.shar.Z


aem

-- 
aem@mthvax.cs.miami.edu .......................................................
Rose is a rose is a rose.				- Gertrude Stein