Shell script as root cron job

Shell script as root cron job

Post by <pra.. » Mon, 30 Apr 2001 00:27:24



I am trying to run a shell script as a root cron job which uses the useradd
cmd to add a new user. The other parts of the script run fine but the
useradd cmd fails saying the it cannot find the cmd. The script runs fine
(including the useradd) if run explicitly from the cmd line. The useradd cmd
works fine run separately or in a script run from the cmd line. The shell
script is owned by root and is part of the root grp. All of the permissions
are set (RWX). Any ideas as to why the useradd does not work from cron'd
script would be appreciated.
Thanks
Tony

 
 
 

Shell script as root cron job

Post by Kaz Kylhe » Mon, 30 Apr 2001 01:06:41




Quote:>are set (RWX). Any ideas as to why the useradd does not work from cron'd
>script would be appreciated.

Because in a process run from cron, you can only assume that your PATH variable
contains /bin and /usr/bin. Your useradd program is probably elsewhere, like
perhaps in /sbin or /usr/sbin. (On the Linux distro I have at hand here,
it is called /usr/sbin/adduser).

 
 
 

Shell script as root cron job

Post by Peter T. Breue » Mon, 30 Apr 2001 01:10:51



> I am trying to run a shell script as a root cron job which uses the useradd
> cmd to add a new user. The other parts of the script run fine but the
> useradd cmd fails saying the it cannot find the cmd. The script runs fine

Then it can't. Face up to the awful fact: the "useradd" command is not in
the path when the cronjob runs!

Quote:> (including the useradd) if run explicitly from the cmd line. The useradd cmd
> works fine run separately or in a script run from the cmd line. The shell
> script is owned by root and is part of the root grp. All of the permissions
> are set (RWX). Any ideas as to why the useradd does not work from cron'd
> script would be appreciated.

Why you can't accept the implications of your observation
is far more puzzling to me! How about running a cronjob whose task is
to mail you its path? That would settle the issue. But it's a cert.
Look at the man page for cron.

Peter

 
 
 

Shell script as root cron job

Post by Floyd Davidso » Mon, 30 Apr 2001 02:58:10



>I am trying to run a shell script as a root cron job ...

Explicitly set the PATH variable in the shell script, and
include the directories for any and all commands that the
script uses.  Commonly there are several directories that
your login shell may include in the PATH variable which are
not added for a non-interactive shell run by cron.

  PATH=/usr/local/bin:/bin:/usr/bin/:/sbin:/usr/sbin

Another way is to call all commands using the full pathname,
and that can easily be done by using variables that are
configured at the top of a shells script.

   #!/bin/sh
   #
   GREP="/usr/bin/grep -w"
   LS=/bin/ls

   ${LS} /bin | ${GREP} ls

--
Floyd L. Davidson         <http://www.ptialaska.net/~floyd>

 
 
 

Shell script as root cron job

Post by Kaz Kylhe » Mon, 30 Apr 2001 14:29:45




>Why you can't accept the implications of your observation
>is far more puzzling to me! How about running a cronjob whose task is
>to mail you its path? That would settle the issue. But it's a cert.
>Look at the man page for cron.

What makes me curious is why anyone would want to add a user from a cron job.
 
 
 

1. Problem while running the Shell script as a cron job

Hi,

I am facing this weird problem on my AIX server (ver 4) :

I have a shell script that is supposed to read the return value from a
executable program and then take actions based on the value returned.

The script is like this:

#!/bin/ksh
###########################################################################
# Script: /celscripts/autosetupCheck.sh
#
# This script reads the numeric value returned by the executable
autoNPRCount
# and then sends out an email to the admin if the value is greater than
maxDepthMail
#
###########################################################################

maxDepthMail=500
MSG="Please check the NPR queue"

queueDepth=`/celutil/autoNPRCount`

if [ $queueDepth -ge $maxDepthMail ]
then
    echo $MSG|mail -s "NPR alert" $ADMIN
fi
exit 0

When I run this script manually, it works perfectly fine. But, when I
run this script as a cron job, I get the following error:
/celscripts/autosetupCheck.sh[16]: 500: 0403-012 A test command
parameter is not valid

This error comes because the variable "queueDepth" is blank. Which
means that while running it as a cron job, the shell is not able to
assign the value returned by "autoNPRCount" to the varibale
"queueDepth".

Solutions/Suggestions for the above problem are most welcome.

Many Thanks,
Nishant

2. Creative 6x

3. Examples of shell scripts for (bash) for a ftp cron job?

4. toshiba libretto floppy

5. script runs from shell but not when called as cron job???

6. server performance comparisons?

7. cron - shell script would not generate output in cron

8. mylex

9. Cron Problems - Script runs from shell but not cron

10. cron did not run root jobs (solaris 2.5.1)

11. Cron jobs running as something other than root

12. Solaris: root cron jobs halting

13. timing of root cron jobs