Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 383
  • Last Modified:

Inconsisten shell behavior using cron

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
mgokman
Asked:
mgokman
  • 5
  • 3
  • 3
1 Solution
 
MichaelKrastevCommented:
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
 
ahoffmannCommented:
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
 
MichaelKrastevCommented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
mgokmanAuthor Commented:
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
 
mgokmanAuthor Commented:
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
 
MichaelKrastevCommented:
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
 
mgokmanAuthor Commented:
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
 
ahoffmannCommented:
00 20 * * 1-4 /bin/ksh /path/myscript > /path/mylog 2>&1
0
 
mgokmanAuthor Commented:
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
 
ahoffmannCommented:
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
 
mgokmanAuthor Commented:
I think I understand now. I think I confused file descriptors with actual files.
Thanks a lot.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now