?
Solved

Inconsisten shell behavior using cron

Posted on 1998-10-23
11
Medium Priority
?
383 Views
Last Modified: 2013-12-23
I'm on DG AViiON DG/UX 5.11
I noticed some inconsistencies in the execution of a shell script. When running it from the system prompt, all variables are properly set. However, if I schedule it through cron, the same variables are not set. Here is my script:
#!/bin/ksh
cat t1 | while read line
do
   echo $line
   eval $line
done
echo "a: $a"

The contents of file t1 is:
a=test
export a

When I run my script from the system prompt, the last echo command shows:
a: test
The same script run by cron shows:
a:
How do I make it consistent?
0
Comment
Question by:mgokman
  • 5
  • 3
  • 3
11 Comments
 
LVL 1

Accepted Solution

by:
MichaelKrastev earned 200 total points
ID: 1582494
Cron demon starts /bin/sh to execute scripts. It doesn't pay
attention to your #!/bin/sh in the first line of the script. This way you have ksh from the sysconsole (if this is your shell or because you explicitely call /bin/ksh) and /bin/sh from the cron. If you want to make sure cron uses ksh to execute your script, prepend /bin/ksh in the definition in your crontab entry, e.g. /bin/ksh <script_name>


0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1582495
MichaelKrastev, well cron uses /bin/sh and this knows what to do with #! in the very first line ;-)
The Problem is the echo command in the while loop: usually in a cron you cannot perform commands using stdout, stderr without redirecting the output.
0
 
LVL 1

Expert Comment

by:MichaelKrastev
ID: 1582496
Well, /bin/sh knows what to do with the magic number #! in the very first line, but it doesn't always want to do so. When is it willing to use it -- your are invoking a program and the shell must determine whether it is a standalone executable or interpretable script, and in the later case what interpreter to use. However, when you explicitely invoke /bin/sh and pass it script that begins with #!/bin/ksh, that /bin/sh process will not exec /bin/ksh to do the job. And it makes sense, if you start /bin/sh then the UNIX understands that this is exactly what you want.

Back to the question. The problem is the variable assignment or expression evaluation, I guess. It has nothing to do with the cron, because it behaves exactly the same way if you run the example manually, first with sh and then with ksh.

All mdokman wants is to have his script work consistently. And this is what I gave him -- run /bin/ksh <script> from the cron.

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 3

Author Comment

by:mgokman
ID: 1582497
Hi MichaelKrastev,
Once I put /bin/ksh at the beginning of the line in my crontab, everything started working the way I wanted.
I noticed quite a few times that certain things work differently on my platform depending on what shell is used, sh or ksh. Also, I'm not sure if eval is available in sh, so when it ran by sh, maybe my variable assignments didn't work. Or, it could be something else. One thing I know for sure using /bin/ksh to call my script from cron makes it work exactly the same way as calling it from the sstem prompt and that's what I needed.
Thanks a lot.
0
 
LVL 3

Author Comment

by:mgokman
ID: 1582498
After reading ahoffmann's comment, I want to mention that the script I gave in my question is certainly no the one I raly had troubles with. This was just a simplified example. My goal realy was to make use of the eval command. I have script that accepts many parameters. Instead of passing them on the command line, I decided to implement something similar to .ini files in Windows environment. This way I can put as many assignments as I want into a file myfile.ini. Then I can loop throught this file and use eval like in my example to make all variable assignments. The .ini file name will be the only parameter for my script. So, the echo command in my script is just for illustartive purpose, the important command is eval. Also, echo works fine no matter how I execute my script. My problem was that I was not getting all variables assignments when running it via cron. Now it works fine. Thanks to MichaelKrastev.
0
 
LVL 1

Expert Comment

by:MichaelKrastev
ID: 1582499
I confess, I can'n explain what is going on inside /bin/sh and /bin/ksh during the processing of eval. However, on Solaris (2.5.1) if you take a look at the end of the man page for eval, there is some vague explanation about the behaviour of eval under /bin/ksh. After reading it 10 minutes, I couldn't understand it. You may give a try.

0
 
LVL 3

Author Comment

by:mgokman
ID: 1582500
I just noticed that since I changed my cron to use /bin/ksh I started getting slightly different output in my script log.
I have the following in my crontab:
00 20 * * 1-4 /bin/ksh /path/myscript 2>&1 > /path/mylog
Within myscript I call various shell scripts and Oracle sql scripts. They all write their output to standard output. However, I don't see errors in the log file. I also don't see the output from dd command that copies files. Now this output goes to my mail.
Any suggestions how to put them all back to the log file. I could just use redirect inside my scripts, but I want to avoind it.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1582501
00 20 * * 1-4 /bin/ksh /path/myscript > /path/mylog 2>&1
0
 
LVL 3

Author Comment

by:mgokman
ID: 1582502
ahoffman,
following your example I changed my cron and now I see all output in my log file, no more errors in mailx. I'm not sure I understand the difference in my syntax and yours, although I read about it in a Unix book.
You deserve point for this answer, but I think I ran out of my points, so I need to check my balance. If I still have them I'll repost this question. You can answer it and I will award you points. I'd appreciate if you provide some explanation why my syntax produces different results.
Thanks a lot to you both.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1582503
from man sh:
--
  The order in which redirections are  specified  is  signifi-
  cant.   The shell evaluates redirections left-to-right.  For
  example:
      ... 1>xxx 2>&1
  first associates file descriptor 1 with file xxx.  It  asso-
  ciates  file descriptor 2 with the file associated with file
  descriptor 1 (that is, xxx).  If the order  of  redirections
  were  reversed,  file  descriptor 2 would be associated with
  the terminal (assuming file descriptor 1 had been) and  file
  descriptor 1 would be associated with file xxx.
--

This is one of the reason why I prefere csh and friends, where
this simply is written as:

     ... >& xxx
0
 
LVL 3

Author Comment

by:mgokman
ID: 1582504
I think I understand now. I think I confused file descriptors with actual files.
Thanks a lot.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
WARNING:   If you follow the instructions here, you will wipe out your VTP and VLAN configurations.  Make sure you have backed up your switch!!! I recently had some issues with a few low-end Cisco routers (RV325) and I opened a case with Cisco TA…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

592 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question