Solved

Inconsisten shell behavior using cron

Posted on 1998-10-23
11
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
11 Comments
 
LVL 1

Accepted Solution

by:
MichaelKrastev earned 50 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This program is used to assist in finding and resolving common problems with wireless connections.
This article is in regards to the Cisco QSFP-4SFP10G-CU1M cables, which are designed to uplink/downlink 40GB ports to 10GB SFP ports. I recently experienced this and found very little configuration documentation on how these are supposed to be confi…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month6 days, 15 hours left to enroll

623 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