Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Inconsisten shell behavior using cron

Posted on 1998-10-23
11
Medium Priority
?
379 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

885 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